Type Casting
- Type casting is used to convert one data type value to another data type value such as int to double, double to int conversion etc.
- But primitive data type values are not recommended to convert one type to other type instead wrapper classes must be used.
- Type casting is used more oftenly to convert one type of object to another type of object
Conversions as some examples are:
byte b0=127b; // wide casting; assigning lower value to higher value
short s0=b0;
int i0=b0;
long l0=b0;
float f0=b0;
double d0=b0;
Here in the above example we have converted byte to rest of all other data types like int, float, double etc...
int i1=10;
byte b1=(byte)i1; // narrow casting needs typecasting
short s1=(short)i1; // narrow casting needs typecasting
long l1=i1; // wide casting don't need typecasting
float f1=i1; // yes
double d1=i1; // yes
Here in the above example we have converted the data types int into other data types. And we have done narrow casting to data type short and byte because of the allocation of memory.
long l2=12345567890L;
byte b2=(byte)l2;
short s2=(short)l2;
int i2=(int)l2;
float f2=(float)l2;
double d2=l2;
Same as above example we have converted long data type into rest all other data types.
double d3=10.24;
byte b3=(byte)d3;
short s3=(short)d3;
int i3=(int)d3;
long l3=(long)d3;
float f3=(float)d3;
Same as above example we have converted long data type into rest all other data types.
Another method of converting one data type to other data types is by using wrapper classes. Now let us see how to convert it using wrapper classes.
Wrapper class:
- The recommended way to convert one primitive type to another primitive type is in java.lang package wrapper classes are provided for each primitive data type names Byte, Short, Integer, Long, Float, Double, Character and Boolean.
- Byte, Short, Integer, Long, Float and Double classes are derived from java.lang.Number class. Number class is derived from Object and implemented from Serializable and Comparable interfaces.
Why Number class is super class to all these classes, is methods which are commonly required from Byte to Double class are all implemented and given in Number class.
The methods are byteValue(), shortValue(), intValue(), longValue(), floatValue(), doubleValue()
To convert double - int, first of all we need to wrap double value into Double class object through its constructor.
double dd=10.2345;
Double ddd=new Double(dd);
and then call intValue() on Double class object.
int j=ddd.intValue();
Every wrapper class is having one public String parameter constructor and one same datatype value as parameter in its constructor.
Wrapper classes mainly serves two purposes:
i) Convert one primitive type to other primitive type. For this we can use both type casting and wrapper classes.
ii) Convert String to int/long/double and vice-versa because user input is always in a form of String. For this wrapper classes is compulsory.
All wrapper classes contains two constant variables called MIN_VALUE and MAX_VALUE.
Every wrapper class contains two constructors one will take same type primitve value and another will take String as a argument.
Primitive - byte short int long float double char boolean
Wrapper classes - Byte Short Integer Long Float Double Character Boolean
Byte(byte b)
Byte(String str)
Short(short s)
Short(String str)
Integer(int i)
Integer(String str)
All wrapper classes are having valueOf(String str) static method that will take String as a argument.
All wrapper classes are having parse method to convert String to the same datatype.
For example to convert int to other primitive types:
// IntToOthersConversion.java
class IntToOthersConversion {
public static void main(String rags[]) {
int i1=10;
// int wrapped into Integer
Integer in=new Integer(i1);
// converted into different types
byte b1=in.byteValue();
System.out.println(b1);
short s1=in.shortValue();
System.out.println(s1);
long l1=in.longValue();
System.out.println(l1);
float f1=in.floatValue();
System.out.println(f1);
double d1=in.doubleValue();
System.out.println(d1);
System.out.println(b1);
}
}
Note: While passing String into parseInt() method if the String is not a valid String Number Format Exception comes.
For more Information or click the button Below
Call: 9848111288
0 Comments