Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Strings

 
Kumaran Pillai
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try this out the code and got the answers which really confusing me much.
I will be thankful if someone explains me the concept behind this code and also explain the code. Thanks in advance for your help.

 
Nageswar Kakolla
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if ("String".replace('g', 'G') == "String".replace('g', 'G'))
System.out.println("Replace Equal");
else
System.out.println("Replace Not Equal");
Answer :Replace Not Equal because = is used to compare object and the objects pointed at and hence String object is different from another String object.

if (" String ".trim() == "String")
System.out.println(" trim Equal");
else
System.out.println("trim Not Equal");
Answer : trim Not Equal, trim() removes only trailing spaces and not leading spaces. Hence " String ".trim() gives " String" which is not equal to "String"
if (" String ".trim() == "String".trim())
System.out.println(" trim 2Equal");
else
System.out.println("trim 2 Not Equal");
Answer: trim 2 Not Equal, same explanation as above

if (" String ".startsWith(""))
System.out.println("True");
else
System.out.println("False");
Answer: This should give false
if ("String".toString() == "String")
System.out.println("tostring Equal");
else
System.out.println("tostring Not Equal");
Answer : String is equals to String and hence
if ("String".replace('t', 't') == "String")
System.out.println("replace Equal");
else
System.out.println("replace Not Equal");
Answer: "String".replace('t', 't') gives String which is equals to String

if ("String".substring(0, 6) == "String")
System.out.println("substring Equal");
else
System.out.println("substring Not Equal");
Answer : "String".substring(0, 6) gives String
 
Nageswar Kakolla
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if ("String".replace('g', 'G') == "String".replace('g', 'G'))
System.out.println("Replace Equal");
else
System.out.println("Replace Not Equal");
Answer :Replace Not Equal because = is used to compare object and the objects pointed at and hence String object is different from another String object.

if (" String ".trim() == "String")
System.out.println(" trim Equal");
else
System.out.println("trim Not Equal");
Answer : trim Not Equal, trim() removes only trailing spaces and not leading spaces. Hence " String ".trim() gives " String" which is not equal to "String"
if (" String ".trim() == "String".trim())
System.out.println(" trim 2Equal");
else
System.out.println("trim 2 Not Equal");
Answer: trim 2 Not Equal, same explanation as above

if (" String ".startsWith(""))
System.out.println("True");
else
System.out.println("False");
Answer: This should give false
if ("String".toString() == "String")
System.out.println("tostring Equal");
else
System.out.println("tostring Not Equal");
Answer : String is equals to String and hence
if ("String".replace('t', 't') == "String")
System.out.println("replace Equal");
else
System.out.println("replace Not Equal");
Answer: "String".replace('t', 't') gives String which is equals to String

if ("String".substring(0, 6) == "String")
System.out.println("substring Equal");
else
System.out.println("substring Not Equal");
Answer : "String".substring(0, 6) gives String
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nageswar Kakolla:
if ("String".replace('g', 'G') == "String".replace('g', 'G'))
System.out.println("Replace Equal");
else
System.out.println("Replace Not Equal");
Answer :Replace Not Equal because = is used to compare object and the objects pointed at and hence String object is different from another String object.
Correct. == compares whether the two objects are the same, and not whether the contents are the same.
if (" String ".trim() == "String")
System.out.println(" trim Equal");
else
System.out.println("trim Not Equal");
Answer : trim Not Equal, trim() removes only trailing spaces and not leading spaces. Hence " String ".trim() gives " String" which is not equal to "String"

if (" String ".trim() == "String".trim())
System.out.println(" trim 2Equal");
else
System.out.println("trim 2 Not Equal");
Answer: trim 2 Not Equal, same explanation as above
Not correct. trim() removes leading and trailing spaces. But since it returns a new String object, the two objects are not equal.
if (" String ".startsWith(""))
System.out.println("True");
else
System.out.println("False");
Answer: This should give false
Not correct. startsWith() with an empty string returns always true.
if ("String".toString() == "String")
System.out.println("tostring Equal");
else
System.out.println("tostring Not Equal");
Answer : String is equals to String and hence
String.toString doesn't create a new string object. As result, this statement is equivalent to: if ("String" == "String")
if ("String".replace('t', 't') == "String")
System.out.println("replace Equal");
else
System.out.println("replace Not Equal");
Answer: "String".replace('t', 't') gives String which is equals to String
replace detects that there is nothing to do and doesn't create a new string object. As result, this statement is equivalent to: if ("String" == "String")
Note: This behaviour isn't mentioned in the API so I wouldn't count on it.
if ("String".substring(0, 6) == "String")
System.out.println("substring Equal");
else
System.out.println("substring Not Equal");
Answer : "String".substring(0, 6) gives String
substring detects that you want the complete string and doesn't create a new string object. As result, this statement is equivalent to: if ("String" == "String")
Note: This behaviour isn't mentioned in the API so I wouldn't count on it.

The article Strings, Literally is helpful for this type of questions.
 
Kumaran Pillai
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Klug,

Thanks for your explanation. I could now understand the concept well.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answers are:



The reasons are as follows:

if test 1:



NOT EQUAL since both the resultant newly created objects are not interned by default.

Means the intern() method is not called by default which ensures that the string object is referenced from the pool and if any one exists already, the same reference is returned and NOT the new one.

if test 2:



NOT EQUAL since the trim returns a new object which is NOT interned by default though the returned string contains the same characters "String" which was already present, a brand new String object is created and returned

if test 3:



Here also same as above. But the RHS does NOT return a new String object as the trim operation is not successful but the LHS is successful thereby a brand new object

if test 4:



True as per the API. It returns true if the string starts with the set of characters mentioned in the argument

It returns true even if the argument is an empty string or equals to this (same) string object as determined by equals() method

if test 5:



toString does NOT return a new string, as per the API. so same string. so true! so Equals!

if test 6:



No successful replacement. so NO new string but same string. so equal!

if test 7:



I too agree with Manfred Klug here, though in the API it says that substring() returns a new String that is a substring of this string.

In this case, we can take that it always returns a new string. But as he said, it looks like it returns true only when IT IS ACTUALLY a SUBSTRING and NOT the SAME STRING CONTENT.

Thats why it seems like returning the same String Object.

Can any ranchers clear this please?

[ July 06, 2007: Message edited by: Raghavan Muthu ]
[ July 06, 2007: Message edited by: Raghavan Muthu ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic