http://www.technicalpage.net/search/label/SQL

Type Casting in Java

Type Casting in Java


getting the value/output in the desired datatype for the value/variable which is not in the desired datatype. Such as , 3.1416 is not in int but we want the integer value of this value then
int i = (int) 3.1416

float  f = 4.894f
double k = (double) f

int  x = (double) f

Type casting could be implicit or explicit
Type casting not possible for boolean data type


implicit:
changing small datatype to larger datatype
such as long l = (int) Value  , long is 64 bit and int is 32bit
Implicit casting is done by compiler itself/automatically.
Which dataType is bigger? see below pic:

Note: Float is larger than long. It depends on the range of values of the datatypes.

Explicit :
changing larger datatype(higher range datatype) to smaller datatype(lower range datatype)

int i = (int) LongValue


There might be data loss or truncation of data in explicit casting such as
Truncation of data:
int i = (int) 3.14
OUTPUT:
i = 3

DataLoss (out of range)
long l = 123456789 l
byte b = (byte) l

No comments:

Post a Comment