Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book


What happens if you assign an integer value to a variable that is too large for that variable to hold?

Logically, you might think that the variable is converted to the next larger type, but this isn't what happens. Instead, an overflow occurs—a situation in which the number wraps around from one size extreme to the other. An example of overflow would be a byte variable that goes from 127 (acceptable value) to 128 (unacceptable). It would wrap around to the lowest acceptable value, which is –128, and start counting upward from there. Overflow isn't something you can readily deal with in a program, so you should be sure to give your variables plenty of living space in their chosen data type.

Why does Java have all these shorthand operators for arithmetic and assignment? It's really hard to read that way.

Java's syntax is based on C++, which is based on C (more Russian nesting doll behavior). C is an expert language that values programming power over readability, and the shorthand operators are one of the legacies of that design priority. Using them in a program isn't required because effective substitutes are available, so you can avoid them in your own programming if you prefer.

  • + Share This
  • 🔖 Save To Your Account