Dividing two ints gives an int: 7 / 2 is 3. Use 7 / 2.0 for 3.5.
💡 Key Takeaways: Java Type Casting & Operators
Mastering Java Type Casting & Operators helps you write clean and reliable Java code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
Java Type Casting & Operators Code Example
Java
public class Main {
public static void main(String[] args) {
double d = 9.78;
int i = (int) d;
int parsed = Integer.parseInt("42");
System.out.println(i);
System.out.println(parsed + 8);
System.out.println(7 / 2);
System.out.println(7 / 2.0);
System.out.println(17 % 5);
int x = 10;
x += 5;
System.out.println(x > 12 && x < 20);
}
}
Output
9
50
3
3.5
2
true
Run this example on your own computer. The "Get Started" lesson shows how to set it up.
Java Type Casting & Operators Practice Exercises
Check what you learned in “Java Type Casting & Operators”.