• 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

There are Strings attached everywhere!

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone!
Please have a look at the code below:
<code>
class testc
{
public static void main(String[] args)
{
System.out.println("String".toString()=="String");//1.true
System.out.println(" String ".trim()=="String");//2.false
System.out.println("String".trim()=="String".trim());//3.true
System.out.println("STRING".toUpperCase()=="STRING");//4.true
System.out.println("STRING".toUpperCase()=="STRING".toUpperCase());//5.true
System.out.println("String".substring(0)=="String");//6.true
System.out.println("String".substring(0,6)=="String");//7.true
System.out.println("String".replace('t','t')=="String");//8.true
System.out.println("String".replace('g','G')=="String");//9.false
</code>
My question is:
Why is 2 false?
The java API documentation clearly says: "Removes white space from both ends of this string." There is NO specification whether it is a NEW string. Hence answer should be TRUE.
Why are 6 and 7 true?
Again, quoting the API specifications: "substring(int beginIndex)
Returns a new string that is a substring of this string.
i.e. Answer should definitely be FALSE.
Same goes for the other overloaded constructor of substring(int beingIndex, int endIndex).
Thanks a lot
Rahul
 
Rahul Ramachandran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No one?
Yours sincerely,
Rahul
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per the API,
"let k be the index of the first character in the string whose code is greater than '\u0020', and let m be the index of the last character in the string whose code is greater than '\u0020'. A NEW String object is created." ( Refer for details )
If there are no whitespaces to be removed then, the same string
will be returned
So in the case //2 of the examples given, after trimming the whitespaces
from front and end, a new String object will be created.
Check the below code,
//1
if(" String ".trim() == ("String") )
System.out.println("Equal trim(with space)");
else
System.out.println("Not Equal trim(with space)");
o/p : Not Equal trim(with space)

//2
if("String".trim() == ("String") )
System.out.println("Equal trim(with no spaces)");
else
System.out.println("Not Equal trim(with spaces)");
o/p:Equal trim(with no spaces)
 
reply
    Bookmark Topic Watch Topic
  • New Topic