Today class:
Hiee everyone :)today prithvi sir did not come to class,so vijay sir entered the class and he remaining the topic about past lessons and he asked question and he explain it
And he tell about, how to evaluate my self in class lessons he say search compiler topic in chatgpt to asked question for me and to evaluate myself rate 10/5 are 10/? and daily revise the past lessons
*Today i learn:
*
*Java Type Casting
*
https://www.w3schools.com/java/java_type_casting.asp
Type casting is when you assign a value of one primitive data type to another type.
In Java, there are two types of casting:
Widening Casting (automatically) - converting a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double
Narrowing Casting (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
Widening Casting
Widening casting is done automatically when passing a smaller size type to a larger size type
public class Main {
public static void main(String[] args) {
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double
System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0
}
}
*Narrowing Casting
*
Narrowing casting must be done manually by placing the type in parentheses () in front of the value
public class Main {
public static void main(String[] args) {
double myDouble = 9.78d;
int myInt = (int) myDouble; // Manual casting: double to int
System.out.println(myDouble); // Outputs 9.78
System.out.println(myInt); // Outputs 9
}
}
Top comments (0)