ControlC ControlC · Pastebin

java-tut-typecasting-primitives-Non-Floating-Point-Typecasting-During-Operation-1

Pasted: Nov 28, 2024, 8:40:04 am · Views: 37
public class SampleConversion{

public static void main(String[]args){
short s1 = 50;

//valid
int intOne = s1 + 100;

//error: possible lossy conversion
//from int to short 'cause the
//result is int
//short s2 = s1 + 100;

//invalid
//result is converted to int
//byte b1 = 25;
//short s1 = 50;
//byte b2 = b1 + s1;

//valid
//result is long since long
//has higher bits than int
long longOne = 100 + 100L;
//invalid
//int intOne = 100 + 100L;

//valid
int intTwo = 'c' + 5;

//if char and an integer literal
//are both operands in one operation
//and the result is gonna be stored
//in a char variable then, the result
//is gonna be char type

//valid
char character = 'c' + 5;

//invalid
//int intOne = 5;
//char character = 'c' + intOne;

}
}