• 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

== question

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would someone please tell me why this would print "False". Thank you.
--Tom
==========
1: Byte b1 = new Byte("127");
2:
3: if(b1.toString() == b1.toString())
4: System.out.println("True");
5: else
6: System.out.println("False");
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Theere are several of these questions answered earlier.Check earlier postings..
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Herculis
Once you have converted byte to a String, all the methods of String are applicable. The "==" operator checks the memory to which it is referencing. Since the addresses are different , you get a false as the answer. However if you wish to match the contents of the two operands, then do a ".equals()" instead
Rahil
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Whenever you call toString() method on any object,the toString() method creates new String by using new operator.If any string created by 'new' opearator will not be in the String pool (that usually happens if u create String without new keyword),hence == operator is not refering to the same memory address.Thats why the the answer is false.
HTH
Prasad
 
herkulis nugent
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Prasad. So how can one find out if a string already exists in the string pool? Also, does it follow that that toString() always create new String object even if there's one in the pool?
--Tom
=========

Originally posted by Prasad Ballari:
Hi,
Whenever you call toString() method on any object,the toString() method creates new String by using [b]new
operator.If any string created by 'new' opearator will not be in the String pool (that usually happens if u create String without new keyword),hence == operator is not refering to the same memory address.Thats why the the answer is false.
HTH
Prasad[/B]


 
Prasad Ballari
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
herkulis/Tom,
We need not worry about whether specified string exists in the String pool or not ;as the compiler will do it for you.As the String objects are immutable in nature; the compiler tries to optimise and save some memory by refering it from pool as it knows the String object value is not going to change (because of its immutable nature).
NB: You can put a String in to the pool by calling intern() method
HTH
-Prasad Ballari

------------------
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<html><body>
Here is a modification to your program so that it prints TRUE. Using intern() method:
<code>
class sringbyte{
public static void main(String ard[]){
Byte b1 = new Byte("127");
if(b1.toString().intern() == b1.toString().intern())
System.out.println("True");
else
System.out.println("False");
}
}</code>
The string false was printed because each time when you use the toString() method for object you create it creates a new string in the pool. So, to cover it up you can use the intern() method.</body></html>
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic