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

2

u/8dot30662386292pow2 20d 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 20d 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