Java p4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Program No-4 :-Write a program in Java to perform type conversion

of different data types.

1 Introduction

Type Casting: In typing casting, a data type is converted into another data type by the
programmer using the casting operator during the program design. In typing casting, the
destination data type may be smaller than the source data type when converting the data
type to another data type, that’s why it is also called narrowing conversion or Explicit
Conversion.

Syntax/Declaration:-

float x;

byte y;

y=(byte)x;

we are converting float(source) data type into byte(target) data type.

Type conversion : In type conversion, a data type is automatically converted into another
data type by a compiler at the compiler time. In type conversion, the destination data type
cannot be smaller than the source data type, that’s why it is also called widening
conversion or Implicit Conversion. One more important thing is that it can only be
applied to compatible data types.

Type Conversion example –

int x=30;
float y;

y=x; // y==30.000000.

1. Widening or Automatic Type Conversion

Automatic or implicit type conversion is done by the compiler internally. Converting a


lower data type into a higher one is known as widening. If two variables involved in an
operation are of the same data type, the result is also similar. If variables of different but
compatible data types are involved, then implicit conversion is automatic.

//Program to perform Implicit conversion:-

class Implicit{

public static void main(String args[]){

int y=90; //4 Byte

float z=y; //2 Byte

System.out.println(“Implicit Conversion:”+” “+z);

/*Output

Implicit Conversion: 90
*/

2. Narrowing or Explicit Type Conversion

Narrowing or Explicit conversion involves casting a higher data type into a lower one.
This type of conversion is not done automatically, and the desired data type needs to be
explicitly mentioned within ( ). Data types need not be compatible with each other for
explicit typecasting.

// Program to perform Explicit conversion

Class Explicit

Public static void main(String args[])

double y=78.9; //8 Byte

int z=(int)y; //4 Byte

System.out.println(“Explicit Conversion:”+” “+z);

}
/*Output

Explicit Conversion: 78.

*/

Condition for type conversion in Java:-

The condition for type conversion in Java is that both of the datatypes should be
compatible with each other. For example, we can convert the byte to int but not byte to
float data type or string as it is not a valid conversion.

Which type conversion is not accepted in Java?

Type conversion that is not accepted in Java is when there is a conversion between two
incompatible datatypes. For example, converting int to a string data type is not accepted.

We need typecasting in Java to ensure the compatibility between the data types of the
operands. Type casting in Java helps in the prevention of errors and makes the program
to run as expected.

You might also like