What is Type-Casting in Java | Java Learning | E-Learn Platform @StudyEcart

Yogi Siddeswara 0

 TypeCasting is actually the process of converting one data type value into compatible datatype values. here we try to assign values into byte b, but it is not possible directly because we try to place 4 bytes of space value into one-byte space, technically it's not possible, but by using explicit type casting we can do cast here.

Like,


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package elearn.studyecart.basics;

public class StudyEcartLab {

	public static void main(String[] args) {
		int a=10;
		// here we try to assign a values into byte b ,it not possible directly because int -4byte,byte -1byte 
		//byte b=a;// here we have to do explicit type-casting 
		byte b=(byte)a;
		
	   byte b1=120;
	   
	   // implicit type casting
	   int a1=b1;// here its possible ,because  4 byte storage can hold 1 byte of data
		

	}

}

Post a Comment

0 Comments