Two Laptop Bag
The moose likes Beginning Java and the fly likes Regarding type casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regarding type casting" Watch "Regarding type casting" New topic

Regarding type casting

Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 186

Dear friends,

Please help me in this type casting problem:

I am using JDK 1.4.
I want to convert a long(primitive data type) type cast it and assign it to a Long(Wrapper class) variable.

Kindly provide your help regarding this pronlem.
Thanks in advance.

Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

Use Long.valueOf(long)

SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 186

Thanks a lot Freddy
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Note that this is not type-casting. You are constructing a new object.

Type casting is for object references or primitives.

For object references, you type cast when you have declared a reference as a superclass, but you know that the object it points to is actually of some subclass.

For primitives, you type cast when you want to force the primitive value into a variable whose type is not automatically compatible. For instance, you can assign an int value to a long, with no cast. But if you want to assign a long to an int, you need to cast. If the long does not fit in the int, information will be lost.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
I agree. Here's the link:
subject: Regarding type casting
It's not a secret anymore!