• 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

String declaration and initialization

 
Greenhorn
Posts: 2
MS IE Opera Objective C
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys I am planning to give OCPJP exam. I ve been preparing for last one month and am new to this site as well.I want to know the difference between :

String s1 ="XYZ"; // and
String s2= new String("XYZ");

what i know is "new" is used to call the constructor to create the object and initiallise the instance variables, so what happens in the first case are we not creating object?Or are they both same.Please clear the doubt.
 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

dilip IneverGiveUp wrote:Hi Guys I am planning to give OCPJP exam. I ve been preparing for last one month and am new to this site as well.I want to know the difference between :

String s1 ="XYZ"; // and
String s2= new String("XYZ");

what i know is "new" is used to call the constructor to create the object and initiallise the instance variables, so what happens in the first case are we not creating object?Or are they both same.Please clear the doubt.



There is no difference,both will create string object s1 and s2, and you need to change your name as per forum rules.
 
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

There is no difference,both will create string object s1 and s2.....



Creating String without the use of new operator, the jvm will fetch the reference from the string memory pool (not sure what this is called), but if we make a object using new operator, the jvm will be forced to create a new object which will be placed in the heap memory.
 
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So both s1 and s2 refer to different objects. Read more about String pooling ScjpTipLine-StringsLiterally. As Saloni suggested please adhere to the naming policy as given here
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sumit Patil wrote:

There is no difference,both will create string object s1 and s2.....



Creating String without the use of new operator, the jvm will fetch the reference from the string memory pool (not sure what this is called), but if we make a object using new operator, the jvm will be forced to create a new object which will be placed in the heap memory.



Yes for string literal "XYZ" s1 object will be created on string literal pool and it is also a part of heap and for second new String("XYZ") s2 object will be created on heap but ultimately objects will be created
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:

Yes for string literal "XYZ" s1 object will be created on string literal pool and it is also a part of heap and for second new String("XYZ") s2 object will be created on heap but ultimately objects will be created



I agree both the objects would be created, but these 2 objects won't be the same, i.e s1 == s2 would be false
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sumit Patil wrote:

saloni jhanwar wrote:

Yes for string literal "XYZ" s1 object will be created on string literal pool and it is also a part of heap and for second new String("XYZ") s2 object will be created on heap but ultimately objects will be created



I agree both the objects would be created, but these 2 objects won't be the same, i.e s1 == s2 would be false



Yes, They have difference address then how they will be same ?
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:
Yes, They have difference address then how they will be same ?





Now,s1 == s2 will be true, while s1 == s3 will be false.
s1 = "XYZ"
May or may not create a new String object.
If a String object with the literal "XYZ" already exists the reference 's1' will only point to it.
Since String objects are immutable.

Where as,
String s3 = new String("XYZ");
will always create a new Sting object.

More details here
 
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s1.equals(s2) will return true but s1==s2 will return false

By Saying s1.equals(s2) , you are comparing that the Objects are meaningfully equal

By Saying s1==s2 , you are comparing the Objects with respect to the refernce variables that is the address where the values are stored and which will return false, because the addresses are different
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sumit Patil wrote:

saloni jhanwar wrote:
Yes, They have difference address then how they will be same ?





Now,s1 == s2 will be true, while s1 == s3 will be false.
s1 = "XYZ"
May or may not create a new String object.
If a String object with the literal "XYZ" already exists the reference 's1' will only point to it.
Since String objects are immutable.

Where as,
String s3 = new String("XYZ");
will always create a new Sting object.

More details here



I was talking about given scenario.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deviating from the topic There is a method intern() that searches the String pool for already created String before creating a new one. Discussed well in StringsLiterally

Sample Program for intern:
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jai wrote:Deviating from the topic There is a method intern() that searches the String pool for already created String before creating a new one. Discussed well in StringsLiterally

Sample Program for intern:



nice.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys, Thanks a lottt .I really got a lot of help..This was the first question I asked and i am really thanks ful to all of you.And yes I tried changing my name a lot but could not do so.So I made a new account with my actual name.Also the links provided by some people are really help full.I had this doubt from day one,but never had people to help me out, and now i think i do.Thanks!!!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic