Variables, Data Types and Literals in Java

Java has eight primitive types and everything else is a reference. Knowing which is which explains assignment, defaults and equality.

What a variable is

A variable is a named, typed storage location. In Java the type is fixed when the variable is declared and can never change, which is what statically typed means in practice.

int count = 10;          // declaration with initialisation
double price;            // declaration
price = 249.75;          // assignment

The two families of type

Every Java type is either a primitive or a reference. There is no third option.

  • A primitive variable holds the value itself.
  • A reference variable holds a reference to an object stored elsewhere. Its own value is that reference, never the object.

The eight primitive types

TypeSizeRange or valuesDefault
byte8 bit-128 to 1270
short16 bit-32768 to 327670
int32 bitabout -2.1 to 2.1 billion0
long64 bitabout -9.2 to 9.2 quintillion0L
float32 bitapproximate decimal0.0f
double64 bitapproximate decimal0.0d
char16 bita single UTF-16 code unitthe null character
booleanunspecifiedtrue or falsefalse
Defaults apply to fields and array elements only. A local variable has no default, and reading one before assignment is a compile error rather than a bug at runtime.

Reference types

Classes, interfaces, arrays, enums and records are all reference types. Their default value is null.

String city = "Chennai";     // a reference to a String object
int[] scores = new int[3];   // a reference to an array object
String missing = null;       // a reference that points at nothing

Constants with final

final int MAX_RETRIES = 3;
// MAX_RETRIES = 4;          // compile error

static final double TAX_RATE = 0.18;   // a class level constant

final fixes the variable, not the object it points to. A final reference to a mutable list can still have elements added to it.

Local variable type inference with var

Since Java 10 the compiler can infer the type of a local variable:

var total = 0;                        // inferred as int
var names = new ArrayList<String>();  // inferred as ArrayList of String

var is not dynamic typing. The type is fixed at compile time exactly as if it had been written out. It is allowed only for local variables that have an initialiser, never for fields, parameters or return types.

Literals

int decimal   = 1_000_000;   // underscores are for readability only
int hex       = 0xFF;        // 255
int binary    = 0b1011;      // 11
long big      = 9000000000L; // the L suffix is required
float rate    = 1.5f;        // the f suffix is required
char letter   = 65;          // the character A, from its code unit
boolean valid = true;

A whole number literal is an int unless suffixed with L, and a decimal fraction is a double unless suffixed with f. Forgetting either suffix is a frequent early error.

Text blocks

Since Java 15 a multi line string can be written without escaping every line break:

String message = """
        Order received.
        Thank you.""";

Common mistakes

  • int average = 7 / 2; gives 3. Integer division discards the remainder before any assignment happens.
  • double d = 0.1 + 0.2; is not exactly 0.3. Binary floating point cannot represent those values precisely, so money should use BigDecimal or whole minor units.
  • Assuming an uninitialised local variable is zero.
  • Declaring a variable far from where it is used, which widens its scope for no reason.

Best practices

  • Declare a variable at the point of first use and give it the narrowest scope that works.
  • Prefer int and double unless a specific reason argues otherwise. Smaller types rarely save anything meaningful.
  • Use final for values that must not change, and write constants in upper case with underscores.
  • Use var only when the initialiser already makes the type obvious to a reader.

Practice

  1. Predict the output of System.out.println(5 / 2 + " " + 5 % 2 + " " + 5.0 / 2);
  2. Why does byte b = 200; fail to compile while byte b = 100; succeeds?
  3. Write a declaration for a constant holding the number of seconds in a day, choosing the type deliberately.
  4. Explain why final List<String> names does not make the list unmodifiable.

Conclusion

Primitives hold values and references hold the addresses of objects. That distinction decides how assignment, comparison and default values behave, and almost every confusing result later traces back to it.

Topics #Beginner #Java
Written by Lorens Mishra

Default administrator account created by the installer.

Continue reading

All Java notes →
Java

Introduction to Java

Java is a statically typed, object oriented language that compiles to bytecode and runs on a virtual machine, which is what makes it portable.

Read more

Discussion

0 comments
Sign in to join the discussion.

No comments yet. Be the first to say something.