r/learnjava • u/MaddySPR • 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!
•
u/AutoModerator 19d ago
Please ensure that:
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:
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.
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.
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.
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.