wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wrapper class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wrapper class" Watch "wrapper class" New topic
Author

wrapper class

abhishek chauhan
Greenhorn

Joined: Jul 21, 2005
Posts: 8
in continuation to what i asked earlier

the answer is
true
false
true
false

if i am giving values less than equal to 10 then == operator is giving answer as true .. but for the values greater than 10 things get otherwise
explain
[ July 27, 2005: Message edited by: Barry Gaunt ]
Karthikeyan Varadarajan
Ranch Hand

Joined: Jul 04, 2002
Posts: 98
Hi abhishek,

Your are correct. It will behave like that only.

i don't want to spoil your learning zeal. The hint is in the Integer.toString(int i,10) method. Just look in to this. You will get the answer.


~With Smile<br />VK
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please use tags if you want people to read your formatted code.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
Originally posted by Karthikeyan Varadarajan:
Hi abhishek,

Your are correct. It will behave like that only.

i don't want to spoil your learning zeal. The hint is in the Integer.toString(int i,10) method. Just look in to this. You will get the answer.



not much understood... explain in easy words pls


Thanks and Regards, Amit Taneja
Karthikeyan Varadarajan
Ranch Hand

Joined: Jul 04, 2002
Posts: 98
The toString method of Integer finally calls Integer's toString(i) method.

This is the code snippet from that method.




If the value is less than or equeal to 10 , then it will return the same string from string pool.

If the value is greater than 10 , it will create new string.

Hope it helps and also hope this kind of question won't be in the exam
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
So, author didn't see my answers in his first topic. Too bad.

Why to ask twice the same question?

The point is: This kind of behavior of Integer.toString() method is not guaranteed. I believe that developer of Integer class had his (her ???) own reasons for choosing the range of constants for switch cases.

I can add to the answer: JVM 1.5 has different realization of this method. And your == return true because method return strings from string constants pool (string literals "-3","-2", etc.)

You can read about string pool in other topic of this forum. Check this code up:

[ July 28, 2005: Message edited by: George Bolyuba ]

SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
Debashree Halder
Ranch Hand

Joined: Jul 27, 2005
Posts: 81
Hi,

If you use a.toString().equals( b.toString()) instead of == operator then it'll return true in each case.....== operator checked the equility of two String 'objects' NOT the VALUE of it!So the result is not guranteed..... if you want to compare the value of two strings, use .equals method.

pls correct me if I'm wrong.

Debashree


Debashree Halder<br />SCJP 1.5<br />Washington DC
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
Originally posted by Debashree Halder:
Hi,

If you use a.toString().equals( b.toString()) instead of == operator then it'll return true in each case.....== operator checked the equility of two String 'objects' NOT the VALUE of it!So the result is not guranteed..... if you want to compare the value of two strings, use .equals method.

pls correct me if I'm wrong.

Debashree


You ara right.

There is a better way to compare to Integer objects.
-> a.equals(b);

All we need to know now is what the author really wanted to do.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wrapper class
 
Similar Threads
Byte
When String gets Equal
character and strings
wrapper classes
equals versus ==