What is casting ?

What is casting ?

Casting is the process of assigning the value of one type of variable to another type of variable. 

Two types of casting are there in java:

  Implicit casting
  Explicit casting

We can directly assign the value of a variable to other variable if that variable can be easily fit by memory size into the other variable, this is called implicit casting.


 Example:

 int a=5; //a having 4 byte of memorybyte b=6; //b having 1 byte of memorya=b; 

//OK! byte b can be easily fit into an int a.b=a; //NO! int a can't be fit into the byte b.b=(byte)a;

 //OK! Explicit casting.The above statement telling the compiler that, I know there may be some loss of bits.


By:Jalees
Date:2012-01-09 00:00:00

Casting is the process of assigning the value of one type of variable to another type of variable. 

Two types of casting are there in java: *Implicit casting *Explicit castingWe can directly assign the value of a variable to other variable if that variable can be easily fit by memory size into the other variable, this is called implicit casting.

 Example:

 int a=5; //a having 4 byte of memorybyte b=6; //b having 1 byte of memorya=b; 

//OK! byte b can be easily fit into an int a.b=a; //NO! int a can\'t be fit into the byte b.b=(byte)a;

 //OK! Explicit casting.The above statement telling the compiler that, I know there may be some loss of bits.


By:Sumit Tripathi
Date:2019-05-15 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related Java Subjective Links

Java Subjective interview questions and answers for experienced and fresher