Master Core Java Programming From Scratch

Clear, interactive, and structured coding lessons designed for absolute beginners.

Type Conversion in Java

Learn how values are converted from one data type to another in Java using widening conversion, narrowing conversion, and explicit casting.

What is Type Conversion?

Type conversion is the process of converting a value from one data type to another data type.

Java supports both implicit and explicit type conversion. The conversion performed depends on the relationship between the source and target data types.

Types of Type Conversion

Widening Conversion

Converts a smaller numeric type into a larger compatible numeric type automatically.

Java
int number = 100;

long value = number;
Narrowing Conversion

Converts a larger numeric type into a smaller type using an explicit cast.

Java
double price = 99.99;

int value = (int) price;

1. Widening Conversion

Widening conversion occurs when a value is converted from a type with a smaller range to a type with a larger range.

For compatible primitive numeric types, Java can perform this conversion automatically.

Java
int number = 100;

long value = number;

System.out.println(value);
Common Numeric Widening Path
Java
byte
  ↓
short
  ↓
int
  ↓
long
  ↓
float
  ↓
double
Important: Widening generally does not require an explicit cast, although conversions involving floating-point types can still involve precision considerations.

Widening Example

Java
public class DataTypes {

    public static void main(String[] args) {

        byte a = 10;

        short b = a;

        int c = b;

        long d = c;

        float e = d;

        double f = e;

        System.out.println(f);

    }

}

2. Narrowing Conversion

Narrowing conversion converts a value from a larger numeric type to a smaller numeric type.

Because information can be lost, Java requires an explicit cast.

Java
double number = 100.75;

int value = (int) number;

System.out.println(value);
Output: 100

The fractional part is discarded when converting this value to an integer.

Explicit Casting

Explicit casting is performed by placing the target type in parentheses before the value.

Java
double salary = 45000.75;

int roundedValue = (int) salary;

The general syntax is:

Syntax
targetType variableName = (targetType) value;

Data Loss During Conversion

Narrowing conversion can cause data loss because the target type may not be able to represent the complete source value.

Java
double value = 123.987;

int result = (int) value;

System.out.println(result);

Output:

Output
123

Integer Overflow During Narrowing

If a value is outside the range of the target integer type, narrowing can produce a value that differs significantly from the original value.

Java
int number = 130;

byte value = (byte) number;

System.out.println(value);
Why?

The value 130 is outside the range of a Java byte, which is -128 to 127. The narrowing conversion therefore keeps the low-order bits and the resulting value wraps according to two's-complement representation.

char and Numeric Conversion

A char can participate in numeric conversions because it is an unsigned 16-bit integral type.

Java
char letter = 'A';

int code = letter;

System.out.println(code);

The output is:

Output
65

Integer Division

Type conversion is particularly important when performing arithmetic operations involving integers.

Java
int a = 5;

int b = 2;

double result = a / b;

The result is 2.0, not 2.5, because the division is performed using integer operands before the result is assigned to the double.

To obtain a floating-point result, convert one operand:

Java
int a = 5;

int b = 2;

double result = (double) a / b;

System.out.println(result);

Converting Values to String

Java provides several ways to create a string representation of a value.

Java
int number = 100;

String text = String.valueOf(number);

System.out.println(text);

Converting String to Number

Wrapper classes provide parsing methods for converting text into primitive numeric values.

Java
String text = "123";

int number = Integer.parseInt(text);

System.out.println(number + 10);

Output:

Output
133

String to Double

Java
String text = "45.75";

double value = Double.parseDouble(text);

System.out.println(value);

String to Boolean

Java
String text = "true";

boolean value = Boolean.parseBoolean(text);

System.out.println(value);

Primitive and Wrapper Types

Java provides wrapper classes for primitive types.

Primitive Wrapper
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

Autoboxing

Autoboxing automatically converts a primitive value into its corresponding wrapper object when required.

Java
int number = 100;

Integer object = number;

Unboxing

Unboxing converts a wrapper object back into its corresponding primitive type.

Java
Integer object = 100;

int number = object;

Type Conversion Quick Reference

Conversion Automatic? Example
Widening Yes int → long
Narrowing No double → int
String → int Explicit parsing Integer.parseInt()
int → String Explicit conversion String.valueOf()

Common Mistakes

  • Assuming narrowing conversion is automatic.
  • Forgetting that decimal information may be lost when casting to an integer.
  • Ignoring overflow when converting to a smaller integer type.
  • Expecting integer division to produce a decimal result.
  • Forgetting that parsing invalid text can throw a runtime exception.
Summary

Java supports automatic widening conversion and explicit narrowing conversion. Casting is required when converting many larger numeric types into smaller ones. Java also provides parsing methods such as Integer.parseInt() and Double.parseDouble() for converting text into numeric values.