Command Palette

Search for a command to run...

Phase 1Beginner2 of 17 in Core Java

Variables, Types and Operators

How Java stores values: variables, the eight primitive types, overflow, decimals, casting, every operator, Strings, var, formatted output, input and constants.

Every program, from a calculator to a bank, does the same few things: it keeps values in variables, works on them with operators, and shows results. This phase teaches those building blocks properly, the way the Java compiler and the JVM actually see them.

You'll learn what a byte, an int and a double really are in memory, why Integer.MAX_VALUE + 1 turns negative, why 0.1 + 0.2 prints 0.30000000000000004, when Java converts types for you and when it refuses, and how &&, >>> and % behave in the corner cases interviewers love.

By the end you'll be able to read any line of everyday Java arithmetic and predict its exact output, print neat formatted reports, read input with Scanner, and choose the right type for money, counters, flags and text. Phase 2 then uses these pieces to make decisions and loops.

0/14 · 0%
14 topics ~7 h 57 code blocks & diagrams
Start with the first topic
1
1.1

Variables: Named Boxes for Values

A variable is a named place in memory that holds one value of a fixed type. You declare it once with a type and a name, give it a value before you read it, and can change that value later as long as the new value fits the type.

30 min 4 code practice

2
1.2

The Eight Primitive Types

Java has exactly eight built-in primitive types: four for whole numbers (byte, short, int, long), two for decimals (float, double), one for characters (char) and one for true/false (boolean). Each has a fixed size and range on every computer, which is a big part of why Java programs behave the same everywhere.

28 min 3 code practice

3
1.3

Integer Ranges and Overflow

Java integers have a fixed number of bits, so when a result is too big it silently wraps around to the other end of the range: Integer.MAX_VALUE + 1 is Integer.MIN_VALUE. Knowing how two's complement works lets you predict, detect and prevent overflow bugs.

27 min 3 code practice

4
1.4

Decimals: double, float and Why 0.1 + 0.2 Isn't 0.3

double and float store numbers in binary scientific notation with a fixed number of digits, so most decimal fractions like 0.1 are stored as the nearest binary approximation. That's why 0.1 + 0.2 prints 0.30000000000000004, why you never compare decimals with ==, and why money belongs in BigDecimal.

27 min 3 code practice

5
1.5

char and boolean

A char is a 16-bit number that stands for one character through the Unicode table, so you can do arithmetic on it. A boolean holds only true or false, and unlike in C it is not a number and never converts to or from one.

27 min 4 code practice

6
1.6

Type Conversion and Casting

Java converts a value to a bigger type automatically (widening) because nothing can be lost, but makes you write a cast like (int) to convert to a smaller type (narrowing), because digits or bits may be thrown away. Knowing exactly what a cast keeps and drops, and when Java promotes types inside expressions, explains most 'lossy conversion' errors and many silent bugs.

28 min 4 code practice

7
1.7

Arithmetic Operators, Integer Division and Remainder

Java's arithmetic operators are +, -, *, / and %, plus ++, -- and compound forms like +=. Between integers, / throws away the remainder and % gives it back with the sign of the left operand, and integer division by zero throws an exception while decimal division doesn't.

28 min 4 code practice

8
1.8

Comparison and Logical Operators

Comparison operators (==, !=, <, >, <=, >=) ask a yes/no question and produce a boolean; logical operators (&&, ||, !) combine those answers. && and || short-circuit, skipping the right side when the left already decides the result, which is both an optimisation and a safety tool.

28 min 3 code practice

9
1.9

Bitwise and Shift Operators

Bitwise operators (&, |, ^, ~) work on the individual bits of integers, and shift operators (<<, >>, >>>) slide those bits left or right. They're how Java packs many flags into one number, tests for odd numbers and powers of two in one step, and how HashMap picks a bucket.

29 min 4 code practice

10
1.10

Strings: The Basics

A String is an object that holds a fixed sequence of characters. You create one with double quotes, join Strings with +, read characters by position starting at 0, compare contents with equals (never ==), and every method that seems to change a String actually returns a new one.

31 min 5 code practice

11
1.11

Local Variable Type Inference with var

Since Java 10 you can write var instead of a local variable's type when the initializer already makes the type obvious. The compiler works out the exact type once, at compile time, and the variable keeps that type forever: Java stays statically typed.

30 min 5 code practice

12
1.12

Printing and Formatting Output

System.out.print writes text, println writes text and ends the line, and printf fills a template such as "%-10s %6.2f%n" with values, controlling width, alignment, decimal places and signs. String.format and formatted do the same but give you the String instead of printing it.

29 min 5 code practice

13
1.13

Reading Keyboard Input with Scanner

java.util.Scanner reads text from the keyboard (or from a String or file) and splits it into tokens separated by whitespace, converting each one with nextInt, nextDouble, next or nextLine. Check with hasNextInt before you read, and watch out for the leftover line break after nextInt.

31 min 5 code practice

14
1.14

Constants, final and Literals

A literal is a value written directly in code, such as 42, 0xFF, 3.5f, 'A', true, "hi" or null, and its exact spelling decides its type. Marking a variable final means it can be assigned only once; a static final field with a fixed value is a constant, named in UPPER_SNAKE_CASE.

31 min 5 code practice