• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Prince Sewani
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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
=======================================
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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:

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
 
Prince Sewani
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hey Thanks Jesper..
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!