aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String addition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String addition" Watch "String addition" New topic
Author

String addition

mi te
Greenhorn

Joined: Mar 09, 2006
Posts: 29
What is happenning when you add two string?

code:

public class StringAddition{

public static void main(String args[]){

String a = "a";
String b = "b";
String ab = "ab";

ab = a + b;
System.out.println(ab == "ab");
System.out.println(ab == ab);
System.out.println((a+b) == ab);
}
}

output:

false
true
false

I understand why the first and second output are as they are, but why the third one is false? What is the difference between the objects of ab and (a + b)? Please someone explain. I read about String on sites but I don`t still get it.
mambe nanje
Ranch Hand

Joined: Feb 22, 2006
Posts: 31
well (a+b) is adding the two stings objects and passing it in a constructor of a new string and such that a new string object is created which is not equal to the former, remember strings are immutable, but if they were in the pool it will be different


Da Clone in programming world
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String addition
 
Similar Threads
Question On overloading?
string constant pool
internal doubt abou string and string buffer
Difference bet "" and null