Start Learning
Javaneer
Back to stage
Stage 1·Core Language

Programming Basics

Variables, primitive types, operators, and input/output - explained with everyday analogies.

18 min readBeginner
On this page

Now we build real programs. Every program, no matter how complex, is made of a few simple ingredients: values, variables, types, and operators. Master these and you can express almost anything.

Variables: labeled boxes for data

A variable is a named container that stores a value. You give it a name, and you can read or change what's inside.

Variables are labeled boxes

Picture a shelf of labeled boxes. A box labeled age holds the number 25. A box labeled name holds the text "Ada". You can peek inside any box by its label, or swap its contents for something new. That's exactly how variables work.

int age = 25;
String name = "Ada";

System.out.println(name);  // Ada
age = 26;                  // change what's in the box
System.out.println(age);   // 26

Reading the first line: "Create a box named age that holds whole numbers, and put 25 in it." The word int declares the type of data the box can hold.

Primitive types: the basic kinds of data

Java has eight built-in primitive types. As a beginner you'll use these four constantly:

TypeHoldsExample
intWhole numbers42, -7, 0
doubleDecimal numbers3.14, -0.5
booleanTrue or falsetrue, false
charA single character'A', '?'
int score = 100;
double price = 19.99;
boolean isReady = true;
char grade = 'A';

The other four (long, short, byte, float) are variations for specific needs - you'll meet them when you need them.

int vs. double

Use int for things you count (people, items, points) and double for things you measure (prices, temperatures, distances). Dividing two ints throws away the remainder: 7 / 2 is 3, not 3.5! Use double when you need the fraction.

Strings: text

Text is stored in a String (note the capital S). Strings are wrapped in double quotes:

String greeting = "Hello";
String fullName = "Ada Lovelace";

// Join strings together with +
String message = greeting + ", " + fullName + "!";
System.out.println(message);  // Hello, Ada Lovelace!

We'll explore Strings deeply in a later lesson - for now, just know String holds text and + joins strings together.

Operators: doing things with values

Operators combine or compare values.

Arithmetic (math):

int a = 10, b = 3;
System.out.println(a + b);  // 13  (add)
System.out.println(a - b);  // 7   (subtract)
System.out.println(a * b);  // 30  (multiply)
System.out.println(a / b);  // 3   (divide - whole number!)
System.out.println(a % b);  // 1   (remainder / "modulo")

Comparison (produce a boolean):

System.out.println(a > b);   // true
System.out.println(a == b);  // false  (== means "equal to")
System.out.println(a != b);  // true   (!= means "not equal")

= vs. ==

A single = assigns a value (x = 5 puts 5 into x). A double == compares two values (x == 5 asks "is x equal to 5?"). Mixing these up is one of the most common beginner bugs.

Input and output

You already know output: System.out.println(...) prints a line.

For input (reading what a user types), Java provides Scanner:

import java.util.Scanner;

public class Greeter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("What's your name? ");
        String name = scanner.nextLine();
        System.out.println("Nice to meet you, " + name + "!");
    }
}
Output

What's your name? Grace Nice to meet you, Grace!

Quick check

What does the expression 7 / 2 evaluate to in Java?

Key takeaways

  • A variable is a named container for a value; its type declares what it can hold.
  • Common primitives: int (whole numbers), double (decimals), boolean (true/false), char (one character).
  • String holds text; the + operator joins strings together.
  • Arithmetic operators include + - * / and % (remainder); integer division discards the remainder.
  • = assigns a value, while == compares values - don't confuse them.
  • Print with System.out.println; read user input with a Scanner.

Next, we'll learn how to make decisions and repeat actions - the control flow that turns a list of values into real logic.