Source: Final Exam Notes, Computer Science and Engineering, Arizona State University
Difficulty: Beginner | Prerequisites: None, though familiarity with any programming language helps.
This material covers the absolute foundations of writing Java: how you name things, what types of data exist, how you store values in variables, and how you combine them with operators. Every line of Java you ever write will use these building blocks, so this is not a topic you can skim and revisit later. If you are arriving from Python or JavaScript, the key shift is that Java is statically typed, meaning you must declare a variable's type before you use it. These notes map directly onto the earliest weeks of an introductory CSE course at ASU.
Java has strict rules for naming identifiers and eight primitive data types (byte, short, int, long, float, double, char, boolean). Variables must be declared with a type before use, and operators follow standard arithmetic rules with a few Java-specific quirks around integer division and compound assignment.
Identifier
A name given to a variable, method, class, or other entity in Java. Must start with a letter, underscore, or dollar sign, followed by letters, digits, underscores, or dollar signs. Cannot be a reserved word.
In simple terms, this is the name you choose for the things in your code.
Primitive data type
One of eight built-in types in Java: byte, short, int, long, float, double, char, boolean. These store simple values directly in memory rather than references to objects.
Think of it as the basic building blocks for storing numbers, characters, and true/false values.
Variable declaration
A statement that creates a new variable by specifying its type and name, e.g. int numDogs;.
In simple terms, you are telling Java "I need a box of this size, and here is its label."
Variable initialisation
Assigning a value to a variable at the point of declaration, e.g. int numDogs = 0;.
Think of it as labelling the box and putting something inside it straight away.
Literal
A specific, fixed value written directly in code, such as 2, 3.14, 'a', or true.
In simple terms, a literal is a hard-coded value, not something computed or looked up.
Compound operator
A shorthand operator that combines an arithmetic operation with assignment, e.g. /=, +=, -=, *=.
Think of it as saying "take what is already there, do this to it, and put the result back."
Scientific notation (in Java)
A way to express very large or very small numbers using e syntax: 1.0e-3 equals 0.001, and 6.02e23 equals 6.02 x 10^23.
In simple terms, the e means "times ten to the power of."
Reserved word
A word that Java has claimed for its own syntax (e.g. int, class, return, if). You cannot use a reserved word as an identifier.
In simple terms, these names are already taken by the language itself.
Widening cast
An automatic type conversion from a smaller type to a larger type, e.g. int to double. No data is lost.
Think of it as moving from a smaller box to a bigger one: everything fits.
Narrowing cast
An explicit type conversion from a larger type to a smaller type, e.g. double to int. May lose data (the decimal portion is truncated).
Think of it as forcing something into a smaller box: you might lose what does not fit.
Must begin with a letter, underscore (_), or dollar sign ($)
After the first character, digits (0-9) are also allowed
Cannot be a Java reserved word (e.g. class, int, void)
Cannot contain spaces
Case-sensitive: numPeople and NumPeople are different identifiers
Valid examples: x = y + 2 is a valid expression. x + 1 = 3 is invalid (you cannot assign to an expression). x + y = y + x is invalid for the same reason.
Java's primitives fall into four categories:
Integer types (whole numbers): byte, short, int, long
Floating-point types (decimals): float, double
Character type: char (a single character, e.g. 'A')
Boolean type: boolean (true or false)
Choosing the right type matters. Use int for most whole numbers, double for most decimals. Use float only when memory is tight, and remember that float requires an f suffix on literals (e.g. 3.14f).
The average number of kids per household would be a double, because it will have a fractional part.
Money is typically stored as an int (for pence/cents) to avoid floating-point rounding errors.
Declare an integer variable: int numPeople;
Assign a value: numCars = 99; (the variable must already be declared)
Declare and initialise together: int numDogs = 0;
Declare a double: double personHeight;
Compound operators combine arithmetic with assignment:
carCount = carCount / 2; becomes carCount /= 2;
numItems = boxCount + 1; cannot be rewritten with a compound operator, because the left-hand side (numItems) is not the same as the variable being operated on (boxCount)
13.0 / 3.0 = 4.33333... (floating-point division preserves the decimal)
0.0 / 5.0 = 0.0
12.0 / 0.0 = +Infinity (not an error in Java for floating-point types)
0.0 / 0.0 = NaN (Not a Number)
4 / 9 = 0 (integer division truncates the decimal)
100 % 0 = undefined (throws an ArithmeticException)
Infinity and NaN are special float and double values. Integer division by zero throws an exception.
Get the last digit of 693: 693 % 10 yields 3
Get the last four digits of a number: x % 10000
Generate a random number between 5 and 10: (x % 6) + 5
Generate a number between -10 and 10: (x % 21) - 10
1 x 10^-3 is written as 1.0e-3
6.02 x 10^23 is written as 6.02e23
System.out.printf("%.1f", 0.125); outputs 0.1
System.out.printf("%.3f", 9.1357); outputs 9.136 (rounds up)
System.out.printf("%.2f", 7.0/3.0); outputs 1.33 (actually this outputs 2.33)
The format specifier % followed by width and precision controls how numbers are displayed. %-10s left-aligns a string in a field 10 characters wide. %10.2f right-aligns a float with 2 decimal places in a field 10 characters wide.
Declaration syntax: type variableName; e.g. int numPeople;
Declaration with initialisation: type variableName = value; e.g. int numDogs = 0;
printf format: System.out.printf("formatString", value);
Common format specifiers: %d (integer), %f (float/double), %s (string), %e (scientific notation)
Precision control: %.2f (2 decimal places), %10.2f (10-wide field, 2 decimal places)
Understanding data types is not just academic. Financial software uses specific types to avoid rounding errors in currency calculations. Game engines choose between float and double to balance precision against performance. The modulo operator turns up everywhere from generating PINs to distributing work across servers.
Students often think 4 / 9 gives 0.44 in Java. It does not. Because both operands are integers, Java performs integer division and returns 0. To get the decimal result, at least one operand must be a floating-point type: 4.0 / 9.
Students assume you can use a compound operator whenever two variables are involved. You cannot. numItems /= boxCount only works if you want to divide numItems by boxCount and store the result back in numItems. You cannot use a compound operator to assign to a different variable.
Students forget that float literals require the f suffix. Writing 3.14 gives you a double, not a float. Writing float x = 3.14; will not compile without the cast or suffix.
Students confuse = (assignment) with == (comparison). x = 5 stores the value 5 in x. x == 5 checks whether x equals 5.
⚠️ Integer division truncation is a classic exam question. Know that 4 / 9 = 0 and why.
⚠️ Expect questions asking you to identify valid vs invalid identifiers.
⚠️ Know the difference between declaring, initialising, and assigning a variable.
⚠️ Be ready to convert a standard assignment to a compound operator, and to explain when that is not possible.
⚠️ printf formatting (field width, precision, alignment) is commonly tested. Practice reading format strings.
True or false: 2name is a valid Java identifier. (False, identifiers cannot start with a digit.)
Fill in the blank: Java has ___ primitive data types. (Eight.)
True or false: 12.0 / 0.0 throws an exception in Java. (False, it returns positive Infinity.)
Fill in the blank: The compound operator form of x = x * 3; is ___. (x *= 3;)
True or false: String is a primitive type in Java. (False, String is a reference type, indicated by its capital S.)
Q: Declare an integer variable named numPeople.
A: int numPeople;
Q: Write an assignment statement to assign numCars with 99.
A: numCars = 99;
Q: Declare an integer variable named numDogs, initialising the variable to 0 in the declaration.
A: int numDogs = 0;
Q: Rewrite carCount = carCount / 2; using a compound operator.
A: carCount /= 2;
Q: What is the result of 13.0 / 3.0 in Java?
A: 4.33333... (floating-point division preserves the fractional part)
Q: What is the result of 4 / 9 in Java?
A: 0 (integer division truncates the decimal)
Q: What does System.out.printf("%.2f", 7.0/3.0); output?
A: 2.33 (the result is 2.3333..., rounded to two decimal places)
Q: How would you get the last digit of the number 693 in Java?
A: 693 % 10, which yields 3.
This material connects directly to control flow (if-else, loops), because conditional expressions rely on boolean values and comparison operators covered here. It also underpins Strings and I/O, since reading user input with Scanner requires you to call the correct method for each data type (nextInt(), nextDouble(), etc.). Understanding type casting is essential when you begin working with mixed-type arithmetic in more complex programs.
Java data types, primitive types, int, double, float, boolean, char, byte, short, long, variable declaration, variable initialisation, assignment statement, compound operator, compound assignment, modulo operator, remainder operator, integer division, floating-point division, printf, format specifier, scientific notation, identifier rules, reserved words, Java keywords, widening cast, narrowing cast, type conversion, type casting, literal, NaN, Infinity, ASU CSE, introductory Java, Java fundamentals