Difficulty: Beginner | Prerequisites: None
Java has strict rules for naming things (identifiers), declaring variables, and choosing data types. Every variable needs a type declared up front, and Java will refuse to compile if the types do not match. Operator precedence determines evaluation order in expressions, and getting it wrong is one of the most common sources of logic bugs in introductory Java.
Identifier
The name given to a variable, method, class, or other element in Java. Must start with a letter, underscore, or dollar sign, and can contain letters, digits, underscores, and dollar signs after that.
In simple terms, this is the label you give something so you can refer to it later in your code.
Variable
A named storage location in memory that holds a value of a specific type. Declared with the syntax type name = value; or type name;.
Think of it as a labelled box that can only hold one kind of thing (integers, text, true/false, etc.).
Primitive data type
One of Java's eight built-in types that store simple values directly in memory: int, double, float, char, boolean, byte, short, long.
Think of these as the basic building blocks. They are not objects and have no methods.
int
A primitive type that stores whole numbers (no decimal point). Example: int count = 7;
double
A primitive type that stores decimal numbers with high precision (unlimited decimal places in practice). Example: double pi = 3.14159;
float
A primitive type that stores decimal numbers with limited precision. Requires an f suffix on the literal. Example: float price = 9.99f;
In simple terms, use float when you need fewer decimal places (money, simple measurements) and double when precision matters more.
char
A primitive type that stores a single character, enclosed in single quotes. Example: char grade = 'A';
boolean
A primitive type that stores only true or false (both lowercase). Example: boolean isReady = true;
String
A non-primitive (reference) type that stores a sequence of characters, enclosed in double quotes. Note the capital S. Example: String name = "Alice";
Think of it as a word or sentence stored as a single value. Strings are objects in Java, not primitives.
Operator precedence
The fixed order in which Java evaluates operators in an expression. Multiplication, division, and modulus are evaluated before addition and subtraction, and arithmetic is evaluated before comparisons and logical operators.
In simple terms, it is Java's version of PEMDAS, extended to cover comparisons and logic.
Modulus (%)
An arithmetic operator that returns the remainder after integer division. Example: 10 % 3 evaluates to 1.
Think of it as "what is left over after dividing evenly."
Can contain letters, digits, underscores (_), and dollar signs ($)
The first character cannot be a digit
No spaces allowed
No special characters other than _ and $
Valid examples: _name, Name123, $name, _n1a2m3e_
Invalid examples:
1name (starts with a digit)
Name! (contains a special character)
n a m e (contains spaces)
n@me (contains a special character)
Two forms:
With initialisation: type name = value;
Without initialisation: type name; (the variable is null/default until assigned)
Correct declarations:
int number = 3;
float myFloat = 3.25f; (the f suffix is required for float literals)
String myName = "student"; (double quotes, capital S on String)
boolean amAlive = true; (only true or false, both lowercase)
char firstLetter = 'L'; (single quotes for char)
Common declaration errors:
string name = 3; (lowercase string is wrong, and the value type does not match)
name = float int; (the data type must come first)
int my number = 3; (spaces in variable names are not allowed)
int 123number = 3; (cannot start with a digit)
int %()number = 3; (special characters not allowed)
Int; (no variable name provided)
In an assignment like a = b + c, only a changes. The variables on the right side (b and c) keep their current values.
Worked example:
int a = 3;
int b = 5;
b = 3 * a; // b becomes 9, a is still 3
a = b - 2; // a becomes 7, b is still 9
System.out.println(a); // prints 7
System.out.println(b); // prints 9int for whole numbers
double for decimal numbers needing high precision
float for decimal numbers with limited precision (money, simple measurements); requires f suffix
char for a single character
boolean for true/false values
Also available: byte, short, long
String, arrays, and objects (such as Scanner) are not primitive types. They are reference types.
Unary operators: !, +, -
Multiplication, division, modulus: *, /, %
Addition, subtraction: +, -
Relational comparisons: <, <=, >, >=
Equality comparisons: ==, !=
Logical AND: &&
Logical OR: ||
Assignment: =
Key detail: && is evaluated before ||.
Worked example:
2 + 5 <= 8 && 17 - 3 > 6Step by step:
Arithmetic first: 2 + 5 = 7, 17 - 3 = 14
Comparisons next: 7 <= 8 is true, 14 > 6 is true
Logical last: true && true = true
Students often think String is a primitive data type. It is not. String is a reference type (an object). The eight primitives are int, double, float, char, boolean, byte, short, and long.
Students forget the f suffix on float literals. Writing float x = 3.25; will not compile. It must be float x = 3.25f;.
Students assume that in a = b + c, all three variables change. Only a is reassigned. b and c retain their values.
Students confuse = (assignment) with == (equality check). x = 5 assigns the value 5 to x. x == 5 checks whether x is currently equal to 5.
⚠️ Expect questions that show a variable declaration and ask whether it compiles. Check: correct type, valid identifier, matching value type, proper literal suffix.
⚠️ Operator precedence questions are common. You will be given an expression and asked to evaluate it step by step. Know that && binds more tightly than ||.
⚠️ Tracing variable values through a sequence of assignments is a staple of CSE 110 exams. Only the left-hand side of an assignment changes.
⚠️ Know which types are primitive and which are not. String, arrays, and Scanner are reference types.
True or false: 2myVar is a valid Java identifier.
Answer: False. Identifiers cannot start with a digit.
True or false: String is a primitive data type in Java.
Answer: False. String is a reference type.
Fill in the blank: A float literal must end with the letter ____.
Answer: f
True or false: In x = y + z, the values of y and z change.
Answer: False. Only x is reassigned.
Which operator is evaluated first: && or ||?
Answer: &&
Q: Which of the following is a valid variable declaration? (a) int 3x = 10; (b) double price = 4.99; (c) boolean flag = "true"; (d) char letter = "A";
A: (b). Option (a) starts with a digit. Option (c) assigns a String to a boolean. Option (d) uses double quotes instead of single quotes for a char.
Q: Given the code below, what is the output?
int a = 4;
int b = 10;
a = b * 2;
b = a - 5;
System.out.println(a);
System.out.println(b);A: The output is 20 then 15. After a = b * 2, a is 20 and b is still 10. After b = a - 5, b is 15.
Q: Evaluate the following expression: 3 + 4 * 2 > 10 || 5 - 1 == 4 && false
A: First, arithmetic: 4 * 2 = 8, so 3 + 8 = 11, and 5 - 1 = 4. Then comparisons: 11 > 10 is true, and 4 == 4 is true. Then logic: true && false = false (because && is evaluated before ||). Finally: true || false = true.
Q: Name three things that make a Java identifier invalid.
A: Starting with a digit, containing spaces, and containing special characters other than underscore or dollar sign.
Q: What is the difference between float and double?
A: double provides higher precision (more decimal places) and is the default decimal type. float has limited precision and requires an f suffix on its literals.
This connects to Strings because String is a reference type that behaves differently from primitives (compared with .equals() rather than ==). It also connects to Arrays, which require a declared type for all elements, and to Functions, where understanding how variable assignment works is essential for tracing method calls.
Java identifiers, variable declaration, primitive types, reference types, int, double, float, char, boolean, String, byte, short, long, operator precedence, order of operations, modulus, remainder operator, assignment operator, equality operator, logical AND, logical OR, type mismatch, CSE 110, ASU, Arizona State, intro to Java, Java fundamentals