aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Basic String function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Basic String function" Watch "Basic String function" New topic
Author

Basic String function

parag bharambe
Ranch Hand

Joined: Sep 01, 2000
Posts: 41
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).]
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
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
Hemal Mehta
Ranch Hand

Joined: Nov 16, 2000
Posts: 101
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);
}

}
Edy
Greenhorn

Joined: Nov 16, 2000
Posts: 4
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"
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
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
Rajiv Ranjan
Ranch Hand

Joined: Sep 28, 2000
Posts: 61
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
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
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.
 
 
subject: Basic String function