aspose file tools
The moose likes Beginning Java and the fly likes Difference Between String s = new String( Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Difference Between String s = new String("Computer") and String s = "Computer" ???" Watch "Difference Between String s = new String("Computer") and String s = "Computer" ???" New topic
Author

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

Prince Sewani
Ranch Hand

Joined: Nov 24, 2010
Posts: 32
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
=======================================


Regards
Prince
---------------------------------------------------------
SCJP 6 , still a life long learner
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2925
    
  15

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:


Mohamed Sanaulla | My Blog
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2925
    
  15

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

Joined: Nov 24, 2010
Posts: 32
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

Joined: Sep 08, 2007
Posts: 2925
    
  15

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
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

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

Joined: Nov 24, 2010
Posts: 32


Hey Thanks Jesper..
 
I agree. Here's the link: http://zeroturnaround.com/jrebel
 
subject: Difference Between String s = new String("Computer") and String s = "Computer" ???
 
Similar Threads
String
excellent == vs equals question
StringBuffer and String doubt
equals doubt??
Wats the O/P...