DEV Community

loading...

Discussion on: Java first steps: 20 things I'd forgotten

Collapse
khmarbaise profile image
Karl Heinz Marbaise • Edited

You have stated:

  1. Important Versions: 1 [1995], 5 [2004] and 8 [2014]. Current version in 2020-09: 9.

The current version of Java is JDK 15 ... released on the 15. September... Java 9 is already old... see en.wikipedia.org/wiki/Java_version... If I correctly understand that setence.

  1. Environment Variables: unless on Windows, you will need to set environment variable.

Where is the difference? On Windows you can set and should set environment variables (usually JAVA_HOME) as well as on other platform or not using environment variables at all as on other platform...

Comparison [==, !=, >, <, >=, <=, >>=, <<=]

Hm.. >>= If you check this page: docs.oracle.com/javase/tutorial/ja...
you will see the >>= belongs to the assignment operators and has nothing to do with comparison. Also for <<= which you have missed.

  1. Floating-Point Data Types: Java has some peculiarities about floating-point, like the outputs below.

You have stated there are some peculiarities.. let us take a look.

double salary = 1250.70;
System.out.println("S: " + salary); //Output: S: 1250.7

This is an assignment of a floating point (double precision) to a variable of the type double. Is this something unusual?

double division = 3.14 / 2;
System.out.println(division); //Output: 1.57

I don't see anything unusual here?

int otherDivision = 5 / 2;
System.out.println(otherDivision); //Output: 2

Integer division?

double newDivision = 5 / 2;
System.out.println(newDivision); //Output: 2.0
double lastDivision = 5.0 / 2;
System.out.println(lastDivision); //Output: 2.5

So the first one divides integer (the digit do not have .).. which result into integer division whereas the second one based on the writing 5.0 is a floating point which is by default a double see details docs.oracle.com/javase/tutorial/ja... furthermore docs.oracle.com/javase/specs/jls/s...

double number1 = 0.2;
double number2 = 0.1;
System.out.println(number1 +number2);
//Output: 0.30000000000000004

This is the usual behaviour for IEEE 754 double precision number cause internally is based on the power of 2 which means not every number can be converted from decimal into it's internal value without loss. For example 0.2 can't be expressed by a limited number of powers of 2 neither 0.1 (Take a look here: h-schmidt.net/FloatConverter/IEEE7...) this is general problem with floating point. The same result will be printed if you try the same in Rust:

let number1 = 0.2;
let number2 = 0.1;
let result = number1 + number2;
println!("{}", result);

Apart from those inaccuracy great article. Continue working on it.