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