• 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 on Wrapper class methods.

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Wrapper classes are useful whenever it would be convinient to treat a primitive data as if it were an object. Wrapper classes provide methods to retrieve/extract the value from the wrapper object. they are the xxxValue() methods. where xxx can be byte, short, int, long, float or double.
I tried to run the following code to retrieve the long value with the shortValue() method and it worked. ! Can anyone explain please?
class getLongval{
public static void main(String args[]){
Long val = new Long(123l);
System.out.println("long value is : "+val.byteValue());
}
}
The output i got is :
long value is : 123
Thanks,
Sunitha. S
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It worked when you got the byteValue() of a long because the long that you used was very small(123). A byte can contain numbers that large. If you try a bigger number it will not work.
 
Sunitha Sounderrajan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i tried with a bigger number ..
i tried with 2232l as the value and i got a negative number and i was wondering why it did not give any errors?
when i gave 2232l as the value the result i got was -72 !!!
Sunitha. S
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sunitha,
The <code>byteValue()</code> method on <code>Long</code> simply casts the long value held in the object to byte. If you browse through the sourcecode for the class, the method appears as follows -

Clearly that explains the results. To help you understand, I modified your code to dumpt he byte value of a primitive long variable using plain casting.

Note that the similar results confirm byteValue() is simply downcasting the long quantity held in the Long object.
Hope that helps,
Ajith
[This message has been edited by Ajith Kallambella (edited October 04, 2000).]
 
Sunitha Sounderrajan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajith,
Thanks for your reply. I did try the code what u sent and it gives the same result as byteValue() fn.
Thanks again!
Sunitha. S

[This message has been edited by Sunitha Sounderrajan (edited October 04, 2000).]
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one explain me that how byte value -72 is coming from Long 2232.
Thanks
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason that the code returns -72 can be understood if you actually look at the binary representation of the initial number.
Decimal (base 10) 2232 represented in Binary (Base 2) is 100010111000. However, if I understand the method correctly, the byteValue() only takes the first 8 bits (from the right to left). This makes since because 8 bits make one byte. With this in mind, the only bits that the JVM looks at are 10111000. This binary number equals, you guessed it, -72. Let me know if there is any more explanation needed.
 
Santosh Jaiswal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Terry. I got it.
Santosh Jaiswal
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Sunitha! Hi!
I would like to request you to try following on wrapper class.
Long L = new Long(9);
Integer I = new Integer(9);
if(L.equals(I)) System.out.println("True");
else
System.out.prinln("False");

if(I.equals(L)) System.out.println("True");
else
System.out.prinln("False");

Both the time you get False.
if you try if(L.equals(new Long(9)))System.out.println("Ture"); Then the o/p is True.
Dharmesh.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'Dhamu.com',
PROPER NAMES ARE NOW REQUIRED!!
Read this post for details.
Ajith
 
Sunitha Sounderrajan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In the following code the if condition compares Long Object with Integer Object and Integer Object with Long object..so it returns false.
Long L = new Long(9);
Integer I = new Integer(9);
if(L.equals(I)) System.out.println("True");
else
System.out.prinln("False");

if(I.equals(L)) System.out.println("True");
else
System.out.prinln("False");

Both the time you get False.

-- In the following case the comparison is between 2 long Objects. So, the result is true. Please explain if my reasoning is not right!
if you try if(L.equals(new Long(9)))System.out.println("Ture"); Then the o/p is True.
Thanks,
Sunitha. S
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sunitha,
I believe it has to do with the comparison method for the wrapper classes. Someone correct me if I'm wrong, but I seem to recall that inside the equals() method for a Long, is a conditional statement similar to this:

Since only a Long object will pass the initial tests, anything else will return false.
Regards,
SP
[This message has been edited by Stephen Pride (edited October 05, 2000).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic