Wednesday, 9 May 2018

Java String manipulation query using trim() and length()

public class X {

public static void main(String[] args) {


String s1=" abc xyz ";
System.out.println(s1.length());
s1.trim();
System.out.println(s1.length());
System.out.println(s1.trim().length());
}
}


O/p:




9
9
7


Kindly explain to me why s1.trim().length() is 7 and not 9 as s1 will still be pointing to the old string ie " abc xyz " ?

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...