• 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

wrapper class

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use tags if you want people to read your formatted code.
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Karthikeyan Varadarajan
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic