Menu

Type Casting and Type Conversion in Java
📰

Type Casting and Type Conversion in Java

DEV Community: java·Rakshambika·7 days ago
#joPGzSaR
#devtojava#devto#dev#number1#type
Reading 0:00
15s threshold

What is Type Conversion ?

Converting a value from one datatype to another.

It happens when we convert the smaller datatype into larger datatype.

It happens automatically by java.

There is no data loss.

This is called Implicit Type Conversion or Widening .

Example:

public class Main { public static void main ( String [] args ) {

int number1 = 10 ;

double result = number1 ;

System . out . println ( result ); } }

Enter fullscreen mode

Exit fullscreen mode

10.0

Enter fullscreen mode

Exit fullscreen mode

number1 is an integer variable.

we assigned number1 to result.

so java automatically converted int to double.

What is Type Casting?

Converting a value from one data type to another.

It happens when we convert a larger data type into a smaller data type.

It is manually performed by the programmer.

There may be data loss during conversion.

This is called Explicit Type Casting or Narrowing Conversion.

Example:

public class Main { public static void main ( String [] args ) {

double number1…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.