Home Exam Help ICT Tutorials Government Jobs General Knowledge Courses Health Travel

Continued from Java Data Types Part 1.

Type Casting

Type casting is converting a value from one data type to another. There are two types:

Widening (Implicit) Casting

Automatically done when converting a smaller type to a larger type. No data loss occurs.

int myInt = 100;
double myDouble = myInt;  // 100.0

Narrowing (Explicit) Casting

Must be done manually when converting a larger type to a smaller type. Data loss may occur.

double myDouble = 9.78;
int myInt = (int) myDouble;  // 9

String Conversion

Use Integer.parseInt() and Double.parseDouble() to convert strings to numbers. Use String.valueOf() to convert numbers to strings.

Related: Wrapper Classes | OOP in Java | All ICT Tutorials