Type Casting refers to exchanging one type (roughly speaking, an object structure)
Example:
public class JavaCast {
public static void main(String... args) {
Example:
public class JavaCast {
public static void main(String... args) {
Integer integer = new Integer(10);
Float floatt = new Float(20F);
Float floatt = new Float(20F);
//this is not a cast - error
// integer = floatt; //compiler error - incompatible types
// integer = (Integer) floatt;//compiler error - inconvertible types
// integer = floatt; //compiler error - incompatible types
// integer = (Integer) floatt;//compiler error - inconvertible types
//upcast - widening conversion
Object obj = integer; //no explicit cast required
System.out.println(obj);
Object obj = integer; //no explicit cast required
System.out.println(obj);
//downcast - narrowing conversion
Integer in = (Integer)obj;//only subtype
System.out.println(in);
Integer in = (Integer)obj;//only subtype
System.out.println(in);
//downcast - Object to String
//runtime issue - instance Object is not of String
String str = (String)obj;//ClassCastException
}
}
Type Conversion refers to translating of values (roughly speaking, contents of the object)
Types & Examples:
//runtime issue - instance Object is not of String
String str = (String)obj;//ClassCastException
}
}
Type Conversion refers to translating of values (roughly speaking, contents of the object)
Types & Examples:
Identity conversions
Widening primitive conversions
Narrowing primitive conversions
Widening reference conversions
Narrowing reference conversions
Boxing conversions
Unboxing conversions
Unchecked conversions
Capture conversions
String conversions
Value set conversions
Widening primitive conversions
Narrowing primitive conversions
Widening reference conversions
Narrowing reference conversions
Boxing conversions
Unboxing conversions
Unchecked conversions
Capture conversions
String conversions
Value set conversions
Identity Conversion
This is given for theoretical completeness. Assigning two instance of same type is identity conversion.
Integer i1;
Integer i2 = new Integer(2);
Integer i2 = new Integer(2);
i1 = i2; //identity conversion
// cast not required, but done compiler will not complain
i1 = (Integer) i2;
Primitive Conversions and Cast in Java
// cast not required, but done compiler will not complain
i1 = (Integer) i2;
Primitive Conversions and Cast in Java
These are the conversions between the primitives.
Widening Primitive Conversion
“A widening primitive conversion does not lose information about the overall magnitude of a numeric value.” There is no cast required and will never result in a runtime exception. Following are the possible widening conversions,
byte to short, int, long, float, or double
short to int, long, float, or double
char to int, long, float, or double
int to long, float, or double
long to float or double
float to double
class WideningConversion {
public static void main(String[] args) {
int i = 123456789;
float f = i;
}
}
Narrowing Primitive Conversion
short to int, long, float, or double
char to int, long, float, or double
int to long, float, or double
long to float or double
float to double
class WideningConversion {
public static void main(String[] args) {
int i = 123456789;
float f = i;
}
}
Narrowing Primitive Conversion
“A narrowing primitive conversion may lose information about the overall magnitude of a numeric value and may also lose precision and range.” Cast required between types. Overflow and underflow may happen but a runtime exception will never happen. Following are the possible narrowing conversions,
short to byte or char
char to byte or short
int to byte, short, or char
long to byte, short, char, or int
float to byte, short, char, int, or long
double to byte, short, char, int, long, or float
package com.javapapers.java;
char to byte or short
int to byte, short, or char
long to byte, short, char, or int
float to byte, short, char, int, or long
double to byte, short, char, int, long, or float
package com.javapapers.java;
public class NarrowingPrimitiveConversion {
public static void main(String[] args) {
public static void main(String[] args) {
float f = Float.POSITIVE_INFINITY;
long l = (long) f;
int i = (int) f;
long l = (long) f;
int i = (int) f;
System.out.println("long: " + l + " int: " + i);
int j = 255;
byte b = (byte) j;
byte b = (byte) j;
// size is too large and resulted in negative
System.out.println(b);
}
System.out.println(b);
}
No comments:
Post a Comment