Courseiva
1Z0-829Chapter 1 of 18Objective 1.1

Java Basics and Object-Oriented Programming Principles

Java is a strongly typed language, meaning every piece of data must have a clearly defined type that cannot change once declared. This concept solves the problem of memory management and data safety, ensuring that programs behave predictably and errors are caught early. For the 1Z0-829 exam, mastering primitives, wrapper classes, operators, type promotion, and casting is essential because these fundamentals appear in nearly every coding question, and one small mistake can cost you the correct answer.

12 min read
Beginner
Updated Jul 23, 2026
Reviewed by Johnson Ajibi· Senior Network & Security Engineer · MSc IT Security

A simple way to picture Java Basics and Object-Oriented Programming Principles

The Kitchen Measurement Analogy

A kitchen measurement cup is a simple tool with a fixed purpose: it holds a specific, unchangeable amount of liquid. You cannot store half a cup of water in a cup that is designed to hold one full cup, unless you use a different cup. In the same way, a primitive variable in Java is a container that holds exactly one kind of data, like a whole number or a single character, and its size is fixed by the type of data it stores.

When you are baking a cake, you might need to convert measurements. A recipe calls for 250 millilitres of milk, but your measuring jug only shows fluid ounces. You must convert the amount, carefully calculating the new value without spilling or losing any liquid. This conversion is like casting in Java: you take a value from one type of container and put it into another container that might hold more or less. If you pour a full litre of water into a half-litre cup, you lose the excess — that is a narrowing conversion with potential data loss.

Every day, a cook uses different cups for different ingredients: a teaspoon for vanilla extract, a tablespoon for oil, and a cup for flour. Each cup is a primitive type. But a recipe might also call for a "packet" of sugar, which is a wrapper — an object that can hold the sugar and also tell you its expiry date, brand, and nutritional information. The wrapper class, like Integer or Double, gives the simple primitive value extra abilities, such as methods to convert it to a string or compare it to another value.

How It Actually Works

In Java, data comes in two main flavours: primitives and objects. A primitive is the simplest kind of data — think of it as a raw, atomic value that lives directly in memory. Java has eight primitive types: byte, short, int, long, float, double, char, and boolean. Each has a fixed size and a range of values it can hold. For example, an int uses 32 bits and can store whole numbers from about -2.1 billion to 2.1 billion. A double uses 64 bits and can hold decimal numbers with up to 15 significant digits.

A wrapper class is an object that wraps a primitive value inside it. For every primitive, there is a corresponding wrapper class: Integer for int, Double for double, Boolean for boolean, and so on. Wrapper classes are useful because they allow primitives to be used in contexts where only objects are allowed, such as in collections like ArrayList. They also provide useful utility methods, like Integer.parseInt() which converts a string like "123" into an int 123.

Operators are symbols that perform actions on data. The most common are arithmetic operators (+, -, *, /, %), which work with numeric primitives. The assignment operator (=) stores a value into a variable. Comparison operators (==, !=, <, >, <=, >=) compare two values and return a boolean. Logical operators (&&, ||, !) combine boolean expressions. Parentheses () change the order of evaluation, just like in mathematics.

Type promotion happens automatically when Java converts a smaller data type into a larger one during an operation. For example, if you add an int and a double, Java promotes the int to a double before performing the addition, because a double can hold the full range of an int and more precision. This is safe and automatic — no data is lost.

Casting is the manual conversion of a value from one type to another. You write the target type in parentheses before the value, like (int) 3.14. This is necessary when you want to store a larger type into a smaller one — a narrowing conversion. For example, storing a double into an int requires casting, and you will lose the fractional part. Casting between primitive types and wrapper classes happens automatically through autoboxing (Java converts int to Integer automatically) and unboxing (Java converts Integer to int automatically), but only in specific contexts.

Why does all this matter? The Java compiler checks types strictly. If you try to assign a double to an int without casting, the code will not compile. This strictness prevents bugs where data might be misinterpreted. For the exam, you need to predict the result of expressions involving mixed types, know when casting is required, and understand how wrapper classes behave with operators (for example, comparing two Integer objects with == can be tricky because it compares references, not values).

This flowchart shows the eight primitive types on the left, their corresponding wrapper classes on the right, and the autoboxing/unboxing relationship between int and Integer as an example.

Walk-Through

1

Declaring a Primitive Variable

You choose a primitive type based on the kind of data you need to store. For a small whole number, use byte or short. For a typical integer, use int. For a decimal, use double. For a single character, use char. For true/false values, use boolean. This step determines the memory allocation and the range of valid values.

2

Assigning a Value with Assignment Operator

Use the = operator to store a literal value, another variable, or the result of an expression into the variable. The value's type must be compatible with the variable's type. If it is a narrower type, automatic widening may occur. If it is wider, you must cast.

3

Performing Arithmetic Operations

When you use operators like +, -, *, /, or %, the operands and the result follow type promotion rules. For example, mixing an int and a double promotes the int to double. The result type is the promoted type. This step is where many exam traps hide.

4

Casting to a Different Type

When you need to store a value into a variable of a smaller or incompatible type, you write the target type in parentheses before the expression. Example: int x = (int) 3.14;. This truncates the decimal. You are responsible for ensuring the value fits to avoid unexpected results.

5

Using Wrapper Classes and Autoboxing

If you need to put a primitive into a data structure that only accepts objects, Java automatically 'boxes' the primitive into its wrapper class. When retrieving, Java 'unboxes' it back to a primitive. This step is seamless but can throw NullPointerException if the wrapper variable is null.

What This Looks Like on the Job

An IT professional building a financial application for a bank needs to handle money with absolute precision. They cannot use double or float because those types have rounding errors — imagine losing a penny on every transaction because of floating-point imprecision. Instead, they use the BigDecimal class, but even then, they must understand how primitives and wrappers interact so they do not accidentally convert a precise value into an imprecise one.

Step by step, the developer writes a method that calculates interest. The input comes from a user interface as a String, such as "5.75". They parse it into a double using Double.parseDouble(), but they know that for financial calculations, they should convert it to BigDecimal. They must cast it and be aware of the precision loss.

Later, they need to store the account balance in a database column defined as an integer representing cents. They take the user-entered amount in dollars, multiply by 100, and cast from double to int. Without the cast, the compiler would refuse. If the value is too large for an int, the cast silently truncates it, which could cause a serious billing error. The developer therefore adds boundary checks.

When logging transactions, the developer uses wrapper classes to store values in an ArrayList of Objects. Autoboxing converts each primitive int into an Integer automatically when adding to the list. When retrieving, unboxing converts it back. But if the list contains null, unboxing throws a NullPointerException, which must be handled.

Operator precedence rules matter in complex expressions. For example, the formula for compound interest involves multiple arithmetic operations. The developer uses parentheses to make the order explicit, ensuring the calculation is correct and readable. One misplaced parenthesis could change the entire result. - They test with edge cases: zero interest, negative rates, maximum integer values. - They use the modulus operator (%) to check if a transaction amount is a whole number of cents. - They rely on type promotion to ensure that multiplication of two int values that could overflow is handled by promoting to long.

This daily work demands a deep, instinctual understanding of how types behave, because one cast error can bring down a whole system or cost the bank millions.

How 1Z0-829 Actually Tests This

The 1Z0-829 exam aggressively tests your understanding of primitive types, wrapper classes, and casting. Expect at least 3-5 questions directly on these topics in the first section of the exam. The questions often present a short code snippet and ask you to determine the output or whether the code compiles.

Key traps include:

Comparing Integer objects with ==. Since Integer values between -128 and 127 are cached, two Integer objects with the same value in that range will return true with ==. Outside that range, they return false because they are different objects. The correct way to compare Integer objects is with .equals().

Widening vs. narrowing conversions. The compiler allows widening automatically (e.g., byte to int) but requires explicit casting for narrowing (e.g., int to byte). You must know exactly which conversions are implicit and which are not.

Binary numeric promotion. When two operands of different types are combined, one is promoted. The rules are: if either operand is double, the other becomes double; else if float, the other becomes float; else if long, the other becomes long; else both become int. This is a favourite exam topic.

Unary operators like ++ and -- and their prefix vs. postfix placement. The difference in timing of the increment is a classic exam trick.

Compound assignment operators like +=, -=, *= implicitly cast the result to the left-hand variable's type. For example, int x = 5; x += 3.5; compiles and sets x to 8 (the fractional part is discarded). But x = x + 3.5; does not compile because x + 3.5 is a double.

Questions about the Math class rounding methods like round(), ceil(), floor() often appear alongside casting, because rounding a double to an int is essentially a controlled narrowing conversion.

To pass, memorise the exact range and size of each primitive type, especially byte (8 bits, -128 to 127), short (16 bits, -32768 to 32767), int (32 bits, about ±2.1 billion), and long (64 bits). Know the wrapper class names: Integer, Short, Byte, Long, Float, Double, Character, Boolean. - Practise predicting the result of expressions like: long x = 5; int y = 2; System.out.println(x / y); (output is 2, because 5/2 is integer division, then promoted to long). - Practise with the ternary operator, which involves type promotion rules. - Practise with null pointers from unboxing null wrapper objects.

Key Takeaways

Java has eight primitive types: byte, short, int, long, float, double, char, and boolean, each with a fixed size and value range.

Wrapper classes (e.g., Integer, Double) let you use primitives as objects, with utility methods and compatibility with collections like ArrayList.

Widening conversions (e.g., int to long) happen automatically, but narrowing conversions (e.g., long to int) require explicit casting and risk data loss.

Binary numeric promotion always results in at least an int for arithmetic operations on byte, short, or char operands.

Comparing wrapper objects with == is unreliable; use the .equals() method for value comparison.

Compound assignment operators like += include an implicit cast, so they compile even when the equivalent expanded expression would not.

Easy to Mix Up

These come up on the exam all the time. Here's how to tell them apart.

int (primitive)

Cannot be null; always has a default value of 0.

Stored on the stack for local variables; faster to use.

Cannot use methods like toString() or compareTo() directly.

Integer (wrapper class)

Can be null, which can cause NullPointerException on unboxing.

Stored as an object on the heap; slightly slower.

Provides utility methods like parseInt(), valueOf(), and compareTo().

Automatic widening conversion

Happens without any special syntax; the compiler allows it.

Always safe because the target type can hold all values of the source.

Example: assigning int to long is fine; Java does it silently.

Explicit narrowing conversion (casting)

Requires the cast operator: (type) value.

Can lose data or precision if the source value is too large for the target.

Example: assigning long to int needs (int) and may truncate.

Prefix increment (++x)

Increments the variable first, then returns the new value.

The result of the expression is the incremented value.

Example: int x = 5; int y = ++x; results in x=6, y=6.

Postfix increment (x++)

Returns the original value first, then increments the variable.

The result of the expression is the value before incrementing.

Example: int x = 5; int y = x++; results in x=6, y=5.

Watch Out for These

Mistake

I can use == to compare any two Integer objects safely.

Correct

The == operator compares object references, not values, for Integer objects outside the cached range (-128 to 127). Always use .equals() to compare wrapper objects.

This mistake is common because == works correctly for small numbers due to caching, giving a false sense of security.

Mistake

Casting a double to an int rounds the value to the nearest integer.

Correct

Casting a double to an int truncates the decimal part — it simply discards everything after the decimal point, not rounds. 3.99 becomes 3, not 4.

People confuse casting with the Math.round() method, which does round. The word 'cast' sounds like it might do something smarter.

Mistake

Autoboxing and unboxing happen everywhere automatically.

Correct

Autoboxing and unboxing only happen in specific contexts: assignment, method arguments, and arithmetic operations with wrappers. They do not happen in array initialisation or when passing a primitive to a method that expects a primitive (no conversion needed).

Beginners assume the compiler handles all conversions without exception, but the rules are context-specific.

Mistake

The expression short + short will produce a short.

Correct

In Java, arithmetic operations on byte, short, or char always promote the operands to int, so the result is an int. You must cast it back to short if you want a short result.

This violates the expectation that operations between two same-type values keep that type, but Java's design promotes to int to avoid overflow.

Mistake

A boolean can be converted to an int using casting.

Correct

boolean is not compatible with numeric types in Java. You cannot cast boolean to int or any other primitive. Use a conditional expression instead.

In some languages like C, booleans are integers (0 and 1). Java is strict about type safety, so this conversion is forbidden.

Do You Actually Know This?

Reveal each answer, then mark whether you got it right. Score 60%+ to unlock the next chapter.

Frequently Asked Questions

Can I convert a String to an int without losing data?

Yes, use Integer.parseInt() or Integer.valueOf(). But if the string contains non-numeric characters or is out of the int range, it throws a NumberFormatException.

What happens if I cast a very large double to an int?

The result will be the maximum or minimum value for an int (2147483647 or -2147483648) if the double is outside the int range, plus a potential overflow. The conversion truncates the fractional part first.

Why does Integer.parseInt("10") work but Integer.parseInt("10.5") does not?

parseInt() expects a string containing only digits (and an optional sign). A decimal point is not valid for an integer, so it throws NumberFormatException. Use Double.parseDouble() for decimals.

How do I know when I need to cast in Java?

You need to cast when you are assigning a value of a wider type to a variable of a narrower type, or when you want to convert between incompatible types (though boolean is entirely incompatible with numerics). The compiler will tell you with a compile error.

Is there any difference between int and Integer?

Yes. int is a primitive, stored directly in memory and cannot be null. Integer is an object, can be null, and provides methods like toString() and compareTo(). Use int for performance-critical parts and Integer when you need an object reference.

What does the % operator do with floating-point numbers?

The modulus operator works with floating-point types too, returning the remainder after division. For example, 5.5 % 2.0 yields 1.5. This is useful for periodic calculations or checking divisibility.

Terms Worth Knowing

Keep going

You've finished Java Basics and Object-Oriented Programming Principles. Continue through the 1Z0-829 study guide to build a complete picture of the exam.

Done with this chapter?