Type casting in Java

What is type casting

  •  “The process of converting of data from one type to another type is known as type casting”.

 There are two types of type casting which is given bellow.

  1.  Implicit type casting
  2. Explicit type casting

Implicit type casting

The implicit type casting is automatically done by the compiler without user interference.  

Explicit type casting

This explicit type casting is done by user by using type cast operators.

Implicit type casting with example

Type casting is when you assign a value of one primitive data type to another type.

  • Implicit type casting (automatically) – converting a smaller type to a larger type size
    byte -> short -> char -> int -> long -> float -> double

For example the int data can be cast in long.

OUTPUT:

The above program in written.

package sssss;

public class computertechnologer {

public static void main(String[] args) {
int x = 4;
long y = x;
System.out.println(y);

}

}

OUTPUT:4

Explicit type casting with example

  • Explicit type Casting (manually) – converting a larger type to a smaller size type
    double -> float -> long -> int -> char -> short -> byte

For example double is cast in int.

OUTPUT:

The above program in written

package sssss;

public class computertechnologer {

public static void main(String[] args) {
double x = 3.14;
int y =(int)x;
System.out.println(“the output is ” + y);
}

}

OUTPUT: the output is 3

5/5
I hope you all like this article
Web Designer

Comment bellow if you have any problem in type casting

Rate this post
Categories: Java

Saurabh

A digital marketer, programmer, and WordPress developer who is always looking for new technology stuff and news. Saurabh has 1+ years of experience in writing content and in web development.

1 Comment

web site · May 10, 2020 at 1:07 am

web site indexletme işi.

Leave a Reply

Avatar placeholder

Your email address will not be published.