Can u tell me whats the difference between instance,object and reference
How can i calibrate the below lines based on the above keywords..
String s; String s=null; String s=new String();
instance and object are same. Many objects/instances of a class can be formed. A class is like a template from which an object is created in the memory. Each object of same class keeps its own copy of variables unless those varaibles are class variables.
Reference is the address of object/instance in the memory.
String s; is declaration of variables s which is of type String class and hold some garbage value as reference i.e. can refer to some arbitrary value in memory.
String s=null; means declaration of s and it holds value null i.e. it doesn't refer to any object as yet.
String s=new String(); means declaration of s and it holds a reference to an object of type String class.
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
Originally posted by Damanjit Kaur: instance and object are same.
Not exactly. "Instance" is a more general term, meaning a concrete realization of a more abstract concept. An instance of a class is an object, but there are other instances, too. For example, an instance of an association is a link. RUP is a process framework, and when you create your personal process from it, you get "an instance of RUP". Etc. pp.
Hope this helps.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Peter Chase
Ranch Hand
Joined: Oct 30, 2001
Posts: 1970
posted
0
Originally posted by Damanjit Kaur:
String s; is declaration of variables s which is of type String class and hold some garbage value as reference i.e. can refer to some arbitrary value in memory.
No, you're maybe thinking of "C" language. In Java, such arbitrary values do not happen.
If "String s;" occurs within the definition of a method (i.e. it is a "local variable"), then that declares a variable "s" that is a reference to a String. No value is assigned. However, Java will not allow the value of local variable "s" to be examined until it has been assigned; the compiler will refuse to compile the code if you try to examine it before it has a value.
If "String s;" occurs as the definition of a field of a class, then "s" has the value null, which is not an arbitrary value, but instead means "I am a reference currently pointing to no object".
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
Damanjit Kaur
Ranch Hand
Joined: Oct 18, 2004
Posts: 346
posted
0
Yes one can't use variables declared inside method as automatic initialization doesn't occur. but compiler will compile if you just declare them inside method body without using the variable.
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
Originally posted by Damanjit Kaur: Yes one can't use variables declared inside method as automatic initialization doesn't occur. but compiler will compile if you just declare them inside method body without using the variable.
My guess then would be that it simply ignores the declaration and doesn't generate any byte code for it at all...
Damanjit Kaur
Ranch Hand
Joined: Oct 18, 2004
Posts: 346
posted
0
My guess then would be that it simply ignores the declaration and doesn't generate any byte code for it at all...
What can be problem if it generates the byte code for declaration.
Ok do you mean that compiler while generating byte code checks with the restrictions of java so it will simply ignore this declaration and won't generate byte code.
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
Originally posted by Damanjit Kaur: What can be problem if it generates the byte code for declaration.
As far as I know, declarations of local variables *never* generate any byte code. What it does is telling the compiler that it might need to reserve more room on the stack, and telling it the type of the variable, so that the correct byte code for the usage of the variable gets generated.