• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

instance,object and reference

 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();

Thnks in advance.
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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".
 
Damanjit Kaur
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic