Type casting and Type conversion in java

Type casting and Type conversion in java.


The process of converting one primitive data type to another primitive data type is called type conversion or conversion.

Or
The process of converting value of one data type to another data type is called type casting or type conversion.


There are two types of type conversion/casting.


1.Implicit type casting

2.Explicit type casting


1.implicit or widening or automatic type conversion :


It is converted from lower data type to higher data type automatically by compiler.

Or
It is converted from small data type to large data type automatically by compiler.

Or
It is  an auto conversion performed by the compiler between two compatible data types using java's type promotion rules.


It  is performed by compiler itself.
It is also called type promotion.


Example :


int x=5;
float y;
y=x;
System.out.println("x="+x);
System.out.println("y="+y);


results :
x=5
y=5.0


2.Explicit or manual or narrowing type conversion :


It is converted from large data type to small data type using casting operator.

Or
It is forceful conversion performed by the user/programmer between two incompatible data type using a type casting operator.


It is performed by programmer to specific or desired data type.
It is also called type casting.


Example :


int x=0;
double y=5.3;
x=(int)y;

System.out.println("y="+y);
System.out.println("x="+x);


results:
y=5.3;
x=5;


What is type casting operator?

The type cast is a unary operator which forces the system to convert an experssion to the desired/target data type.


Syntax:


Var=(target data type)variable;

Or
Var=(cast-type)expression;



Example :
int a=2;
double b=3.5;
int res=(int)(a+b);

System.out.println("Result="+res);


results :

Result=5;


why is type casting is needed/required?

Or
What is need of type conversion?


i).user desires an expression to be evaluvated to specific data type.

ii).A compiler time error occurs due to incompatible data types in an assignment operation.


Write a java program to demonstrate type conversion and type casting.

Or 

Write a java program to illustrate use of casting  operator to perform  type casting in java.


program:
public class casting
{
public static void main(String args[])
{
int x=5;
float y;;
y=x; //implicit type conversion
System.out.println("implicit type conversion");
System.out.println("x="+x);
System.out.println("y="+y);

int a=2;
double b=3.5;
int res=(int)(a+b);
//Explicit type conversion
System.out.println("explicit type conversion");
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("result="+res);

System.out.println("or");
//or
int m=0;
double n=5.3;
m=(int)n; //explicit conversion
System.out.println("n="+n);
System.out.println("m="+m);
}
}


Output:
implicit type conversion
x=5
y=5.0
explicit type conversion
a=2
b=3.5
result=5
or
n=5.3
m=5

Comments

Popular posts from this blog

Control Statements:Selection statement ,Iteration statement and Jump statement in Java

Applets - Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet, passing parameters to applets, applet security issues.

Java Database Connectivity (JDBC) ,Steps to connect to the database in Java