Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Mastering Java Type Casting: From Widening to Narrowing and Beyond

Java Type Casting

This guide demystifies Java type casting, covering its primary forms and illustrating each with clear examples.

Before diving into Java type casting, ensure you understand Java’s data types.


Type Casting

The process of converting the value of one data type (int, float, double, etc.) to another is known as type casting.

Java offers 13 conversion types, but this tutorial focuses on the two core categories:

  1. Widening Type Casting
  2. Narrowing Type Casting

For a comprehensive list, see Java’s official Type Conversion documentation.


Widening Type Casting

In Widening Type Casting, Java automatically promotes a smaller primitive to a larger one, ensuring no data loss.

Example: Converting int to double

class Main {
  public static void main(String[] args) {
    int num = 10;
    System.out.println("The integer value: " + num);
    double data = num;
    System.out.println("The double value: " + data);
  }
}

Output

The integer value: 10
The double value: 10.0

This demonstrates how an int variable is promoted to double before assignment. The automatic nature of this conversion is why it’s also called implicit type casting.

Note: Widening conversions are safe and performed by the compiler.


Narrowing Type Casting

With Narrowing Type Casting, you must explicitly cast a larger primitive to a smaller one using parentheses.

Example: Converting double to int

class Main {
  public static void main(String[] args) {
    double num = 10.99;
    System.out.println("The double value: " + num);
    int data = (int)num;
    System.out.println("The integer value: " + data);
  }
}

Output

The double value: 10.99
The integer value: 10

The cast (int) truncates the fractional part, illustrating why this conversion is considered explicit type casting and can lead to data loss.

Note: Java does not automatically perform narrowing conversions.


Below are practical conversions between primitives and strings.

Example 1: int to String

class Main {
  public static void main(String[] args) {
    int num = 10;
    System.out.println("The integer value is: " + num);
    String data = String.valueOf(num);
    System.out.println("The string value is: " + data);
  }
}

Output

The integer value is: 10
The string value is: 10

Here, String.valueOf() converts the integer to its textual representation.


Example 2: String to int

class Main {
  public static void main(String[] args) {
    String data = "10";
    System.out.println("The string value is: " + data);
    int num = Integer.parseInt(data);
    System.out.println("The integer value is: " + num);
  }
}

Output

The string value is: 10
The integer value is: 10

Parsing relies on Integer.parseInt(). If the string cannot be parsed, a NumberFormatException is thrown.

Note: Validate input to avoid runtime exceptions.


Java

  1. Mastering Python Type Conversion & Casting: A Comprehensive Guide
  2. Java Primitive Data Types: A Complete Guide with Examples
  3. Mastering Java Encapsulation: A Practical Guide to Data Hiding
  4. C Typecasting: Mastering Implicit and Explicit Conversions with Code Examples
  5. Java Primitive Data Types: A Clear Guide
  6. Java Data Structures: Exploring Core Classes & Collections Framework
  7. Mastering Java Generics: Simplify Sorting and Enhance Type Safety
  8. C Programming: Mastering Type Casting & Conversion
  9. Java 10 Feature Spotlight: Mastering Local Variable Type Inference
  10. Comprehensive Guide to C# Data Types: Value, Reference, and Pointer