• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

String equality

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.io.*;
class StringTest {

public static void main(String args[]) throws IOException{
System.out.println("s12"==((" s12 ".trim())));
System.out.println( "String" == "String".replace('t','t') );


}
}

running the above code gives output as
false
true

How String pool used by the JVM works in the above two statements?
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
trim method internally uses substring() method which returns new string.

replace(char oldChar, char newChar)

implementation of this class returns same string if oldChar is equal to newChar.
 
Ramesh Sahu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ramesh
I got that
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ramesh Sahu:
import java.io.*;
class StringTest {

public static void main(String args[]) throws IOException{
System.out.println("s12"==((" s12 ".trim())));
System.out.println( "String" == "String".replace('t','t') );


}
}



You should always remember that
Besides String literals and compile time constant expressions
all String objects are created at run-time same as other objects.


When you say " s12 ".trim() then " s12 " object goes String pool
whereas string created on " s12 ".trim() invocation is created at
run-time hence distinct.

Hence "12"==((" s12 ".trim()))
returns false

When you invoke replace() , it returns same object reference if both
arguments are same.



Since both arguments are same hence replace method returns same reference
hence it returns true.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ramesh Sahu:
System.out.println("s12"==((" s12 ".trim())));
System.out.println( "String" == "String".replace('t','t') );



Hello,

1. "s12" and " s12 " is different---- so,false

2. "String".replace('t','t') is nothing but "String"---so,true

Hope This Helps
 
Where does a nanny get ground to air missles? Protect this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic