Skip to content

Chapter 3 of 37

Data Types, Variables, and Operators

Use primitives, references, conversions, constants, and Java operators safely.

34 minutes 10 quick checksBy Subha Prasad
Lesson 3 of 37Course navigation

Lesson content

Read, practise, then check your understanding

Java primitives are boolean, byte, short, int, long, char, float, and double. Reference variables point to objects or hold null. Fields receive default values; local variables must be definitely assigned before reading. Prefer the smallest domain-appropriate type, not merely the smallest storage width.

Values and expressions

final int secondsPerHour = 60 * 60;
long population = 1_428_000_000L;
double ratio = 5.0 / 8.0;
char grade = 'A';
String label = "Java";
boolean valid = population > 0 && label != null;

Arithmetic includes + - * / %; integer division truncates. Relational operators yield booleans. && and || short-circuit, while & | ^ ~ << >> >>> also operate on bits. Numeric promotion may widen operands; narrowing needs an explicit cast and can lose data. Integer overflow wraps in two’s-complement arithmetic, so use Math.addExact when overflow is an error. == compares primitive values but reference identity; object value equality normally uses equals.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes primitive type?
Which Java term matches this description: A built-in value type such as int, double, boolean, or char.
Which statement best describes reference type?
Which Java term matches this description: A type whose variable can refer to an object or be null.
Which statement best describes final variable?
Which Java term matches this description: A variable that can be assigned only once.
Which statement best describes short-circuit evaluation?
Which Java term matches this description: Skipping the right operand when && or || already has its result.
Which statement best describes numeric promotion?
Which Java term matches this description: Converting smaller numeric operands to a common type for an operation.

0 of 10 checks passed

Your progress is saved on this device.

Data Types, Variables, and Operators | Java Lesson | Subha Prasad