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
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
catonmat.net
Bash One-Liners Explained, Part I: Working with files
I love being super fast in the shell so I decided to do a new article series called Bash One-Liners Explained. It's going to be similar to my other article series - Awk One-Liners Explained, Sed One-Liners Explained, and Perl One-Liners Explained. After I'mโฆ
Let's solve something fundamental and easy: https://telegra.ph/Reverse-a-word-01-29
Any feedback is appreciated
#java #beginner
Any feedback is appreciated
#java #beginner
Telegraph
Reverse a word
Hello! ๐ In this short post we will solve a typical task in programming which is given to every beginner. Task You are given a word, you need to reverse it.Examples: word โ drow; radar โ radar; sequence โ ecneuqes; Solution There are many possible solutionsโฆ
Why Math.abs(Integer.MIN_VALUE) equals Integer.MIN_VALUE in Java โ๏ธ
In Java, we often use the
However, an interesting trick happens when you apply
In Java, an int is a 32-bit signed integer, which means it can represent values defined as:
Notice that the absolute value of
Why Does This Happen?
Here's what happens under the hood when you try to get the absolute value of
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
2. Overflow: When applying
In other words itโs similar to
3. Special Case in Java: Java's
Explanation Through Bit Representation
To better understand this behavior, let's examine the binary representation of
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
#java #beginner
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