Java Primitive's little secrets

Here is a bunch of java primitives' interesting behaviour that we ofter ignore or forget! Recap time in 3 - 2 - 1...

Data type promotions

When you try to perform a numerical operation on a short type for example, the result will be promoted to int, therefore it wont compile if you try to store the result in a short variable unless you explicitly cast it (check line x).

//////////////////////////////////////
short x = 5;
short y = 2;
short z = x*y // does NOT compile
short z = (short)(x*y) // does compile
//////////////////////////////////////
short x = 5;
short y = 2;
int u = x+y // it does
//////////////////////////////////////
view raw gistfile1.txt hosted with ❤ by GitHub

The same applies to byte. Adding two numbers of type byte they will automatically be promoted to short.

What about int? Adding two number of type int together wont result to a long promotion (of course)...

Same as int if you try to to add together two float numbers, the result wont be promoted to double.

All the above are great... but... There is an interesting behaviour when you do the following

byte x = 5;
byte y = 3;
y+=x;
view raw gistfile1.txt hosted with ❤ by GitHub

the above is perfectly valid, but the bellow results in a compilation error:

byte x = 5;
byte y = 3;
y=y+x; // doe NOT compile
view raw gistfile1.txt hosted with ❤ by GitHub

Yes indeed!

an easier one now:
Please welcome the infamous Numeric Overflow!

System.out.println(Integer.MAX_VALUE);
//output 2147483647
System.out.println(Integer.MAX_VALUE + 1);
//output -2147483648
System.out.println(Integer.MIN_VALUE);
//output -2147483647
System.out.println(Integer.MIN_VALUE - 1);
//output 2147483648
view raw gistfile1.txt hosted with ❤ by GitHub


Comments

Popular posts from this blog

DTOs conversion with reflection example - applied on Thymeleaf (or how to get read of Pojo's conversion boilerplate)

Make your bash script runnable from every directory on the terminal

How to create a Cassandra container for testing with Keyspace and the latest schema with a single script call