Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothtechsolutions@gmail.com
Vinoth Tech Solutions
  • Home
  • Tutorials
  • End-to-End QA Projects
    • API Manual and Automation Testing using SoapUI
    • DevOps CI CD using Real Time Project Setup
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • About Me & Feedback
    • LinkedIn Profile
    • Placed Students Feedback
    • Online Training Feedback
    • TechTalk
  • FAQS
  • Demo Sites
    • Practice Automation
      • Registration Form
      • Transaction Details
      • DropDown
      • Basic Authentication
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
      • Date Picker Calendar
    • E-Commerce Demo Application
    • Healthcare Demo Page
    • Create Account Demo Page
    • Python JS Online Compiler
  • Home
  • Tutorials
  • End-to-End QA Projects
    • API Manual and Automation Testing using SoapUI
    • DevOps CI CD using Real Time Project Setup
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • About Me & Feedback
    • LinkedIn Profile
    • Placed Students Feedback
    • Online Training Feedback
    • TechTalk
  • FAQS
  • Demo Sites
    • Practice Automation
      • Registration Form
      • Transaction Details
      • DropDown
      • Basic Authentication
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
      • Date Picker Calendar
    • E-Commerce Demo Application
    • Healthcare Demo Page
    • Create Account Demo Page
    • Python JS Online Compiler

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:

📄
filename.js
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:

📄
filename.js
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:

📄
filename.js
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:

📄
filename.js
Double value: 100.99
Int value: 100

Casting Between Primitive Data Types

Widening (Implicit Casting)

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

Example:

📄
filename.js
byte b = 10;

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

Narrowing (Explicit Casting)

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

Example:

📄
filename.js
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:

📄
filename.js
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:

📄
filename.js
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
Table of Contents
  • 1. Implicit Casting (Widening Conversion)
  • Casting Between Reference Types
  • Summary
© 2018 – 2025 V-Tech Solutions Ltd (UK), Reg. No: 16489105