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

String

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

public class Test {
public static void main ( String args [ ] ) {
if("String".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");

}
}

The output of this code is Equal but when I change the code to

public class Test {
public static void main ( String args [ ] ) {
if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");

}
}

Why is this code giving an output of Not Equal . ( In the second code I'm giving spaces in the " String " ) .

Thanks.
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The 2nd code is creating a new object.
 
Puja S
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is another code where I'm having doubt :

if("String".trim() == "String".trim())
System.out.println("Equal");
else
System.out.println("Not Equal");


The output of this code is Equal and when the code is changed to
" String ".trim( ) == " String ".trim( ).......the output is Not Equal.


Why ?
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi puja,
The new string objects will be returned only if there is anything to trim. If u give without any spaces at the ends, the same string will be returned, and therefore the strings will be equal. But if there are anything to trim a different string will be returned and they will become different objects. To understand this u must be aware of the string pool also.
-vipin
 
Puja S
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Pradeep and Vipin........I got your point.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in other words: NEVER use == to compare Strings unless you explicitly intend to check whether two reference variables refer to the same memory location rather then whether they refer to memory with the same content.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic