Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Java by (6.5k points)
retagged by
Could someone tell me what is explicit type casting?

1 Answer

0 votes
by (11.3k points)

Explicit type casting means forcing a smaller datatype variable to store a larger datatype value, overriding the default JAVA type casting rules. I'll explain this with an example:

int a=2;

double b=a;  /*this does not throw any errors as 'double' is larger datatype than 'int' */

a=b;              /* this throws an error because 'int' can't store a value of 'double' type since 'int' is smaller */

a=(int)b;       /* this doesn't throw any errors and this is an example of explicit type-casting */

You can refer to this table for looking up JAVA datatype sizes: (source: w3schools)

Data TypeSizeDescription
byte1 byteStores whole numbers from -128 to 127
short2 bytesStores whole numbers from -32,768 to 32,767
int4 bytesStores whole numbers from -2,147,483,648 to 2,147,483,647
long8 bytesStores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float4 bytesStores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double8 bytesStores fractional numbers. Sufficient for storing 15 decimal digits
boolean1 bitStores true or false values
char2 bytesStores a single character/letter or ASCII values

Browse Categories

...