srikanth mycherla

Greenhorn
+ Follow
since Oct 31, 2008
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by srikanth mycherla

As this topic is leading to Generics..can someone post a small example for generics!


2)prior to java 1.5 java doesnt support parameterized ArrayLists like ArrayList<int> etc
then prior to 1.5 what type of objects can i store in the ArrayLists? I suppose String references are objects and i can store them .

can i store anythin other than String earlier to 1.5 version ?



Any java Object leaving primitives.


yes you are right but any java object in the sense in the data types only string is possible na?
LAST BUT NOT THE LEAST! CONGRATULATIONS PUNIT ...ROCK !
12 years ago
Thank you Ganesh for your reply..you are right
YA .What Ankit said is absolutely correct .....

regards --Ankit , punit and sachin

lets discuss the burning topics here after
ya but ankit is saying there is no constructor in object class ! but i can see constructor in api..confused!

but ankit in api i can see the constructor
as object()
ya i suppose

in object class there is a constructor whose access specifier is default .


but if we dont write any constructor in our class then the compiler will add one public constructor for us

as public classname()
{
super();
}


the specifier is public since we may extend the class across packages

default is not added by the compiler even our class is default .it adds public constructor in order to work across packages

even if the user prefixes public before his class name at any stage there wont be a problem

so it adds public default constructor irrespective of class scope
i agree with you .but will the compiler add public constructor if the classs is default ? In the book it did like this .Is that right?
sorry punit .there is a default constructor in object class.
punit if my class is public (obviously since ill have main method in that class) how come the line
public super() (added by the compiler ) can invoke default constructor of object class ?
confused!!!
ya.Punit is right .There is no default constructor in Object class.but punit You are saying that the compiler will add a constructor


will it again add in the same way as it added in the class.then where will the call go when super is called .

There is no super classs of object class ? right?
Q)/*Which of the lines of code if added to the class on the left at point A . would cause exactly one additional object to be eligible for the garbage collector ? ( Assume that point A will execute for a long time giving the garbage collector time to do its stuff )*/




These can be substituted in the place of A
1.copyGC = null;
2.gc2=null;
3.newGC =gc3;
4.gc1=null;
5.newGC=null;
6.gc4=null;
7.gc3=gc2;
8.gc1=gc4;
9.gc3=null;

These are my assumptions

A) Starting with main method line 7 indicates that we are creating one reference variable for the class GC
B) line 8 indicates that the reference variable gc2 is pointing to one GC object
C) line 9 indicates that the reference variable gc3 is pointing to another new Gc Object

D) line 10 indicates that we are copying ref variable gc3 into gc4 that indicates gc4 and gc3 are pointing to same object

E)line 11 gc1=doStuff(); indicates the compiler to search for doStuff() method ..the compiler sees that the gc1 ref variable type is GC so it goes to line 2 . there it sees -> public static GC doStuff(). here it sees static method doStuff whose return type is GC. and it sees the type GC and checks that the calling type is also GC so it proceeds further ..and goes to line 3 GC newGC = new GC (); which indicates that another reference variable newGC of type GC is pointing to another new object GC ();

F) THEN IT GOES to line 4 doStuff2(newGC); and enters into line 12
where local variable copyGC holds the reference variable newGC

BUT THE OBJECT WHICH WAS CREATED IN CLASS GC doesn't die due to passing of reference variable into copyGC . (AM I RIGHT )
G) Again it comes back to line 4 and goes into line 5 return newGC; and newGC IS RETURNED TO LINE 11.gc1=doStuff(); SO NOW AGAIN THE COMPILER SEES THE TYPE OF newGC which is GC AND TYPE OF gc1 WHICH IS GC so newGC is assigned to gc1.

H) the moment the value is returned to line 11 the object newGC() in static GC doStuff() DIES as the scope is out of the method

I) now gc1=newGC means both are reference variables of class GC but pointing to no object ( AM I RIGHT HERE ) what it indicates by saying gc1= newGC . IN MY VIEW
SINCE NEWGC'S OBJECT is died . By saying gc1=newGC it indicates gc1 = null or can we access gc1 and newGC IN MAIN??? WHICH IS CORRECT ?

J) now substituing the values in the place of A


K) .copyGC = null;// ATTEMPTS TO ACCESS A VARIABLE WHICH is out of scope (not permitted )

L).gc2=null; //permitted since gc2 holds only one object and if we assing it to null the object is eligible for GC so ok

M).newGC = GC3; // OUT OF SCOPE

N) [b]GC1=NULL// IAM CONFUSED WITH THIS .i think gc1 is not pointing to any object then whats the use of saying GC1=null ;[/b]

I THINK since GC1=null another reference variable newGC is also pointing to GC1 so newGC also holds null

but this line is permitted according to answer but why when gc1 and newGC reference variables are not pointing to any object how can we say that the object is eligible for GC (DOUBT)


O)newgc=null// out of scope ok

P).gc4=null; // no since we know gc3 and gc4 are pointing to same object . by assigning gc4 to null gc3 is still pointing to object so not eligible for garbage collection

Q). gc3=gc2;// no since gc4 is still referring to the object
(no doubt) here also

R).gc1=gc4; // here the answer says ok but i didnt understand why

here gc4 is pointing to the object which is pointed by gc3 and when we say gc1=gc4 then gc1 also points to the same object

so gc1 gc4 and gc3 are pointing to same object am i right?

till now gc1 is not holding any object when we say gc1 = gc4 then how come the object is eligible for garbage collection according to answer


S)gc3= null // ok sunce gc4 is still referring to the object


i have written A to S statements which is my opinion towards the above code
please correct me where my way of thinking is wrong by seeing the A TO S statements .


Please indicate when the object is eligible for GARBAGE-COLLECTION between function calls ?
please correct me ranchers

When a class i written in java

like Then since we havent written any constructor for it . the compiler sees that and places a constructor like this and also writes super(); in it .


Am i right till here ?


1) when super () ; key word is there its function is to call the superclass's dummy constructor(no argument constructor ) .We know that Object class is the mother of all classes (super class) .

2) I didnt find any constructor in Object class like that while referring the api . so since the Object class doesnt contain any constructor .it should be a compiler error i suppose !


But why is the compiler ignoring it .

Is MY way of thinking right..correct me ranchers





This is posted by Kshitij dogra ..it is also a good answer to this question
12 years ago