YAJC become Java programmer
77 subscribers
8 photos
30 links
Yet another Java channel about programming
Download Telegram
Hello everybody ๐Ÿ™‹โ€โ™‚๏ธ

Today I've met some excellent articles and I'm happy to share them with you.

https://catonmat.net/bash-one-liners-explained-part-one - working with files
https://catonmat.net/bash-one-liners-explained-part-two - working with strings
https://catonmat.net/bash-one-liners-explained-part-three - this one made my day ๐Ÿ˜ It's about redirections of input/output

We believe that almost every developer should be in close acquaintance with bash. If you didn't read https://t.me/yajc_java/9 yet, don't waste your time and get to it as soon as possible.

In one of the coming articles we'll mix user input and file redirections to process file with a Java program.
#beginner #bash #linux
Why Math.abs(Integer.MIN_VALUE) equals Integer.MIN_VALUE in Java โ˜•๏ธ

In Java, we often use the Math.abs() function to calculate the absolute value of a number. Itโ€™s supposed to return the positive equivalent of any number by stripping away its negative sign.
However, an interesting trick happens when you apply Math.abs() to Integer.MIN_VALUE. Surprisingly, it returns Integer.MIN_VALUE itself, not its positive counterpart. Letโ€™s explore why.

In Java, an int is a 32-bit signed integer, which means it can represent values defined as:

Integer.MIN_VALUE = -2,147,483,648
Integer.MAX_VALUE = 2,147,483,647

Notice that the absolute value of Integer.MIN_VALUE would theoretically be 2,147,483,648, which is one greater than Integer.MAX_VALUE. But Java's int type cannot represent numbers larger than Integer.MAX_VALUE. This creates a situation where the positive equivalent of Integer.MIN_VALUE doesn't exist within the int range.

Why Does This Happen?
Here's what happens under the hood when you try to get the absolute value of Integer.MIN_VALUE

1. Two's Complement Representation (wiki https://en.wikipedia.org/wiki/Two%27s_complement): Java uses two's complement to represent signed integers. In this system, the negative of a number is formed by flipping its bits and adding 1. However, in the case of Integer.MIN_VALUE, thereโ€™s no positive equivalent because of how twoโ€™s complement works. The range for negative values is slightly larger than for positive values.

2. Overflow: When applying Math.abs() to Integer.MIN_VALUE, the function tries to negate the value. Negating Integer.MIN_VALUE would result in 2,147,483,648, which is beyond the representable range of the int type. As a result, the value overflows and remains Integer.MIN_VALUE.
In other words itโ€™s similar to Integer.MAX_VALUE + 1 wichi is equal to Integer.MIN_VALUE.

3. Special Case in Java: Java's Math.abs() function doesnโ€™t throw an error in this situation but recognizes that the result is out of bounds and simply returns Integer.MIN_VALUE.

Explanation Through Bit Representation
To better understand this behavior, let's examine the binary representation of Integer.MIN_VALUE:

Integer.MIN_VALUE in binary:
10000000 00000000 00000000 00000000
(the leftmost 1 indicates that the number is negative in two's complement).
Negating this value in two's complement results in an overflow back to the same number because no positive counterpart exists within the 32-bit int range.

Conclusion
The reason Math.abs(Integer.MIN_VALUE) equals Integer.MIN_VALUE is due to Javaโ€™s use of two's complement arithmetic and the limitations of the int data type. Because Integer.MIN_VALUE has no positive counterpart in the int range, applying Math.abs() to it causes an overflow, returning the same negative value. This behavior is a consequence of how integers are represented and negated in binary.

#java #beginner
1