File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes what is the difference?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is the difference??" Watch "what is the difference??" New topic
Author

what is the difference??

Karthikeyan Ravindran
Ranch Hand

Joined: Nov 20, 2007
Posts: 32
What is the difference between creating an instance and creating an object..


String s=apple;
dog db=new dog();


Thanks in advance

Karthikeyan
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

There is no difference. An object is an instance of a class. The words "instance" and "object" refer to the same thing.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Karthikeyan Ravindran
Ranch Hand

Joined: Nov 20, 2007
Posts: 32
thanks ... i have another question..

what is the diff between the following two lines..

String s1 = apple;
String s2 = new String("apple");

Thanks in Advance
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Unless you already have a String object called apple, the first line won't compile. You ought to have written "apple". If you already have a String called apple, what it means is, "I want to apply the name s1 to the String object I have already got which I have previously called apple". You now have two names for the same Object.

The first line means "I want a String and I want it to read apple," and the second line means "I want a String distinct from any other Strings and I want it to read apple". If you already have a String with the content apple, the JVM may use the same String again, just with a new name s1.
Note this behaviour occurs with Strings only. There is rather similar behaviour if you use the valueOf() method of "wrapper" classes like Integer.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Karthikeyan Ravindran:
...what is the diff between the following two lines...

See Strings, Literally.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
qingwu wang
Ranch Hand

Joined: Feb 19, 2003
Posts: 147
...="apple"; is created in the STACK
...=new String("apple"); is created in the HEAP


Thanks...qingwu<br />When I open my eyes,I see your pretty face.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by qingwu wang:
...="apple"; is created in the STACK
...=new String("apple"); is created in the HEAP

See the article I referenced above.
Objects are created on the heap and Strings are no exception. So, Strings that are part of the "String Literal Pool" still live on the heap, but they have references to them from the String Literal Pool.

...

When a class is loaded (note that loading happens prior to initialization), the JVM goes through the code for the class and looks for String literals. When it finds one, it checks to see if an equivalent String is already referenced from the heap. If not, it creates a String instance on the heap and stores a reference to that object in the constant table.
Karthikeyan Ravindran
Ranch Hand

Joined: Nov 20, 2007
Posts: 32
Thank you Campbell, Qingwu, Marc



Regards,

Karthikeyan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the difference??