Java Types

Here are all articles on Programming.Guide related to types in Java.

  1. Java Basics: Types
    A types tell the compiler how to interpret data. It allows the compiler to catch a whole class of errors before the program starts executing.
  2. Primitive Types
    Java has 8 primitive types: byte (-128..127), short (-32768..32767), int (-2147483648..2147483647), long, float, double, char, boolean. This page is a reference for all relevant information about these types.
  3. Primitives vs Objects and References
    You could view primitive values as actual data, and objects as containers of data. If primitive values where atoms, objects would be molecules. Objects and primitive values have types.
  4. Ranges of Primitive Types
    Ranges of primitive types: byte [-128..127], short [-32768..32767], int [-2147483648..2147483647], long [-9223372036854775808..9223372036854775807], float [±3.40282347×1038], double [±1.7976931348623157×10308], char [\u0000..\uFFFF], boolean [false, true].
  5. Wrapper Types
    Each primitive type (int, byte, double, …) has a corresponding wrapper type (Integer, Byte, Double, …). Since these wrapper types are of reference type, they can be used when working with collections.
  6. Autoboxing and unboxing
    The compiler automatically converts between primitive values and boxed values. This feature is called autoboxing and unboxing. Without it, code would quickly get messy, especially when working with collections.
  7. Boxed values and equality
    Boxed operands are always unboxed, except in the case of == and !=. In these situations operands are not unboxed. This gives rise to some unexpected behavior.
  8. No byte or short literals?
    According to JLS a literal such as 123 is an int. You can turn it into a long, float or double by appending an 'L', 'f' or a 'd', but there's no way to turn it into a byte or a short.
  9. Byte (class) vs byte (primitive)
    Byte is the wrapper type for the primitive type byte. Objects of type Byte are often referred to as boxed values. The fundamental difference is that Byte is a class defined in the standard library, while byte is part of the language itself.
  10. Short (class) vs short (primitive)
    Short is the wrapper type for the primitive type short. Objects of type Short are often referred to as boxed values. The fundamental difference is that Short is a class defined in the standard library, while short is part of the language itself.
  11. Integer vs int
    Integer is the wrapper type for the primitive type int. Objects of type Integer are often referred to as boxed values. The fundamental difference is that Integer is a class defined in the standard library, while int is part of the language itself.
  12. Long (class) vs long (primitive)
    Long is the wrapper type for the primitive type long. Objects of type Long are often referred to as boxed values. The fundamental difference is that Long is a class defined in the standard library, while long is part of the language itself.
  13. Float (class) vs float (primitive)
    Float is the wrapper type for the primitive type float. Objects of type Float are often referred to as boxed values. The fundamental difference is that Float is a class defined in the standard library, while float is part of the language itself.
  14. Double (class) vs double (primitive)
    Double is the wrapper type for the primitive type double. Objects of type Double are often referred to as boxed values. The fundamental difference is that Double is a class defined in the standard library, while double is part of the language itself.
  15. Character vs char
    Character is the wrapper type for the primitive type char. Objects of type Character are often referred to as boxed values. The fundamental difference is that Character is a class defined in the standard library, while char is part of the language itself.
  16. Boolean (class) vs boolean (primitive)
    Boolean is the wrapper type for the primitive type boolean. Objects of type Boolean are often referred to as boxed values. The fundamental difference is that Boolean is a class defined in the standard library, while boolean is part of the language itself.