The Java Platform Java Runtime Environment Data Types![]() Objectives![]() Data Types![]() Variable Types![]() Variable Default value![]() Declaration and Initialization![]() Casting![]() Operators![]() OOP Central Concepts Control Statemets Methods Arrays Classes and Objects Inheritance Constructor Interface Packages & Access Modifiers Java Collections Framework | CastingJava allows implicit casting to larger data types, but casting to smaller data types must be done explicitly. Java allows implicit casting to larger data types, but casting to smaller data types must be done explicitly. byte b = 50;
short s = b; //Valid because short is 16 bits
//easily storing an 8-bit byte.
float f = 10.0F;
double d = f; //A 64-bit double has no problem
//storing a 32-bit float.The following would fail because explicit casting is required: int i = 100;
Short s = i; //Fails!
Short s =(short) i; //The explicit cast is needed.
|