• 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

Basic String function

 
parag bharambe
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
I think I'll just lie down here for a second. And ponder this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic