Forums Register Login

Basic String function

+Pie Number of slices to send: Send
hello
Read this piece of code carefully

if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
Answers
a)the code will compile an print "Equal".
b)the code will compile an print "Not Equal".
c)the code will cause a compiler error
This is from abhilash paper.
Why the answer is Not Equals. Although after trim() both strig will be same address.
2)
Read the code below. Will be the result of attempting to compile and run the code below.

public class AQuestion
{
public void method(Object o)
{
System.out.println("Object Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
Answers
1)The code does not compile.
2)The code compiles cleanly and shows "Object Version".
3)The code compiles cleanly and shows "String Version"
4)The code throws an Exception at Runtime.
Can some guru help?

[This message has been edited by parag bharambe (edited November 16, 2000).]
+Pie Number of slices to send: Send
The trim function will create a new String if the String is changed. So in your example, " String " is padded with spaces, so it does change and a new String is created. If you took out the spaces, then the trim function wouldn't do anything and a new String would not be created, so it would return EQUAL as they both look at the same string item in the pool.
Bill
+Pie Number of slices to send: Send
This is hmehta with new logid. Anyways It's simple, look at the esample below: Its a qesution of reference vs value of String. Ryun the program below and u will understand. In the first part u get not equals and in the second part u get equals.
import java.awt.*;
import java.util.*;
class stringCompare
{
private String String1;
private String String2;
stringCompare(String s)
{
String1=s;
String2=s;
}
void isStringRefEqual1(String String1)
{
System.out.println(" I am here");
if(String1.trim()=="NEW")
{
System.out.println("Equals");
}
else
{
System.out.println("Not Equals");
}
}

void isStringValueEqual2(String String2)
{
String bufferString;
boolean stringValue;
stringValue=String2.trim().equals("NEW");
if(stringValue)
{
System.out.println("Equals");
}
else
{
System.out.println("Not Equals");
}
}
}
public class testString
{
public static void main(String[] args)
{
String s="NEW ";
stringCompare sold=new stringCompare(s);
stringCompare snew=new stringCompare(s);
sold.isStringRefEqual1(s);
snew.isStringValueEqual2(s);
}

}
+Pie Number of slices to send: Send
Wait a minute, guys!
I compiled the first question and ran it. Guess what, the answer is "Equal". I also looked at the source code of java.lang.String.trim(), it should return a new string(if there is spaces before or after the string). At this time,
" String ".trim() will return new String("String"), everybody knows it will pick up the "String" object in the pool in stead of creating a new one. So the answer is "Equal"
+Pie Number of slices to send: Send
Edy, I compiled the code and ran it and got "Not Equals" so one of us is doing something different. But as you said, when you use the trim function, and there are spaces, it returns a NEW string thus creating a new literal in the pool. So if it is creating a new literal, it will not be equal to the literal that is already in the pool.
On a separate note, we have a naming policy here at the Ranch that we are trying to enforce and Edy does not meet those requirements. Check it out here: http://www.javaranch.com/name.jsp
Please re-register with an appropriate name.
Thanks,
Bill
+Pie Number of slices to send: Send
Hi Parag :
I think we discussed the same question( Question no.2 of your posting few days back.
Check this out.
http://www.javaranch.com/ubb/Forum24/HTML/005595.html
Thanks
+Pie Number of slices to send: Send
I would like to request Parag to read all previous posts (if possible) before posting a question. Both of these questions have been asked before.
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 707 times.
Similar Threads
Use of null in method call
Mock Exam Questions (null)
Doubt in overloaded method related question
Pass a Null as parameter?!
help help
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:19:02.