Explanatory Question
What is type casting?
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.
Type casting is the process of converting a variable from one data type to another. In Java, type casting is mainly classified into two types:
public class Main { public static void main(String[] args) { int num = 100; double doubleNum = num; // Automatic widening conversion System.out.println(doubleNum); // Output: 100.0 } }
Conversion order: byte → short → int → long → float → double
public class Main { public static void main(String[] args) { double num = 99.99; int intNum = (int) num; // Explicit casting (double to int) System.out.println(intNum); // Output: 99 (fractional part lost) } }
Conversion order (narrowing): double → float → long → int → short → byte
First read the answer fully, then try to explain it in your own words. After that, open a few related questions and compare the concepts. This method helps you remember the topic for a longer time and improves exam preparation.