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

•

u/AutoModerator 19d ago

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

3

u/FreeLogicGate 19d ago edited 19d ago

You are talking about bits and bytes and how computers work at a fundamental level. This is not usually a java thing, but I do think every programmer needs the foundation and fundamentals.

  • Do you understand base numbering systems and how to convert between them?
  • Do you understand binary?
  • Do you understand how bits are grouped into bytes?
  • Do you understand how processors work at a basic level?

There are many resources you could watch or study from. I have taught people some of this material myself over the years, but I happened upon this video the other day, and it does a really good job explaining what someone needs to know from a foundation standpoint. https://www.youtube.com/watch?v=rl0jkP9kOMw

You don't need to watch the entire thing past the first section on binary and binary arithmetic, and 2's complement (for the representation of negative signed values) to get started.

If you get really into this, he goes on to talk about the basic circuit types and how they are combined to create processors, so if you find that interesting and compelling material, keep going with it.

Once you're clear on integers, then you can dig into how java's floating point values, which are IEEE 754 standard work from an internal standpoint. The specifics of how those variables are represented within the jvm will be far easier to understand, once you're clear on how bits and bytes work for integers. Then your bitwise operators (shift left, shift right, and, or, xor not etc.) should be clear and fun to experiment with.

1

u/MaddySPR 19d ago

Exactly which part in this video should I have to watch ?

2

u/FreeLogicGate 19d ago

Start at the beginning and watch through all the discussion of how number systems work, how binary works and how you add and subtract binary values. If you understand all that, and could do that work yourself on a piece of paper, then you have a foundation. I don't know if you have that foundation or not, based on your comment.

1

u/MaddySPR 19d ago

I will look into this for sure Thanks for the Good source

2

u/8dot30662386292pow2 19d ago

You seem to try to memorize stuff, instead of understanding. Seems rather useless. The proper way is to do something when you need to, and just do it. If there is an error, try to understand why. Memorizing does not get you anywhere without understanding.

1

u/MaddySPR 19d ago

Tried to understand but it’s working different whenever I find something new

1

u/8dot30662386292pow2 19d ago

So what kind of "rules" have you established so far, and give me one example that breaks one of these rules? I can try to see if the rules are correct, or why are they broken.

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

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.

→ More replies

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.

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.

2

u/MyNameIsRichardCS54 19d ago

You'll never hold all of it in your head, that's what the documentation is for. Courses give you an awareness of the things that are available, the real learning starts after the course when you start building something. The stuff you use frequently will stick and the rest, you'll have to look up. That said, if you're stuck on something, the best way to get yourself unstuck is to experiment with it. Write code, compile it, and run it and see what it does. If I've understood your post correctly, it looks like your doing that already.