Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

type casting  RSS feed

 
Ramu Rondla
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends,
Is there any way to type cast primitive data types to class data types?
Thanks,
Ramu
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Java Language Specification, (�5.5 Casting Conversion): A value of a primitive type cannot be cast to a reference type by casting conversion, nor can a value of a reference type be cast to a primitive type.
What is it that you are trying to accomplish?
 
Herb Schildt
Author
Ranch Hand
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramu:
If you want to convert a primitive type into an object, you would normally use one of the type wrappers built into java.lang. These include
Integer
Double
Float
Byte
Short
Long
Char
Here is an example.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the coming Java 1.5 compilers will have a feature known as auto-boxing and auto-unboxing. Primitives won't be cast to objects, but in certain circumstances, the compiler will wrap a primitive up using the appropriate wrapper class, and unwrap wrapper class instances into the appropriate primitive values.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!