This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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");
Hemal Mehta
Ranch Hand
Joined: Nov 16, 2000
Posts: 101
posted
0
Theere are several of these questions answered earlier.Check earlier postings..
rahil qamar
Greenhorn
Joined: Nov 16, 2000
Posts: 8
posted
0
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
rahil
Prasad Ballari
Ranch Hand
Joined: Sep 23, 2000
Posts: 149
posted
0
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
Joined: Nov 22, 2000
Posts: 23
posted
0
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
Joined: Sep 23, 2000
Posts: 149
posted
0
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
------------------
asim wagan
Ranch Hand
Joined: Nov 14, 2000
Posts: 62
posted
0
<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>
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.