Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

String Methods

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone whats going behind the scenes for the followig code snippets
Q1)
******************************************
if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
Answer is "Not Equal"
******************************************
Q2)
******************************************
if("String".substring(0,6) == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
Answer is "Equal"
******************************************
Q3)
******************************************
if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");
Answer is "Not Equal"
******************************************
Q4)
**********************************************
if("String".replace('t','t') == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
Answer is "Equal"
********************************************
Thanks
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are immutable. That means that they can never be changed. Because of this, most of the methods of the String class behave a certain way - if the result of the method is a different String than the original, they create a brand new String object and return it. If the result is the same, however, rather than creating a useless object, the method simply returns the original String that was passed as an argument back.
I hope that helps,
Corey
 
rahul V kumar
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. That was really helpful.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s1 = "ABC";
String s2 = new String (s1);
String s3 = new String (s1);
if (s2 == s3) // return false why ??
s2.trim() == s2.toUpperCase() // true why ??
can u explain using below diagram ?
s2 -> s1 -> "ABC"
s3 ->
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read what Corey said earlier
s1 is created on constant string constant pool
s2 and s3 are created on the heap
s1 -> "ABC"
s2 -> "ABC"
s3 -> "ABC"
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic