DZone
Performance Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Performance Zone > The Great Effect of Small Details

The Great Effect of Small Details

This all goes to show that engineering skills are the most visible at the end points.

Ali Kemal TASCI user avatar by
Ali Kemal TASCI
·
Dec. 23, 16 · Performance Zone · Opinion
Like (4)
Save
Tweet
5.01K Views

Join the DZone community and get the full member experience.

Join For Free

Question: How many lines are written with the following code fragment?

public static void main(String[] args){
for (int i = Integer.MIN_VALUE; i <= Integer.MAX_VALUE; i++) {
System.out.println(i);
} 
}

A. Infinite

B. (2147483647 *2) +1

C. 0

D. Compilation error

Most of us, if we do not pay attention, may think the answer is B. However, the following code fragment can convince us that the answer is A.

public static void main(String[] args){
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MAX_VALUE + 1);
}

Output:

-2147483648

2147483647

-2147483648

In the above code fragment, the code runs as expected until the variable i equals Integer.MAX_VALUE. At that point, the value of i (which is Integer.MAX_VALUE) is printed. After passing the control of current iteration, i is incremented by one. We may think the new value of i is Integer.MAX_VALUE + 1, but no; it is Integer.MIN_VALUE. Since Integer.MIN_VALUE is less than Integer.MAX_VALUE, iteration goes on infinitely.

In order to understand this situation, we have to dive deep into the world of bytes and bits. In Java, int data type can hold values up to 32 bits. The lowest value that it can hold is Integer.MIN_VALUE, which is -2147483648, whose binary representation is 1000 0000 0000 0000 0000 0000 0000 0000. The left-most bit of this representation is the sign bit with 0, which indicates positive, and with 1, which indicates negative. In the previous value, the sign bit is 1. So, as we can see, Java uses only 31 bits to store the number.

Image title

With this knowledge, let’s add one to Integer.MAX_VALUE 2147483647, whose binary representation is 0111 1111 1111 1111 1111 1111 1111 1111:

Image title

As it is seen, the result is Integer.MIN_VALUE.

Thinking the above situation, for example, if we create a JPA entity class with an identifier property of type java.lang.Integer, our code works for a while (perhaps even years). But as the database size grows, the above problem occurs by their range. An integer would work for almost two months if we generate a new identifier each millisecond with no gaps, and a long would last for about 300 million years.

Consequently, choosing right data types may be a simple detail, but can also cause great effects.

By this way, I want to quote, my university professor's statement here: “Engineering skills are visible at the end points.”

Data Types

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top 20 Git Commands With Examples
  • Is NoOps the End of DevOps?
  • PermGen and Metaspace
  • MEAN vs MERN Stack: Which One Is Better?

Comments

Performance Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo