YAJC become Java programmer
75 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
Hello everybody! I've just explored a cool web application: https://hyperskill.org/
It's a web app developed by JetBrains which creates the best IDE for Java: Intellij IDEA.

The learning path is project based and you study topics which are building blocks for the project.
Unfortunately Java is not free but Kotlin is. If you wanted to learn Kotlin I hope it's a good way to do it. Anyway they will give you some trial period and you can learn Java for that period.

If you would like to try it use my join link please: https://hyperskill.org/join/e95f20c89
🔥2
Let's have some practice solving a tricky question
👍2
What will be the output of the code?
Anonymous Quiz
57%
Java
43%
Something else
What does the above code print?
Anonymous Quiz
23%
4.9E-324
49%
0.0
11%
-4.9E-324
17%
-2147483648
While we are preparing an article about Maven try to solve another quiz.
What does this code print?
Anonymous Quiz
15%
-3
15%
-1
0%
0
5%
1
65%
3
Hey 👋
In order to start with Maven we need to learn some basic topics. It’s time for you to choose the next!
Welcome to the chat to discuss what each topic is.
👍1
What is the value of Math.abs(Integer.MIN_VALUE)
Anonymous Quiz
22%
0
44%
-2147483648
0%
-2147483647
0%
2147483648
11%
2147483647
22%
Exception
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