Introduction & Syntax
Java Basics, Syntax and Data Types · Study material
Java is one of the most widely used programming languages in the world: Android apps, banking and insurance systems, e-commerce back ends, big-data tools like Hadoop and Kafka, and the Spring Boot microservices that most product companies and IT services firms run on. It is also the language of choice for many college courses, campus placement tests and coding interviews. This first topic builds the foundation everything else rests on: how a Java program is compiled and run, the structure of a program, the eight primitive data types, literals, variables and var, constants, type casting, arithmetic rules and formatted output. Then test yourself with 60 practice questions – 20 MCQs, 20 predict-the-output puzzles, 10 find-the-error programs and 10 complete programs – every one compiled and run on Java 21.
On this page
- How Java runs: source → bytecode → JVM (JDK, JRE, JVM)
- Structure of a Java program and the main method
- The eight primitive types: sizes, ranges, defaults
- Literals, variables, var and final constants
- Type casting, promotion, overflow and division rules
- String concatenation and printf formatting
How a Java program runs
You write Main.java; the compiler javac turns it into bytecode (Main.class); the Java Virtual Machine (java Main) loads and runs that bytecode, interpreting it and compiling hot code to native machine code (JIT). Because the bytecode is the same everywhere, the same .class file runs on Windows, Linux and macOS – "write once, run anywhere". Since Java 11 you can also run a single file directly with java Main.java.
| Term | What it is |
|---|---|
| JVM | Java Virtual Machine – runs bytecode, manages memory (garbage collection), does JIT compilation |
| JRE | Java Runtime Environment – the JVM plus the standard class libraries needed to run programs |
| JDK | Java Development Kit – the JRE plus tools to develop: javac, jar, javadoc, jshell, debugger |
| Bytecode | platform-independent instructions in .class files |
| JIT | Just-In-Time compiler inside the JVM that turns frequently used bytecode into fast native code |
Structure of a Java program
All code lives inside classes. A source file may contain several classes but at most one public top-level class, and the file must be named after it. Execution starts at public static void main(String[] args): public so the JVM can call it, static so no object is needed, void because it returns nothing, and args holds command-line arguments.
// File name must match the public class: Main.java
public class Main {
// The JVM starts the program here: public, static, void, and a String[] parameter
public static void main(String[] args) {
System.out.println("Hello, LearnFrenzy!"); // println adds a newline
System.out.print("Java "); // print does not
System.out.println(21 + " is the current LTS");
System.out.println("args received: " + args.length);
}
}
Hello, LearnFrenzy! Java 21 is the current LTS args received: 0
Comments: // one line, /* several lines */ and /** documentation */ (read by javadoc). Identifiers may contain letters, digits, _ and $, cannot start with a digit, are case-sensitive, and cannot be one of the reserved keywords (class, int, static, …). Conventions: ClassName, methodAndVariableName, CONSTANT_NAME.
The eight primitive types
| Type | Size | Range / values | Default (fields) |
|---|---|---|---|
byte | 8 bits | -128 to 127 | 0 |
short | 16 bits | -32,768 to 32,767 | 0 |
int | 32 bits | about ±2.1 billion | 0 |
long | 64 bits | about ±9.2 × 1018 | 0L |
float | 32 bits | ~6–7 significant digits | 0.0f |
double | 64 bits | ~15–16 significant digits | 0.0 |
char | 16 bits | '\u0000' to '\uffff' (unsigned) | '\u0000' |
boolean | JVM-dependent | true / false | false |
Unlike C and C++, these sizes are the same on every platform. Everything else – String, arrays, your own classes – is a reference type, whose default value is null.
public class Main {
public static void main(String[] args) {
// The 8 primitive types – their sizes are fixed on every platform
byte b = 127; // 8-bit
short s = 32_767; // 16-bit
int i = 2_147_483_647; // 32-bit
long l = 9_000_000_000L; // 64-bit: needs the L suffix beyond int range
float f = 3.14f; // 32-bit IEEE 754: needs the f suffix
double d = 3.14; // 64-bit IEEE 754 (the default for decimals)
char c = 'A'; // 16-bit unsigned UTF-16 code unit
boolean ok = true; // true or false only – not 0/1
System.out.println(b + " " + s + " " + i + " " + l);
System.out.println(f + " " + d + " " + c + " " + ok);
System.out.println(Byte.SIZE + " " + Short.SIZE + " " + Integer.SIZE + " " + Long.SIZE
+ " " + Float.SIZE + " " + Double.SIZE + " " + Character.SIZE);
System.out.println(Integer.MIN_VALUE + " " + Long.MAX_VALUE);
}
}
127 32767 2147483647 9000000000 3.14 3.14 A true 8 16 32 64 32 64 16 -2147483648 9223372036854775807
Literals, variables, var and final
Integer literals are int unless they end with L; decimal literals are double unless they end with f. You can write hex (0xFF), binary (0b1010), octal (017) and use underscores for readability (1_000_000). var (Java 10) infers a local variable's type from its initialiser – the type is still fixed at compile time. final makes a variable assignable only once.
public class Main {
public static void main(String[] args) {
int hex = 0xFF, bin = 0b1010, oct = 017, big = 1_000_000;
char letter = 'J', unicode = '\u0041', tab = '\t';
long micros = 86_400L * 1_000_000; // the L makes the whole multiplication long
var name = "Asha"; // var (Java 10+): the type is inferred – here String
var marks = 91; // int
var avg = 88.5; // double
final double GST = 0.18; // final: cannot be reassigned
// GST = 0.12; compile error: cannot assign a value to final variable GST
System.out.println(hex + " " + bin + " " + oct + " " + big);
System.out.println(letter + "" + unicode + tab + "|");
System.out.println(micros);
System.out.println(name + " " + marks + " " + avg + " " + (1000 * GST));
}
}
255 10 15 1000000 JA | 86400000000 Asha 91 88.5 180.0
Type casting and promotion
Widening conversions (to a larger type) happen automatically; narrowing conversions need an explicit cast and may lose information. In arithmetic, byte, short and char are promoted to int – so byte + byte is an int.
public class Main {
public static void main(String[] args) {
// Widening (automatic): byte → short → int → long → float → double, and char → int
int i = 'A'; // 65
long l = i;
double d = l;
System.out.println(i + " " + l + " " + d);
// Narrowing (explicit cast): may lose information
double price = 99.99;
int rupees = (int) price; // truncates toward zero
int big = 300;
byte b = (byte) big; // keeps the low 8 bits: 300 - 256 = 44
char c = (char) 66;
System.out.println(rupees + " " + b + " " + c + " " + (int) -7.9);
// Integer arithmetic is done in int (or long): small types are promoted
byte x = 10, y = 20;
int sum = x + y; // byte + byte is an int
// byte z = x + y; compile error: possible lossy conversion from int to byte
x += 5; // compound assignment casts implicitly
System.out.println(sum + " " + x + " " + Math.round(2.5) + " " + Math.round(-2.5));
}
}
65 65 65.0 99 44 B -7 30 15 3 -2
Arithmetic rules every Java developer must know
Integer division truncates toward zero; % keeps the sign of the left operand; integer overflow wraps around silently (use Math.addExact and friends to detect it); integer division by zero throws ArithmeticException, but floating-point division gives Infinity or NaN; and decimal fractions like 0.1 are not exact in binary.
public class Main {
public static void main(String[] args) {
System.out.println(7 / 2 + " " + 7 / 2.0 + " " + 7 % 3 + " " + -7 % 3); // int division truncates
System.out.println(0.1 + 0.2); // binary floating point
System.out.println(1.0 / 0 + " " + (0.0 / 0.0) + " " + -1.0 / 0); // no exception for doubles
int max = Integer.MAX_VALUE;
System.out.println(max + 1); // silent overflow: wraps around
System.out.println(Math.addExact(1, 2)); // addExact throws on overflow
char c = 'a';
c++; // chars are numbers
System.out.println(c + " " + (char) ('a' + 2) + " " + ('a' + 2));
// System.out.println(5 / 0); // ArithmeticException at run time
}
}
3 3.5 1 -1 0.30000000000000004 Infinity NaN -Infinity -2147483648 3 b c 99
String concatenation and formatted output
+ with a String on either side concatenates. Because + is evaluated left to right, numbers before the first String are added first. For tables and reports use System.out.printf or String.format with format specifiers like %d, %.2f, %s, %-10s and %n.
public class Main {
public static void main(String[] args) {
// + is evaluated left to right: numbers add until a String appears, then everything is concatenated
System.out.println(1 + 2 + "3"); // "33"
System.out.println("1" + 2 + 3); // "123"
System.out.println("1" + (2 + 3)); // "15"
System.out.println('a' + 'b' + "!"); // chars add as numbers: 97 + 98
System.out.println("" + 'a' + 'b'); // "ab"
// printf / String.format for formatted output
System.out.printf("%-6s|%5d|%8.2f|%c|%b%n", "Asha", 91, 3.14159, 'Z', true);
System.out.println(String.format("%05d %,d %x %e", 42, 1234567, 255, 12345.678));
}
}
33 123 15 195! ab Asha | 91| 3.14|Z|true 00042 1,234,567 ff 1.234568e+04
Common mistakes
- File name not matching the public class – compile error.
- Using a local variable before assigning it – compile error (only fields have defaults).
- Forgetting L or f –
long x = 9000000000;andfloat f = 1.5;do not compile. - Integer division surprises –
5 / 2is 2; write5 / 2.0. - Silent overflow –
Integer.MAX_VALUE + 1is negative. - Comparing doubles with == – use a tolerance or BigDecimal for money.
- "Sum: " + a + b – concatenates instead of adding; use parentheses.
Key points to remember
- javac compiles to bytecode; the JVM runs it on any platform.
- main is public static void main(String[] args).
- 8 primitives with fixed sizes; everything else is a reference type.
- Integer literals are int, decimal literals double; use L and f suffixes.
- Fields have defaults; local variables must be initialised.
- Widening is automatic; narrowing needs a cast.
- Small integer types are promoted to int in arithmetic.
Frequently asked questions
Is Java fully object-oriented?
Not purely: primitive types are not objects. Every primitive has a wrapper class (Integer, Double, …) used where objects are needed, such as collections.
Which Java version should I learn?
Java 21 is the current long-term-support release used by new projects; everything on this page also works on Java 17. Older code you will meet at work often targets Java 8 or 11.
Is Java pass-by-value or pass-by-reference?
Always pass-by-value – for objects, the value passed is a copy of the reference. See the Methods topic.
Where can I learn more?
Read Java Variables, Java Data Types and Java Type Casting, then practise with the exercises below.