• 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

Byte

 
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have code :
class A
{
public static void main(String[] args)
{
Byte a = new Byte("127");
if (a.toString() == a.toString())
System.out.println("True");
else
System.out.println("False");
}
}
Why the answer is "False" ?
thanks
daniel
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, the reason is, every time the .toString() method return a new String object. Two new String objects comparison will return false even they contain same text inside.
Hope it helps.

------------------
Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
try my mock exam¹²³ at my homepage.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But:

returns True. So why doesn't the first example?
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
WE WILL ANALYSE YOUR CODE
--------------------
class Str{
public static void main(String[] args){ String a = "hello";// THIS IS A STRING LITERAL
String b = "hello";// AND ALSO THIS
// STRING LITERALS ARE NOT OBJECTS
// THEY ARE CREATED IN STRING POOL
// HERE IN String a,b YOU R REFERING TO THE SAME LITERAL
// HENCE NO FREASH STRING IS CRETED FOR YOUR SECOND STATEMENT
// THAT IS String b="hello" WILL BE THE SME LITERAL AS GENERATED
// USING String a="hello" UT VARIABLE b ALSO POINTS TO IT
// THEY ARE NOT FOR GARBAGE COLLECTION
// NOW DOING == OPERATION REFERS TO THE SAME STRING IN STRING POOL
// AND HENCE
if (a == b){ System.out.println("True");// CORRECT }
else { System.out.println("False"); }
}}
HOPE THIS HELPS
tvs sundaram
SCP for Java 2 Platform
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark,
the reason is the way java handles strings. When you create a string literal, java adds it to an internal table. Since strings are immutable, there is no need to keep multiple copies of the same data. So since you already created "hello", it is added to the table. "hello" is again looked up and found in the table when you add a reference to b. since a & b now point to the same reference to "hello", they are indeed equal. I believe that if you were to have said b = new String("hello"), then you would have received false because a new strinq object would have been created. There is a little used method on the String object called intern() which would add the value of the string object to the interal string table.
 
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic