r/learnjava • • 29d 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 comments

2

u/8dot30662386292pow2 29d 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 29d ago edited 29d 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 29d ago edited 29d 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 29d 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 29d 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 (0)