Skip to main content

Type Casting in Java


We deal with different data types in Java. Such as int, long, double, char, etc.

These data types have their own size.

For example, double data type requires greater space in the memory compared to float. Like that ‘byte’ requires lesser space compared to ‘short’ data type.

But, every data type occupies certain amount of space in the memory.

We always try to assign the exact data type for a specific type of data. For integer value we can assign ‘int’ or ‘long’ data type, for character we assign ‘char’ data type.

Now, when we assign the value of one data type to another data type is known as type casting.

 

There are two types of type casting:

1.     Automatic type casting (Widening)

2.     Manual type casting (Narrowing)

 

Automatic type casting:

Automatic type casting is the process of assigning larger data type to a variable from a smaller data type.

Example:  byte -> short -> char -> int -> long -> float -> double.

It is not necessary to be in the same sequence shown above.

It can be from ‘byte’ to ‘char’ or maybe from ‘int’ to ‘double’.

Note: Boolean data type cannot be type casted to any other data type.

As the name suggests automatic type casting is performed by the machine automatically and does not require and manual instructions to be given to the compiler.

Java Programming for Automatic Type Casting:

package typeCasting;

 

public class TypeCasting {

 

     public static void main(String[] args) {

 

         

          byte x = 2;       

          int y = x;        

          float f = y;      

          double z = f;

         

         

          System.out.println(x);

          System.out.println(y);

          System.out.println(f);

          System.out.println(z);

         

 

     }

 

}

 

Console Result:

2

2

2.0

2.0

 

 

 

Manual type casting:

Manual type casting is just opposite phenomenon of automatic type casting.

Manual type casting is the process of assigning smaller data type to a variable from a larger data type.

Example:  double -> float -> long -> int -> char -> short -> byte

Same above sequence need not to be followed but you can also type cast from double to long or may be char to byte

In this case of type casting you have to instruct the compiler through a small piece of coding to accomplish the task.

Compiler cannot do narrowing automatically.

 

Java Programming for Manual Type Casting:

package typeCasting;

 

public class TypeCasting {

 

     public static void main(String[] args) {

 

          double x = 24.7568;

          int y = (int)x;

         

          System.out.println(x);

          System.out.println(y);

         

 

     }

 

}

 

Console result:

24.7568

24

 

As you can see, when we perform the above type casting from double to int, it always prints the floor value and not the ceiling value.

 

 

Comments

Popular posts from this blog

What is Java

Java is a general purpose programming language. Which means this programming language is used in many purposes apart from being used only in android application development. There are a few similarities between Java programming language and C++ programming language. But Java programming language is having some advanced features which you are going to learn in future. In programming languages you write programs in the form of statements in many lines and those statements are executed one by one. In Java, the execution happens really fast and concurrently it can execute many statements. Java is a platform independent (Java compiled byte code can be run across different operating systems) and object oriented programming language.   Why should we learn Java programming language?   Easy to learn: Java is a easy to learn programming language. Once you start learning Java programming, you will actually be learning the real programming and you will observe everyth...

Java User Input Exercise

Exercise 1: Write a simple java program to take user input of a number and store it inside an integer variable. Later print the number on the screen.   Solution 1:   import java.util.Scanner;   public class UserInpur {        public static void main(String[] args ) {               Scanner sc = new Scanner(System. in );           System. out .println( "Enter a number" );           int x = sc .nextInt();           System. out .println( "You have entered:" );                   System. out .println( x );        }   }   Exercise 2: Write a simple java program to tak...

Java Operators

In this lesson you are goin g to learn about operators in Java and also what are the different types of operators used in Java programming.   In our previous lesson we have learnt how to take user input in Java. We were performing different types of arithmetic operations in order to create a simple ration shop billing application which took different values as the user input and calculated total billing amount.   During the programming the billing application we used different signs like addition (+) , multiplication (*) etc. These signs are called operators in Java programming.   Types of Operators in Java There are five types of operators in Java. Arithmetic Operators Bitwise Operators Assignment Operators Comparison Operators Logical Operators 1.Java Arithmetic Operators Arithmetic operators are used to perform mathematical operations such addition, division, multiplication, subtraction and modulation. Let’s see the example of arithmetic operators in below:   ...