• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Difference Between String s = new String("Computer") and String s = "Computer" ???

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a question :-

When i put

String s = new String("Computer");

if(s=="Computer"){
System.out.println("Equals A");
}

if(s.equals("Computer")){
System.out.println("Equals B");
}


into the main method the output is : "Equals B"

and when i declare s as :-

String s = "Computer";

that prints both i.e.
Equals A
Equals B

can someone throw some light at it??
=======================================
I'm just beginning to the fascinating world of Java
=======================================
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's something called String pool- So when you do

"Computer" is created in the string pool and "s" refers to that. So next time when you try to use "Computer" again- It will not create a new one but instead use the same available in the String pool.

When you use

This creates a String object on the heap. So:

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please note that this is a frequently asked question - if you do a search in the forums here, you will find lots of answers to the same question.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its always ideal to search in the forum first. And Agree with Jesper that this is the most commonly asked question.

To the JavaRanch Team: Why not have a FAQ on this? Or is there one already?
 
Prince Sewani
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohamed sanaullah wrote:Its always ideal to search in the forum first. And Agree with Jesper that this is the most commonly asked question.

To the JavaRanch Team: Why not have a FAQ on this? Or is there one already?



Hey i searched but bingo..i didn't find it jesper and mohamed,

i didn't get it, do you mean to say when i say:-

String s = "Computer";

and if(s=="Computer")

its going to match s(address) with the string "Computer"???

then why does it just print "Equals B" as when i do :-

String s = new String("Computer");

if(s=="Computer"){
System.out.println("Equals A");
}

if(s.equals("Computer"))
{
System.out.println("Equals B");
}

the output comes "Equals B",
just can't get the difference that the way of declaration is making??






 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, let me get it clear this time.

equals() - It has been overridden in String class to actually compare the contents of the String object. So if you have

So you can see that equals method is comparing the contents of the String and not just which objects they are referring to. Suppose you have this:

It is because- These two references point to different instances. And "==" just checks if the value with in the variable "c" or "c2" is same. And the value within the variable- "c" or "c2" would be the address for the location of the respective objects they are referring to.

Lets take a look at another example:

The reason for this behavior is explained just above- Both c and c1 refer to different object (though their contents are same). To check for the equality of the contents- You use equals().
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prince Sewani wrote:Hey i searched but bingo..i didn't find it jesper ...


Ok, I did a search for "difference new string" and found for example these:

Difference between String s
New String
What is the difference in String a = "abc" and String a = new String("abc")
difference between two string object
String creation
Any one explain the following one
Creating new Strings
strings and string pool
Difference between String s = "Marcus"; vs String s2 = new String("Marcus");
String!!!

Also see Strings, Literally from the JavaRanch Journal.
 
Prince Sewani
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:

Prince Sewani wrote:Hey i searched but bingo..i didn't find it jesper ...


Ok, I did a search for "difference new string" and found for example these:

Difference between String s
New String
What is the difference in String a = "abc" and String a = new String("abc")
difference between two string object
String creation
Any one explain the following one
Creating new Strings
strings and string pool
Difference between String s = "Marcus"; vs String s2 = new String("Marcus");
String!!!

Also see Strings, Literally from the JavaRanch Journal.



Hey Thanks Jesper..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic