Quick revision
The short answer
It prints "-126 -1294967296 3". Casting an int or long to a smaller integer type keeps only the lowest bits, so (byte) 130 is 130 - 256 = -126 and (int) 3,000,000,000L is 3,000,000,000 - 4,294,967,296 = -1,294,967,296. Casting a double to int truncates toward zero, so 3.99 becomes 3. None of these narrowing conversions throws an exception; they silently discard information.
What to remember
In this answer, we worked through three narrowing conversions. Integer narrowing keeps only the low bits, so (byte) 130 is -126 and (int) 3,000,000,000L is -1,294,967,296, and floating point to int truncates toward zero, so (int) 3.99 is 3. The line prints "-126 -1294967296 3" without any exception. We also saw that NaN converts to 0, out-of-range doubles clamp to the int limits, and Math.toIntExact and Math.round are safer when we need checking or rounding.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
It prints "-126 -1294967296 3". Casting an int or long to a smaller integer type keeps only the lowest bits, so (byte) 130 is 130 - 256 = -126 and (int) 3,000,000,000L is 3,000,000,000 - 4,294,967,296 = -1,294,967,296. Casting a double to int truncates toward zero, so 3.99 becomes 3. None of these narrowing conversions throws an exception; they silently discard information.
Why this question is asked
Narrowing casts appear whenever we read binary data, convert ids between types, or turn a computed double into an index or a count. They are also a common source of silent corruption, because the cast tells the compiler we accept any loss. Interviewers ask this question to see whether we can work out the exact results instead of guessing, which requires understanding two's complement and the rules for floating point conversion. In this answer, we will recap two's complement, work through each of the three casts, look at the special floating point cases, and finish with safer alternatives.
1A Quick Recap of Two's Complement
Java stores integers in two's complement. In an n-bit number, the top bit counts as minus 2 to the power n - 1 instead of plus. For a byte, that bit is worth -128, so the bit pattern 1000 0010 means -128 + 2 = -126. Every integer type uses the same encoding, only with more bits: int has 32 and long has 64.
A narrowing conversion between integer types simply throws away the high bits and keeps the low ones. The kept bits are then read as a signed number of the smaller type. That is why the result can change sign: if the new top bit happens to be 1, the value becomes negative. Arithmetically, the result is the original value minus the nearest multiple of 2 to the power n that brings it into range.
2Case 1: (byte) 130
130 in binary is 1000 0010, which needs 8 bits. As an int it is 0000 0000 0000 0000 0000 0000 1000 0010. The cast to byte keeps the low 8 bits, 1000 0010, and reads them as a signed byte.
The last line shows a pattern we use when reading bytes from a stream: masking with 0xFF widens the byte to int and clears the sign extension, so we get the unsigned value 0 to 255 back.
3Case 2: (int) 3,000,000,000L
The largest int is 2,147,483,647. The value 3,000,000,000 fits easily in a long, but in 32 bits it has its top bit set. The cast keeps the low 32 bits, and reading them as a signed int gives 3,000,000,000 - 2^32.
| Cast | Rule | Calculation | Result |
|---|---|---|---|
| (byte) 130 | keep the low 8 bits | 130 - 256 | -126 |
| (int) 3000000000L | keep the low 32 bits | 3000000000 - 4294967296 | -1294967296 |
| (int) 3.99 | truncate toward zero | drop the fraction | 3 |
| (int) -3.99 | truncate toward zero | drop the fraction | -3 |
Notice that the information is not all gone: the low 32 bits are still there, which is why reading them as unsigned recovers the number. But the signed value is wrong for any normal use, and nothing warned us. Math.toIntExact is the safe version of this cast: it returns the value when it fits and throws when it does not.
4Case 3: (int) 3.99
Converting a floating point number to an integer type follows different rules. Java rounds toward zero, which means it simply drops the fractional part. It does not round to the nearest value, so 3.99 becomes 3 and -3.99 becomes -3.
So putting the three together, the original println prints -126 -1294967296 3. When we want rounding rather than truncation, we should say so explicitly with Math.round, Math.floor or Math.ceil, which also makes the intent clear to the next reader.
5Special Floating Point Values
Floating point to int conversion never wraps like integer narrowing. Instead, a value too large for int becomes Integer.MAX_VALUE, a value too small becomes Integer.MIN_VALUE, and NaN, the not-a-number value, becomes 0. These rules come from the language specification, so they are the same on every JVM.
The last line combines both rules. A cast from double to byte first converts to int, with truncation and clamping, and then narrows the int to byte by keeping the low 8 bits, so 300.0 becomes 300 and then 300 - 256 = 44.
6Follow-up questions
What does (int) Double.NaN return, and (int) 1e20?
(int) Double.NaN returns 0, and (int) 1e20 returns 2147483647, Integer.MAX_VALUE. When a floating point value is converted to int or long, NaN becomes zero and values outside the range are clamped to the minimum or maximum of the target type, rather than wrapping the way integer narrowing does.
How do I convert a long to an int safely?
Use Math.toIntExact(value), which returns the int when the value fits and throws ArithmeticException otherwise. If the value comes from user input or another system, it is often better to validate the range first and return a clear error message.
Why does b & 0xFF give a positive number?
Because & promotes b to int with sign extension, giving 0xFFFFFF82 for -126, and the mask 0xFF clears everything except the low 8 bits, leaving 0x82, which is 130. This is the standard way to read a byte as an unsigned value.
7Conclusion
In this answer, we worked through three narrowing conversions. Integer narrowing keeps only the low bits, so (byte) 130 is -126 and (int) 3,000,000,000L is -1,294,967,296, and floating point to int truncates toward zero, so (int) 3.99 is 3. The line prints "-126 -1294967296 3" without any exception. We also saw that NaN converts to 0, out-of-range doubles clamp to the int limits, and Math.toIntExact and Math.round are safer when we need checking or rounding.
Open with
It prints minus 126, minus 1294967296 and 3. All three are narrowing primitive conversions, and Java performs them silently, with no exception. For the integer casts, Java keeps only the low bits of the value and reads them as a signed number in two's complement. 130 is 1000 0010 in binary, and as a signed byte that top bit is worth minus 128, so the result is 130 minus 256, which is minus 126.
Then, if they want more
The long 3 billion does not fit in an int, whose maximum is about 2.147 billion; keeping the low 32 bits gives 3 billion minus 2 to the 32, so minus 1294967296. For the double, conversion to int truncates toward zero, it does not round, so 3.99 becomes 3, and minus 3.99 would become minus 3.
I would add that double to int conversion does not wrap: NaN becomes zero and values too large clamp to Integer.MAX_VALUE or MIN_VALUE, so (int) 1e20 is 2147483647.
In real code I avoid silent narrowing where the value might not fit: Math.toIntExact throws if a long does not fit in an int, Math.round or floor make rounding explicit, and masking with 0xFF reads a byte as unsigned when parsing binary data.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What do these print?": https://scrivio-interview-library.netlify.app/java/types-and-values/9c550ad865/