Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java literal value assignment behaviour

Tags:

java

In the book of SCJP guide by Kathy Sierra, in the assignments chapter, we learn that we can declare something like this byte b = 7;. Behind the scene the code is byte b = (byte) 7;. This is so because in java, number 7 is considered a literal int value so has be to cast to int.

Now other situation. Double can include every byte contained within a float value as it is a bigger datatype. So can we say float f = 10.543; As 10.543 is quite a small value and should fit within a float. Also literal value for such number is considered a Double so compiler should implicitly cast it to float. But it's not so, compiler stops us. We have to append an F or f after that value.

Why are these two conflicting behaviour there for literal value assignment? In short if byte b = 7 is possible. Why is float f = 10.543 not possible?

like image 579
Shades88 Avatar asked Sep 14 '12 06:09

Shades88


People also ask

What is literal value in Java?

Literals in Java are a synthetic representation of boolean, character, numeric, or string data. They are a means of expressing particular values within a program. They are constant values that directly appear in a program and can be assigned now to a variable.

What are character literals in Java?

Character literals are constant valued character expressions embedded in a Java program. Java characters are sixteen bit Unicode characters, ranging from 0 to 65535. Character literals are expressed in Java as a single quote, the character, and a closing single quote ( 'a' , '7' , '$' , 'π' ).

How many types of literals are allowed in Java?

Literals in Java can be classified into six types, as below: Integral Literals. Floating-point Literals. Char Literals.


1 Answers

You can read JLS 5.2 Assignment Conversion

The compile-time narrowing of constants means that code such as:

 byte theAnswer = 42;

is allowed. Without the narrowing, the fact that the integer literal 42 has type int would mean that a cast to byte would be required:

byte theAnswer = (byte)42;  // cast is permitted but not required

If the type of the expression cannot be converted to the type of the variable by a conversion permitted in an assignment context, then a compile-time error occurs.

If the type of the variable is float or double, then value set conversion (§5.1.13) is applied to the value v

JLS #3.10.2.Floating-Point Literals

A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d

5.1.2. Widening Primitive Conversion

A narrowing primitive conversion from double to float is governed by the IEEE 754 rounding rules (§4.2.4). This conversion can lose precision, but also lose range, resulting in a float zero from a nonzero double and a float infinity from a finite double. A double NaN is converted to a float NaN and a double infinity is converted to the same-signed float infinity.

I hope above clarifies your doubt.

like image 62
Amit Deshpande Avatar answered Oct 06 '22 01:10

Amit Deshpande