BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Type Casting in Java

Last Updated: October 26, 2022 by Chaitanya Singh | Filed Under: java

Assigning a value of one primitive data type to another primitive data type is known as casting. There are two types of type casting in java as shown in the following diagram.

  • Narrowing(explicit) type casting
  • Widening(implicit) type casting
Type Casting in Java

Narrowing Type Casting

It is also known as explicit type casting. It is done when assigning a larger size data type to smaller size data type, thus the term narrowing is used.

Notice the int inside parentheses before double variable d and float variable f. This is done when passing the larger size data type value to smaller size data type.

public class JavaExample {
  public static void main(String[] args) {
    double d = 4.55d;
    float f = 1.25f;
    int i = (int)d; // Explicit casting: double to int
    int i2 = (int)f; // Explicit casting: float to int

    System.out.println("Double Value: "+d); // Prints 4.55
    System.out.println("Double to int: "+i); // Prints 4
    System.out.println("Float value: "+f); // Prints 1.25
    System.out.println("Float to int: "+i2); // Prints 1
  }
}

Output:

Type casting example output - Explicit casting

Widening Type Casting

Widening type casting is an automatic casting. In this type casting, a value of smaller data type is assigned to larger size data type. This is also called implicit type casting as there is no need to mention the data type in parentheses and the conversion is handled automatically by the compiler.

public class JavaExample {
  public static void main(String[] args) {
    int i = 1001;
    float f = 105.00f;
    long l = i; // Implicit casting: int to long
    double d = f; // Implicit casting: float to double

    System.out.println("int Value: "+i); // Prints 1001
    System.out.println("int to long: "+l); // Prints 1001
    System.out.println("float value: "+f); // Prints 105.0
    System.out.println("float to double: "+d); // Prints 105.0
  }
}

Output:

implicit type casting example output
❮ PreviousNext ❯

Top Related Articles:

  1. Learn Java 9 Modules in 15 Minutes
  2. Passing a List to a Varargs method
  3. Java Integer byteValue() Method
  4. Java String to int Conversion
  5. How to loop LinkedList in Java

Tags: Java-Conversion

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap