In this article,Typecasting in Java is explained in detail with examples.It is the process of converting one data type value into another datatype value.
In the java program, it is done in two ways.
- Implicit type casting.
- Explicit type casting.
Implicit typecasting.
This casting is done by Java compiler automatically during the compilation process. This casting is done mainly in two situations.
This casting is done by Java compiler automatically during the compilation process. This casting is done mainly in two situations.
Ex
1. In mathematical expressions, lower datatype values are automatically converted to higher datatype value.
lower datatype------higher datatype.
(a+b+c)--------(int+double+float)
---------(double+double+double)--(output is in the form of double datatype).
2. In assignment expressions, RHS value is Automatically converted to LHS datatype value, provided RHS is a lower datatype. (From topic Typecasting in Java)
2. In assignment expressions, RHS value is Automatically converted to LHS datatype value, provided RHS is a lower datatype. (From topic Typecasting in Java)
Explicit typecasting.
In situations,where implicit typecasting fails, the programmer has to perform explicit typecasting manually.It is done as follows.
Syntax.
(datatype) var-name/value;
Ex
double x=5.66;
(float) x;
Note
Whenever lower datatypes are converted to higher datatypes, there is no loss of information. However when bigger datatypes are getting converted to lower datatypes, there is a possible loss of information. (From topic Typecasting in Java)
Case I
double is getting converted to float. In this case, precision is lost after decimal digits.
Ex. 28.123456789256789-----28.123457.
Case II
Long is converted to int|short|byte.
Int is converted to short|byte.
short is converted to a byte.
In this case, Excess higher order bits are completely dropped, resulting in loss of information. (From topic Typecasting in Java)
Ex
Case III
Any floating to any integers
Ex
(double/float)----(Long/int/short/byte)
In this case, decimal part is completely truncated.
Ex
Rs.15.16/--> Rs.15-only. // during conversion from float to integer the decimal values are roundoff to near values.
Rs.1.05 Crores--->Rs.1 crore.
(1 crore 5 lakhs)--(1 crore only)
Case IV
Any integer to char (vice-versa)
(Long/int/short/byte)----char(vice-versa).
In these case,corresponding Unicode or its character is considered. (From topic Typecasting in Java)
Ex
int x;
x='A'; //unicode value of A=65.
char ch;
ch=97;//unicode value of 97=a.
Unicode
|
Range
|
A-Z
|
65-90
|
a-z
|
97-122
|
0-9
|
48-57
|
$
|
36
|
space
|
32
|
Ex
//program related to Typecasting in Java.
class Typecastingdemo
{
public static void main(String args[])
{
byte b;
short s;
int i;
long l;
float f;
double d;
b=10;
i=b;
System.out.println("Byte value: "+b);
System.out.println("Int value: "+i);
f=(float)10.56;
//f=10.56f;
System.out.println("float value: "+f);
d=f;
System.out.println("double value: "+d);
int sum;
sum=b+i;
System.out.println("Sum value: "+sum);
double total;
total=f+d;
System.out.println("Total value: "+total);
float add;
add=(float)(d+d); //Explicit typecasting.
System.out.println("Addition value is: "+add);
double dd=10.5665598624;
float ff;
System.out.println("Double dd: "+dd);
ff=(float)dd;
System.out.println("float ff: "+ff);
short ss=128;
byte bb;
System.out.println("short ss: "+ss);
bb=(byte)ss;
System.out.println("Byte bb: "+bb);
int ii=65;
char cc;
System.out.println("Int ii: "+ii);
cc=(char)ii; //Uni-code conversion
System.out.println("char cc: "+cc);
double ddd=10.5696879;
int iii;
System.out.println("Double ddd: "+add);
iii=(int)ddd;
System.out.println("Int iii: "+ii);
}
}
Output
Continue to next topic Control and Branching structures in Java.
Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing
Do check my new startup Surprise Planners in Hyderabad- Lavish Surprises our services are surprise party planners in Hyderabad, surprise gifts, surprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.
Hi Friends, Please comment down your views in the comment section below. Thank you...
Hi Friends, Please comment down your views in the comment section below. Thank you...
No comments:
Post a Comment