GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • C programming
  • C++ programming
  • Java programming
  • Python programming
  • HTML
  • CSS
  • Codenora IDE (opens in a new tab)
  • About us
  • Pricing
  • Contact us
  • Privacy policy
  • Terms of service
  • Refund and Cancellation

This view

  • Original page
  • New address

GeezerVison is showing www.dremendo.com — Typecasting in Java Programming Language | Dremendo

  • Dremendo
  • C
  • C++
  • Java
  • Python
  • HTML
  • CSS
  • More
    • Home
    • C Tutorial
    • C++ Tutorial
    • Java Tutorial
    • Python Tutorial
    • HTML Tutorial
    • CSS Tutorial
?>
profile image
Announcements
No new announcements.
mentora icon
Java Programming Tutorial
  • Java Basic Concepts
    • Introduction
    • Variable
    • Data Types
    • Naming Conventions
    • Integer Variable
    • Long Variable
    • Short Variable
    • Byte Variable
    • Float and Double Variables
    • Character Variable
    • Boolean Variable
    • Constant Variable
    • Typecasting
  • Operators in Java
    • Operators
    • Assignment Operators
    • Arithmetic Operators
    • Relational Operators
    • Logical Operators
    • Operator Precedence
  • Input and Output in Java
    • Environment Setup
    • Program Basic Structure
    • Output
    • Input
    • Comments
  • Decision Making in Java
    • Decision Making
    • if Statement
    • if else Statement
    • if else if Statement
    • Conditional Operator
    • switch case Statement
  • Mathematical Functions
    • Mathematical Functions
  • Loops in Java
    • Loops
    • for Loop
    • while Loop
    • do while Loop
  • Arrays in Java
    • One Dimensional Array
    • Two Dimensional Array
  • String in Java
    • String
    • StringBuffer
    • StringBuilder
  • Searching in Java
    • Linear Search
    • Binary Search
  • Sorting in Java
    • Bubble Sort
    • Selection Sort
    • Insertion Sort
  • Function in Java
    • Function
    • Recursive Function
    • Function Overloading
  • OOPs in Java
    • Classes and Objects
    • Instance Variables
    • Instance Methods
    • Access Modifiers
    • Constructor
    • Static Variables
    • Static Methods
    • Inheritance
    • Interface
    • Types of Inheritance
    • Polymorphism
    • Final Class and Method
    • Abstract Class and Method
    • Wrapper Classes
  • Data Structures in Java
    • Stack
    • Queue
    • Circular Queue
    • Double Ended Queue
    • Singly Linked List
    • Doubly Linked List
    • Stack using Linked List
    • Queue using Linked List
  • Packages in Java
    • Packages
  • Exception Handling in Java
    • Exception Handling
    • User-Defined Exception
  • Thread in Java
    • Thread
    • Thread Methods
  • File Handling in Java
    • Text File Handling
    • Binary File Handling
    • Random Access File Handling

bookmark
Table of Contents

    Typecasting in Java Programming Language

    In this lesson, we will learn about what is Typecasting and different types of it. We will also go through some examples and take a quiz at the end.

    What is Typecasting

    In Java program the process of changing one data type into another data type is known as Typecasting. There are two types of typecasting:

    • Implicit Typecasting
    • Explicit Typecasting
    Video Explanation

    Click the button below to watch the explanation.

    Please wait while the video is loading....
    video image

    Generally when we add, subtract, multiply, or divide two or more values of similar data type the output is also of the same data type. For example:

    int value / int value  =  output will be an int value
    float value / float value =  output will be a float value
    double value / double value = output will be a double value
    
    int a = 10;
    int b = 5;
    int c = a/b;    // the output of c is 2 which is of int data type

    In typecasting, when we work with dissimilar data types, the output can be a different data type. Let's explore the explanations of each type of typecasting with examples and how to use them in programming.

    Implicit Typecasting

    This is also known as automatic type conversion. In this type of casting (conversion) the data is converted from one data type to another data type by the compiler itself without programmer interference. This usually happens when we assign a smaller data type to a larger data type.

    Example
    int num = 10;
    float result;
    
    // Implicit type casting: int to float
    result = num;

    In the above example, when we assign the integer variable num to the float variable result, the compiler will automatically converts the integer value into a float value because the int datatype is smaller than float so it can be easily stored in float.

    Let's see some more examples.

    Example
    int value / float value = output will be of float datatype
    float value / int value = output will be of float datatype
    
    int value / double value = output will be of double datatype
    double value / int value = output will be of double datatype
    
    float value / double value = output will be of double datatype
    double value / float value = output will be of double datatype

    Whenever an equation contains two or more different data types, as shown in the above examples, the result will always be in the data type which is largest amongst the data types involved in the equation. This is because all other data types automatically convert to the highest data type available in the equation, resulting in the final output being of the highest data type. This type of conversion is known as Implicit Casting.

    Explicit Typecasting

    This is also known as manual type conversion. In this type of casting (conversion) the programmer manually converts a variable from one type to another using the cast operator. The syntax for explicit type casting is:

    Syntax
    (datatype_name) expression
    Example
    float num = 5.67f;
    int result;
    
    // Explicit type casting: float to int
    result = (int)num;

    In this example, the float num is explicitly cast to an integer when assigned to result variable. As a result, the fractional part of the float is discarded, and only the integer part is retained.

    Note

    Whenever we want to change the data type of a variable using explicit casting method, just write the name of the data type within ( ) before the variable name we want to convert, as shown in the example above.

    Let's see one more example for more clarification.

    Example
    int a = 5;
    int b = 2;
    float result;
    
    // Explicit type casting: int to float
    result = (float)a/b;    // result will be 2.5

    In the above example, the variables a and b are of int datatype. So if we divide then without typecasting the result will be 2.0 which is wrong. If we want the correct result, then we have to cast any one of the integer variables (a or b) to float using the cast operator as shown in the above example.

    Note

    Changing the data type of the variables a or b using explicit casting within the equation does not alter their original declared data types. The original data types remain unchanged. The changes are temporary and only apply for the purpose of solving the equation.

    What should we do if the expression looks like this?

    float c = 10 / 7;    // result of c will be 1.0

    In this case, the result is 1.0 because both numbers are treated as integers. To get a more accurate result, we can change either of the numbers to a decimal by adding a f (here f represent float) to the end of the number like this:

    float c = 10f / 7;    // result of c will be 1.428571

    Or like this:

    float c = 10 / 7f;    // result of c will be 1.428571
    Note

    To change an integer into a double type value, you can add .0 to the end of the number, like 7.0. This ensures that the number is treated as a double type value.

    Test Your Knowledge

    Attempt the questions in the exercise to check whether you have understood the lesson clearly.

    Exercise
    Test Your Knowledge
    Previous Next

    We care about your privacy

    Dremendo uses cookies and similar technologies that are necessary for the site to work properly, understand how you interact with our website, and measure the effectiveness of our marketing. By clicking Accept All, you consent to the use of non-essential cookies and similar technologies for analytics, advertising, and ad measurement, as described in our Privacy Policy.

    Cookie Preferences

    Choose which types of cookies Dremendo may use.

    Necessary Cookies

    Required for essential website functionality and security.

    Allow
    Analytics Cookies

    Help us understand how visitors use Dremendo.

    Allow
    Marketing Cookies

    Help us measure advertising performance and understand interactions with our advertisements.

    Allow
    Mentora Question Asked: 0 / 20

    Hi, I’m Mentora!

    I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way.

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.