Course contentsShow
← Java
Lesson 101 of 2,8703. Operators and ExpressionsFree lesson

Increment/Decrement with Different Integer Types

Apply ++ and -- to byte, short, long types and understand implicit casting behavior.

Increment/Decrement with Different Integer Types

What you'll learn: How the ++ and -- operators work with byte, short, and long types, and why Java handles them safely.

The Core Concept

You've used ++ and -- with int variables, but Java has other integer types: byte, short, and long. The good news? These operators work with all of them. The interesting part? Java automatically handles type conversions for you behind the scenes.

How It Works with Different Types

When you apply ++ or -- to a byte, short, or long variable, Java:

  1. Performs the operation
  2. Implicitly casts the result back to the original type
  3. Stores it in the variable

This is special behavior! Normally, operations on byte and short promote to int, but increment/decrement operators include an automatic narrowing cast.

byte b = 100;
b++;  // Java does: b = (byte)(b + 1);
// b is now 101

short s = 30000;
s--;  // Java does: s = (short)(s - 1);
// s is now 29999

long miles = 999999999L;
miles++;  // No casting needed; result stays long
// miles is now 1000000000L

Notice you don't write the cast yourself—the ++ and -- operators handle it automatically. This makes them more convenient than writing b = b + 1 (which would cause a compilation error without an explicit cast).

Watch Out for Overflow

Remember that overflow still applies. If a byte is at 127 (its maximum) and you increment it, it wraps to -128.

Key Takeaway: The ++ and -- operators work on byte, short, and long types with automatic implicit casting, making them safer and more convenient than manual arithmetic and assignment.