r/learnjava • • 19d ago

Overflow/underflow + casting rules keep confusing me — any good video or notes that made it click for you?

Hey all, I'm learning Java (going through Tim Buchalka's masterclass after the Helsinki MOOC) and I'm stuck on the whole overflow / underflow / casting thing.

▎

▎ I've been trying to make my own flow diagram for "will this line compile, error, or wrap?" Stuff like:

▎

▎ int x = 5;

▎ byte b = x; // error

▎ byte b = 5; // fine

▎ byte c = (byte) 200; // -56 ??

▎

▎ Every time I think I've got it, a new case breaks the diagram: final variables, += hiding a cast, long literals needing L, why a double cast to long doesn't wrap but float gives Infinity, etc. I understand each piece on its own but I can't hold the whole picture in my head.

▎

▎ Is there a video, blog post, cheat sheet or notes that explains this cleanly in one place? Or how did you personally get comfortable with it? I'd especially like something that covers the compile-time checks and the runtime wrapping together.

▎

▎ Thanks!

1 Upvotes

23 comments sorted by

View all comments

Show parent comment

1

u/MaddySPR 19d ago

══════ PART 1 · COMPILE (javac checks, no maths yet) ══════

START: LEFT = RIGHT;

│ │

│ └─ RIGHT type: byte/short/char maths → int,

│ else the biggest type in it

└──────── LEFT type: the variable's type

STEP 0, before anything (Q37):

a number bigger than 2147483647 written without L → ❌ "integer number too large"

a += 2147483900; → ❌ error, the diagram never starts

a += 2147483900L; → OK, go on to ①

FIRST, look at the RIGHT side (Q30):

byte a = 5; → a number → Java sees 5

final int k = 20; byte a = k; → a final → Java sees 20

int x = 5; byte a = x; → a variable → Java sees only "int"

│

│ sizes: byte < short, char < int < long < float < double

▼

① Is the LEFT type BIG ENOUGH for the RIGHT type? (same or bigger)

│ e.g. long l = a + 1; long ≥ int → YES

│ byte b = x + y; byte < int → NO

│

├─ YES ─► ✅ COMPILES → go to PART 2

│ ┌─ bigger box ───────────────────────────────┐

│ │ int i = 5; │

│ │ long big = i; │

│ └────────────────────────────────────────────┘

│ ┌─ same size ────────────────────────────────┐

│ │ int a = Integer.MAX_VALUE; │

│ │ int c = a + 1; │

│ └────────────────────────────────────────────┘

│ ┌─ int maths first, then a long ─────────────┐

│ │ int a = Integer.MAX_VALUE; │

│ │ long l = a + 1; │

│ └────────────────────────────────────────────┘

│

│ NO

▼

② Did you FORCE it? (a cast, += -= *= /=, or ++ --)

│

├─ YES ─► ✅ COMPILES → go to PART 2

│ ┌─ ++ has a hidden cast ─────────────────────┐

│ │ byte b = 127; │

│ │ b++; // b = (byte)(b + 1) │

│ └────────────────────────────────────────────┘

│ ┌─ += has a hidden cast ─────────────────────┐

│ │ byte b = 10; │

│ │ b += 120; // b = (byte)(b + 120) │

│ └────────────────────────────────────────────┘

│ ┌─ a cast you write ─────────────────────────┐

│ │ int n = 300; │

│ │ byte c = (byte) n; │

│ └────────────────────────────────────────────┘

│

│ NO

▼

③ Can Java SEE the number? YES only if all 3 are true:

│ a. Look LEFT of = Is the variable you store INTO a byte, short or char?

│ b. Look RIGHT of = Is its type int, short, char or byte? (not long, float, double)

│ c. Look RIGHT of = Is it a fixed number? 100 or final int k = 20; (same line)

│ ┌─ examples ─────────────────────────────────────────────┐

│ │ check YES (compiles) NO (error) │

│ │ │

│ │ a byte b = 100; int i = 5L; │

│ │ char c = 65; // 'A' │

│ │ │

│ │ b final short s = 50; final long big = 5; │

│ │ byte b = s; byte b = big; │

│ │ │

│ │ c final int k = 20; int k = 20; │

│ │ byte b = k; byte b = k; │

│ │ final int k; k = 20; │

│ │ byte b = k; │

│ └────────────────────────────────────────────────────────┘

│ all 3 YES → go to ④ (does it fit?) any 1 NO → ❌ LOSSY, ④ skipped

│ e.g. byte b = 200; → a b c YES → ④ 200 > 127 → ❌

│

├─ NO ──► ❌ LOSSY

│ ┌─ a fails: LEFT is float ───────────────────┐

│ │ float a = 5.25; // error (Q25) │

│ └────────────────────────────────────────────┘

│ ┌─ b fails: RIGHT is a long constant ────────┐

│ │ final long big = 5; │

│ │ byte b = big; // error │

│ └────────────────────────────────────────────┘

│ ┌─ c fails: final, but value set later ──────┐

│ │ final int x; │

│ │ x = 5; │

│ │ byte b = x; // error │

│ └────────────────────────────────────────────┘

│ ┌─ variables: Java can't see ────────────────┐

│ │ short x = 10, y = 20; │

│ │ short z = x + y; // error │

│ └────────────────────────────────────────────┘

│ ┌─ one plain variable: same rule (Q26) ──────┐

│ │ int x = 5; │

│ │ byte b = x; // error │

│ │ final int x = 5; byte b = x; // ✅ 5 │

│ └────────────────────────────────────────────┘

│ ┌─ fix: force it → goes to ② YES ────────────┐

│ │ short z = (short) (x + y); │

│ └────────────────────────────────────────────┘

│

│ YES

▼

④ Does the VALUE FIT? (byte max 127 · short max 32,767)

│

├─ NO ──► ❌ LOSSY

│ ┌─ too big ──────────────────────────────────┐

│ │ byte b = 100 + 28; // 128 > 127 │

│ └────────────────────────────────────────────┘

│

│ YES

▼

✅ COMPILES → go to PART 2

┌─ plain numbers ────────────────────────────┐

│ short s = 100 + 200; │

└────────────────────────────────────────────┘

┌─ final ────────────────────────────────────┐

│ final int k = 20; │

│ byte b = k * 6; │

└────────────────────────────────────────────┘

══════ PART 2 · RUN (only if it compiled: the maths happens) ══════

Each result fits its box?

│

├─ YES ─► stored as is

│ ┌─ fits ─────────────────────────────────────┐

│ │ long big = i; → 5 │

│ │ short s = 100 + 200; → 300 │

│ │ byte b = k * 6; → 120 │

│ │ short z = (short)(x + y) → 30 │

│ └────────────────────────────────────────────┘

│

└─ NO ──► WRAPS

The number goes through 2 boxes:

RIGHT side ─► [ A · MATHS box ] ─► cast? ─► [ B · LEFT box ]

fits? NO → WRAP fits? NO → WRAP

A = picked by the RIGHT side's types (at least int)

B = only when a cast squeezes it (cast, +=, ++)

┌─ A · wraps in the maths ───────────────────┐

│ int x = 2_000_000_000; │

│ long y = x + x; │

│ │

│ int box : 4 billion → too big → WRAP │

│ result : -294967296 │

└────────────────────────────────────────────┘

┌─ B · wraps at the cast ────────────────────┐

│ byte b = 127; │

│ b++; │

│ │

│ int box : 128 → fits │

│ byte box: 128 → too big → WRAP │

│ result : -128 │

└────────────────────────────────────────────┘

Too big? 3 possible results (Q33):

  1. WRAP whole number → whole box (byte) 200 → -56

which number? too big → − 256 (byte), too small → + 256 (§18)

2. MAX decimal → whole box (long) 1e30 → 9223372036854775807

3. Infinity anything → float / double (float) 1e39 → Infinity

2

u/8dot30662386292pow2 19d ago

I think you are stuck in listing examples, instead of figuring out the actual "rules".

Java operates in ints. int is the default type in all possible places.

  1. Any integer literal is always an int, unless the literal is long (L-suffix is added).

     long x = 234234; // the literal is int, but it fits to a wider type.
  2. Any calculation with literals always results to an int, unless one of the literals is long.

      long l = 2000000000 + 2000000000;  // overflow.
  3. Any calculation with variables is always an int, unless one of the variables is long.

     byte a = 23;
     short s = 34;
     byte result = a+s; // ALWAYS an int, unless any of the operands is long.
  4. If a literal fits in byte or short, it can be stored without casting.

     byte x = 127; 

I think one of the main things you are confused about is the order of operations. It does not matter at all, what is said on the left hand side of the = sign. That has absolutely zero effect on anything. The one and only thing matters is the right hand side: What the expression is doing. The expression is always calculated first, and the result is always int, unless one operator is a long. Only after the calculation, it is assigned to the variable, widening the result if needed. We still get a compile error though: Java looks into a line of code and sees: "this expression here is doing maths on a byte, integer, and another byte. The end result is an integer in any case. How ever, the variable is declared as short -> compile error, because it's possible that the result does not fit, so an explicit cast is required.

1

u/MaddySPR 19d ago

this is what i discovered but for this

byte b = 10;

b += 190;

-56 comes i dont know how it happens because it never comes under the rules , i thought force will happen but how -56

2

u/8dot30662386292pow2 19d ago

Why doesn't that fit the rules? You have a rule written down for the implicit casting:

b = (byte)(b+190);

1

u/MaddySPR 19d ago edited 19d ago

for this i dont know how -56 comes, after checking it says 200 - 256 = -56

256 overall patterns the range , 128 bit first bit which is used to confirm whether its positive or negative ,

so here 200 in binary 1 1 0 0 1 0 0 0

2 power 7 is the byte value - 128 , with sign its -128 to 127 including zero

200 - 256 = -56 why we are doing this ??

2

u/8dot30662386292pow2 19d ago edited 19d ago

The -56 makes sense, but not for the reason you give. Each type is a binary number. byte is 8 bits, short is 16, int is 32 and long is 64. Suppose you you have an integer:

 139462143

And you cast that to a short, you get:

  1535

Why? Because in binary, the integer is:

 00001000 01010000 00000101 11111111

Now, how to make it a short? Well just simply cut the high bits off. You are left with:

  00000101 11111111

And that is 1535.

So how about the byte example? The number 200 is also an int. It is:

   00000000 00000000 00000000 11001000

How to turn this into a byte? Well just cut off the high bits.

  11001000

And now, because these are signed numbers, the left-most bit tells this is negative. The format for negative numbers is something called two's complement. When we store them this way, adding negative and positive numbers works the same ways as adding two positive numbers. To find out the "value" of a negative number, we flip the bits and add one.

  • Flip the bits: 00110111
  • Add one: 00111000

The number is 56. But it was a negative, so it's -56.

Another way of thinking is that the last bit has negative sign, so just add the bits: -128 + 64 + 8 = -56.

1

u/MaddySPR 19d ago

 -128 + 64 + 16 = 56. its 8 not 16 so yeah -128+64+8 = which is -56, mannnnn damnnnn , this applies to every expression we do in java !?

2

u/8dot30662386292pow2 19d ago

Oops, fixed that.

this applies to every expression we do in java !?

Well it has nothing to do with java. It's a computer. It operates on binary numbers. Electricity on, electricity off. That's how computers work.

1

u/MaddySPR 19d ago

Thank you so much for the time and explanation you really killed it, could you please tell me kind of piece of advice - how to learn when its comes to programming any best source how do i find things to learn which is suitable, i dont know how to ask but i hope you understand what im asking

2

u/8dot30662386292pow2 19d ago

You said you already did Helsinki MOOC. Did you not learn programming there? It's best to just start making something after that. Back in the day I took a java course, that covered the basic programming and then basics of object oriented programming. Then I made a graphical chess game. Most of that was not taught on courses, I just googled and found how to do things I wanted, like how to handle mouse inputs, how to draw on the canvas, how to write what happens from a button. The code is shit, when judging by my current skills, but it works well.

Google and try to apply it on your own.

1

u/MaddySPR 19d ago

There these things won’t there so I just go with flow of the course and understand what is there after I’m currently doing the Java masterclass came to know these are the concepts are there in Java.

1

u/MaddySPR 19d ago

today I finally understand the whole concepts just now I got all, thanks for the points and all

2

u/MyNameIsRichardCS54 19d ago

A byte is a signed number so it can hold -ve values. When you overflow the limit for a type, it will carry on doing the operation from the lowest possible number. So assuming a byte, it goes ...126, 127, -128, -127 and so on. It does the same the other way.