Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Q.A Academy
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Java

  • What is Java?
  • Why to Learn Java?
  • History of Java
  • Types of Java Applications
  • Naming Conventions
  • Hello World Program
  • Internal Details of Java program
  • What is Java JDK, JRE and JVM ?
  • What is Java Comments ?
  • What is Java Variable?
  • Variable Naming convention
  • Primitive Data Types
  • Non Primitive Data Types
  • Type Casting
  • Java Arithmetic Operators
  • Relational Operators
  • What is logical operators in Java? 
  • What is Assignment Operators in Java? 
  • What is Unary Operators in Java? 
  • What is Ternary Operators in Java?
  • What is Java Control Statements?
  • What are Decision Making statements?
  • What are looping statements?
  • What are Jump statements?
  • What is OOPS concept in Java?
  • Advantages of Java OOPs
  • What is Java Inheritance?
  • What is Java Constructor?
  • What is this keyword?
  • What is Polymorphism?
  • What is super keyword ?
  • What is final keyword? 
  • What are access modifiers?
  • What is Encapsulation? 
  • What is Abstraction? 
View Categories
  • Home
  • Tutorials
  • Java
  • Java
  • Type Casting

Type Casting

Type Casting

In Java, type casting is the process of converting a variable from one data type to another. This is necessary when you need to convert data from one type to another for various operations. Type casting is divided into two types: implicit casting (automatic) and explicit casting (manual).

1. Implicit Casting (Widening Conversion) #

Implicit casting is done automatically by the Java compiler when converting a smaller data type to a larger data type. This type of casting does not require explicit syntax and is safe because there is no risk of data loss.

Example:

public class Main {

    public static void main(String[] args) {

        int intValue = 100;
        
        // Implicit casting from int to long 
        long longValue = intValue;  

        // Implicit casting from long to float
        float floatValue = longValue;  

        System.out.println("Int value: " + intValue);   

        System.out.println("Long value: " + longValue);   

        System.out.println("Float value: " + floatValue); 

    }

}

Output:

Int value: 100
Long value: 100
Float value: 100.0

2. Explicit Casting (Narrowing Conversion)

Explicit casting is required when converting a larger data type to a smaller data type. This type of casting can result in data loss, so it must be done explicitly using the cast operator (type).

Example:

public class Main {

    public static void main(String[] args) {

        double doubleValue = 100.99;

        // Explicit casting from double to int
        int intValue = (int) doubleValue;  

        System.out.println("Double value: " + doubleValue);  

        System.out.println("Int value: " + intValue);  

    }

}

Output:

Double value: 100.99
Int value: 100

Casting Between Primitive Data Types

Widening (Implicit Casting)

  • byte -> short -> int -> long -> float -> double

Example:

byte b = 10;

int i = b;  // Implicit casting from byte to int

Narrowing (Explicit Casting)

  • double -> float -> long -> int -> short -> byte

Example:

int i = 100;

byte b = (byte) i;  // Explicit casting from int to byte

Casting Between Reference Types #

Casting between reference types involves converting objects of one type to another within the inheritance hierarchy.

Upcasting (Implicit)

  • Converting a subclass type to a superclass type.

Example:

class Animal {}
class Dog extends Animal {}

public class Main {

    public static void main(String[] args) {

        Dog dog = new Dog();

        Animal animal = dog;  // Implicit upcasting

    }

}

Downcasting (Explicit)

  • Converting a superclass type to a subclass type.

Example:

class Animal {}

class Dog extends Animal {}

public class Main {

    public static void main(String[] args) {

        Animal animal = new Dog();

        Dog dog = (Dog) animal;  // Explicit downcasting

    }

}

Summary #

  • Implicit Casting (Widening): Automatic conversion from a smaller to a larger data type (e.g., int to long).
  • Explicit Casting (Narrowing): Manual conversion from a larger to a smaller data type (e.g., double to int).
  • Upcasting: Converting a subclass type to a superclass type.
  • Downcasting: Converting a superclass type to a subclass type.

Understanding type casting is crucial for handling different data types and ensuring proper operations in your Java programs.

Java
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
Non Primitive Data TypesJava Arithmetic Operators
Table of Contents
  • 1. Implicit Casting (Widening Conversion)
  • Casting Between Reference Types
  • Summary
© Copyright [2018-2025]. All Rights Reserved.