How do you make Java remember information like a user's name, a price, or whether a light switch is on or off? That's exactly the problem that declaring and initialising variables solves. For the 1Z0-829 exam, you need to master this skill because every Java program — from a simple calculator to a massive banking system — relies on variables to hold and manipulate data.
Jump to a section
A simple way to picture Working with Java Data Types
Ever stood in front of a stack of plastic containers, trying to decide which one to use for your leftover soup? You've got tiny ones for spices, medium ones for sandwiches, and giant ones for big batches of stew. Each container is designed for a specific purpose and can hold a certain type of thing.
In Java, variables work the same way. A variable is like a labelled storage container. When you declare a variable, you're choosing the container and giving it a name label. The 'type' of the variable (like 'int' or 'String') determines what size the container is and what kind of stuff it can hold — numbers, text, or complex objects. A tiny 'int' container can hold a whole number up to about 2 billion, but it will spill if you try to put a fraction or a huge number in it. A 'String' container can hold words, but it's useless for maths.
The key skill is picking the right container for your job. If you're storing someone's age, you use an 'int' (whole number, small). If you're storing their name, you use a 'String' (text). If you're storing a bank account balance, you might use a 'double' (decimal number). And sometimes, instead of being super specific about the container type, Java lets you say 'I'll figure out the right container later' — that's the 'var' keyword, which is like saying 'give me an appropriate container based on what I'm putting in it right now'.
When you write a Java program, you need the computer to store pieces of information. Maybe it's the temperature outside, the name of a customer, or the result of a calculation. A variable is a named storage location in memory that can hold a value. Think of it as a labelled box where you keep data.
To use a variable, you need to do two things: declare it and initialise it. Declaration is telling Java: 'Hey, I want a storage container with this name and this type.' Initialisation is the first time you actually put a value into that container.
Here's the basic syntax:
double price = 19.99;
The first part, 'double', is the data type. It tells Java what kind of information the container can hold. The second part, 'price', is the variable name — the label on your container. The equals sign is the assignment operator; it means 'put this value into the container'. The '19.99' is the value itself. The semicolon ends the statement.
Java has two broad categories of data type: primitive types and reference types.
Primitive types are the most basic building blocks. They hold simple values directly. There are eight primitive types:
byte: holds a very small whole number ( -128 to 127 ). Uses very little memory.
short: holds a small whole number ( -32,768 to 32,767 ).
int: the default whole number type. Holds about -2.1 billion to +2.1 billion.
long: for when int isn't big enough. Use an 'L' suffix, like 10000000000L.
float: holds fractional numbers with about 6-7 decimal digits of precision. Use an 'f' suffix, like 3.14f.
double: the default decimal type. Holds about 15 decimal digits of precision.
char: holds a single character, like 'A', 'z', or '9'. Uses single quotes.
boolean: holds either true or false. Ideal for yes/no decisions.
Reference types, on the other hand, store a 'reference' or 'address' to where the actual data lives in memory — not the data itself. They point to objects. The most common reference type is 'String', which holds text. Other reference types include arrays and any classes you or the Java libraries create. When you write 'String name = "Alice";', the variable 'name' doesn't contain the letters 'Alice' directly; it contains a memory address that leads to an object containing those letters.
Now, here's a crucial rule: local variables — variables declared inside a method — are NOT given a default value by Java. You MUST initialise them before you use them. If you try to use an uninitialised local variable, the compiler will give you a 'variable might not have been initialized' error. This is a common exam trap.
Instance variables (outside a method but inside a class) and static variables are automatically given default values. For numbers, the default is 0 (or 0.0 for decimal types). For boolean, it's false. For reference types, it's null.
Finally, there's 'var'. Introduced in Java 10, 'var' is a special keyword for local variable type inference. It tells the compiler: 'Look at the value I'm assigning, and figure out the data type for me.' So instead of writing 'int items = 5;', you can write 'var items = 5;'. The compiler sees the '5', knows it's an int, and treats 'items' as an int variable. 'var' can only be used for local variables inside methods — not for instance variables, method parameters, or return types. You must also provide an initialiser (a value on the right side of the equals sign) when using 'var'. This is a favourite exam topic.
Identify the information you need to store
Before writing any code, decide what data your program needs. Is it a person's age (whole number), a price (decimal), a name (text), or a yes/no status (boolean)? This step is crucial because it determines which data type you will declare.
Choose the correct data type
For whole numbers, pick byte, short, int, or long depending on the range. For decimals, use float or double (double is the default). For a single character, use char. For true/false, use boolean. For text, use String. For other objects, use the appropriate reference type (e.g., LocalDate for a date). Choosing the wrong type can cause compilation errors or produce incorrect maths.
Write the declaration statement
Write the data type followed by the variable name and a semicolon. For example: 'int age;'. This tells Java to set aside memory space for an integer and label it 'age'. At this point, the variable has no meaningful value (if it's a local variable). If you want to use 'var', you must combine declaration with initialisation: 'var age = 25;'.
Initialise the variable with a value
Use the assignment operator '=' to give the variable its first value: 'age = 30;'. This is called initialisation. You can also declare and initialise in one step: 'int age = 30;'. For local variables, this step is mandatory before you can read the variable. For instance variables, initialisation is optional because defaults are provided.
Use the variable in your code
Now you can read the variable's value in expressions, pass it to methods, or print it. For example: 'System.out.println("Age: " + age);'. Remember that if you are concatenating a number with a String, Java automatically converts the number to its String representation. This step is where the variable fulfils its purpose in your program.
Reassign or update the variable as needed
You can change the value stored in an existing variable by using the assignment operator again without re-declaring the type: 'age = 31;'. This overwrites the previous value. This step demonstrates that a variable's content can vary over time, which is why it's called a 'variable'. You cannot change the variable's type after declaration.
Imagine you're building the checkout system for an online supermarket. A real IT professional would use Java data types to model all the different pieces of information in the system.
First, you need to store the name of each product. You'd use a 'String' variable: 'String productName = "Organic Almond Milk";'. You need the price — a decimal number, so 'double price = 3.99;'. The quantity the customer wants is a whole number, so 'int quantity = 2;'. Is the product in stock? That's a yes/no question, perfect for 'boolean inStock = true;'.
Now, you need to calculate the subtotal. You multiply price by quantity. But what type should the result be? If you multiply a double by an int, Java automatically 'promotes' the int to a double, and the result is a double. This is called type conversion. Your code might look like:
double subtotal = price * quantity;
Next, you apply a discount. The discount might be stored as a 'double discountPercent = 0.10;' (representing 10%). You calculate the discount amount: 'double discountAmount = subtotal * discountPercent;'. Then the final total: 'double total = subtotal - discountAmount;'.
But what if you need to display the total to the user? You might need to combine it with text. That's where String concatenation comes in. You could write:
System.out.println("Your total is: $" + total);
Notice that 'total' is a double, but when you concatenate it with Strings using the '+' operator, Java automatically converts the double to a String representation.
A professional would also handle potential issues. For example, if you're dealing with financial calculations, using 'double' can lead to tiny rounding errors (which is why systems that handle money often use 'BigDecimal' instead, but that's beyond this chapter). They would also ensure variables are initialised properly. If 'quantity' somehow got used without being assigned a value, the program would crash — so they'd always initialise it, maybe to 0, before using it in a calculation.
Finally, they might use 'var' for local variables to make their code cleaner. Instead of writing:
List<Product> shoppingCart = new ArrayList<>();
They could write:
var shoppingCart = new ArrayList<Product>();
The compiler infers the type, and the code is easier to read and modify.
The 1Z0-829 exam tests your understanding of variable declaration, initialisation, and 'var' in several question formats. You will see multiple-choice questions, often with more than one correct answer (select-all-that-apply), and drag-and-drop or code-completion tasks.
Here are the exact concepts they love to test:
Local variable initialisation: 90% of the traps involve local variables. The exam loves to show you code where a local variable is declared but not initialised, then used in a condition or output. You must recognise that this will cause a compile-time error. For instance:
int x; if (true) { x = 5; } System.out.println(x); // This compiles because the compiler sees that x is always assigned.
But:
int x; if (someCondition) { x = 5; } System.out.println(x); // This will NOT compile because the compiler can't guarantee x is assigned in all paths. - Default values for instance/static variables: They will ask, 'What is the default value of an int field?' The answer is 0. For a boolean field, it's false. For a String field (reference type), it's null. They may ask this in the context of an array of objects, where each element is automatically null. - Valid and invalid uses of 'var': The exam will present several statements using 'var' and ask which ones compile. Remember: - 'var' can only be used for local variables (inside a method, constructor, or initializer block). - You must provide an initialiser on the same line: 'var x = 10;' is fine; 'var x;' alone is not. - You cannot use 'var' for method parameters, method return types, or instance variables. - You cannot assign 'null' directly with var unless there's enough context for the compiler to infer the type: 'var x = null;' will not compile because null can be any reference type. - You can use 'var' in a for-each loop: 'for (var item : list)' is allowed. - Data type ranges and literals: They will test whether you know that 'long x = 1234567890123;' doesn't compile because the literal is too large for an int (the default literal type). You must add an 'L': '1234567890123L'. Similarly, 'float f = 3.14;' fails because 3.14 is a double literal by default; you need '3.14f'. - The 'uninitialized variable' trap when using 'var': 'var x;' followed by 'x = 5;' on the next line is illegal. The initialiser must be present at declaration. - Multiple declarations on one line: 'int a, b, c = 5;' — only 'c' is initialised to 5; 'a' and 'b' are declared but not initialised. The exam tests this subtlety. - Char and int compatibility: 'char' can hold a number (its Unicode value), and you can assign a char to an int. But assigning an int to a char without casting is a compilation error because it's a narrowing conversion.
Variables must be declared with a type (or 'var' for local type inference) before they can be used.
Local variables must be explicitly initialised before use, or your code will not compile.
Instance and static variables get default values: numeric types default to 0, boolean defaults to false, and reference types default to null.
The 'var' keyword can only be used for local variables and requires an initialiser on the same line to infer the type.
Primitive types store actual values directly in memory, while reference types store a memory address pointing to the object.
There are eight primitive data types in Java: byte, short, int, long, float, double, char, and boolean.
A 'long' literal requires an 'L' suffix, and a 'float' literal requires an 'f' suffix, or the code will not compile.
You cannot use 'var' for method parameters, return types, or instance variables — only for local variables inside methods.
These come up on the exam all the time. Here's how to tell them apart.
Primitive Types
Store the actual value directly in memory (e.g., int holds the number 5).
Eight fixed types: byte, short, int, long, float, double, char, boolean.
Default values for instance variables: 0, 0.0, false, or \u0000 (for char).
Reference Types
Store a memory address (reference) pointing to where the object is kept in memory.
Unlimited number of types, including String, arrays, and custom classes.
Default value for instance variables is always null (no object referenced).
Local Variables
Declared inside a method, constructor, or block.
Must be explicitly initialised before use; no default values.
Can use 'var' for type inference.
Instance Variables
Declared inside a class but outside any method.
Given a default value if not explicitly initialised (0, false, or null).
Cannot use 'var'; type must be explicitly declared.
int Data Type
Holds whole numbers from -2,147,483,648 to 2,147,483,647.
The default type for integer literals (e.g., 100 is an int).
Uses 32 bits (4 bytes) of memory.
long Data Type
Holds whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Requires an 'L' or 'l' suffix on literals (e.g., 100L).
Uses 64 bits (8 bytes) of memory.
double Data Type
Holds decimal numbers with about 15 digits of precision.
The default type for decimal literals (e.g., 3.14 is a double).
Uses 64 bits (8 bytes) of memory.
float Data Type
Holds decimal numbers with about 6-7 digits of precision.
Requires an 'f' or 'F' suffix on literals (e.g., 3.14f).
Uses 32 bits (4 bytes) of memory.
Mistake
All variables in Java are automatically given a default value of 0, false, or null, even local variables inside methods.
Correct
Only instance variables (fields) and static variables get default values. Local variables inside methods must be explicitly initialised before use, or the code will not compile.
Beginners often generalise from seeing default values for class-level fields and assume it applies everywhere. The concept of 'definite assignment' (the compiler checking that every local variable is assigned before use) is not obvious until you hit the compile error.
Mistake
Using 'var' makes the variable dynamically typed, like JavaScript, so you can change its type later by assigning a different kind of value.
Correct
'var' is still statically typed. The compiler infers the type from the initialiser, and that type is fixed for the variable's lifetime. var x = 10; makes x an int forever. You cannot later assign x = "hello";.
The word 'inference' sounds flexible, and beginners familiar with loosely-typed languages assume Java's var works the same way. The exam explicitly tests this with code that tries to reassign a var variable with a different type.
Mistake
A 'String' variable is a primitive data type because it stores a simple value like text.
Correct
String is a reference type. String variables store a reference (memory address) to an object, not the text itself. Primitives store the actual value directly in memory. This is why two String variables can point to the same object, and why comparing Strings with '==' compares references, not text content.
String looks simple to use (like a primitive), and many tutorials introduce it alongside int and double without clearly explaining its reference nature. The misconception persists until the == vs .equals() trap appears.
Mistake
Declaring 'int x = 5;' then later writing 'x = 10;' is called 're-declaring' the variable.
Correct
This is called 'reassigning' the variable. You are changing the value stored in the existing variable. Re-declaration means trying to declare a second variable with the same name in the same scope, which is illegal. 'int x = 10;' a second time will cause a compile error.
The terms 'declare' and 'assign' sound similar. Beginners often use them interchangeably, and the exam sets traps with code that tries to re-declare a variable in the same block.
Reveal each answer, then mark whether you got it right. Score 60%+ to unlock the next chapter.
It means you declared a local variable without giving it a value, and then you tried to use it (e.g., in a print statement or calculation). Java requires every local variable to be definitely assigned before it is read. The compiler catches this as an error to prevent unpredictable behaviour.
No. You must initialise the variable with a value when you declare it with var. If you write 'var x = null;', the compiler cannot infer the type because null can be assigned to any reference type. This causes a compilation error. You would need to declare the specific type instead.
Both are functionally identical after compilation. In the first case, you explicitly tell Java the type is int. In the second case, Java infers the type from the value 5 (which is an int literal). The compiled bytecode is the same. 'var' is just a shorthand for the developer's convenience.
Because integer literals without a suffix are treated as 'int' values by default. The number 1,234,567,890,123 is larger than the maximum value an int can hold (about 2.1 billion), so the compiler rejects it. Adding the 'L' suffix (e.g., '1234567890123L') tells Java it's a long literal.
You use double quotes: 'String greeting = "Hello, World!";'. Unlike char (which uses single quotes and holds exactly one character), String can hold zero or more characters. String is a reference type, so the variable stores a memory address pointing to the object that contains the text.
Yes. You can write 'int a, b, c = 5;'. This declares three int variables named a, b, and c. Only c is initialised to 5. Variables a and b are declared but not initialised. You can also chain assignments: 'int a, b, c; a = b = c = 10;' — this initialises all three to 10.
You've finished Working with Java Data Types. Continue through the 1Z0-829 study guide to build a complete picture of the exam.
Done with this chapter?