1. Java Basics
- Java is case-sensitive —
myVar and MyVar are different
- Every statement ends with a semicolon
;
- Comments:
// single line and /* multi-line */
- File name must match the class name:
MathApp.java contains class MathApp
- All code goes in
src/main/java inside a package like com.pluralsight
package com.pluralsight;
public class MathApp {
public static void main(String[] args) {
// Your code starts here
System.out.println("Hello, YearUp!");
}
}
Tip: Every Java program needs a public static void main(String[] args) method — this is where execution begins.
2. Data Types & Variables
| Type | Size | Range / Notes |
byte | 1 B | -128 to 127 |
short | 2 B | -32,768 to 32,767 |
int | 4 B | ~2.1 billion (most common) |
long | 8 B | Needs L suffix: 100L |
float | 4 B | Needs f suffix: 3.14f |
double | 8 B | Default decimal type |
char | 2 B | Single character: 'A' |
boolean | 1 bit | true or false |
Most used: int, double, boolean, String
Watch out: String is a class (capital S), not a primitive. Use double quotes "text" not single quotes.
Naming conventions:
| Variables / methods | camelCase | firstName |
| Classes | PascalCase | MathApp |
| Packages | all lowercase | com.pluralsight |
| Constants | UPPER_SNAKE | final int MAX_SIZE = 10; |
3. Operators & Expressions
Assignment
+= -= *= /= %=
Integer division: int / int = int — 10 / 3 gives 3, not 3.333!
Fix: use double or cast: (double) num1 / num2
Pre/post increment:
++x | Increment first, then use the value |
x++ | Use the value first, then increment |
int x = 5;
int a = ++x; // x becomes 6, a = 6
int y = 5;
int b = y++; // b = 5, then y becomes 6
Type conversion:
Widening (safe, automatic):
byte → short → int → long → float → double
Narrowing (dangerous, needs cast): reverse direction
int x = (int) 3.99; // x = 3 (truncates!)
4. Output & Input
Output
System.out.println("text"); — prints with newline
System.out.printf("Price: %.2f", price); — formatted output
| Specifier | Use | Example |
%d | int / long | printf("%d", 42) |
%f | float / double | printf("%f", 3.14) |
%.2f | 2 decimal places | printf("%.2f", 3.14159) → 3.14 |
%s | String | printf("%s", name) |
Scanner Input
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int age = scanner.nextInt();
double gpa = scanner.nextDouble();
Buffer gotcha: After nextInt() or nextDouble(), call scanner.nextLine(); to consume the leftover newline before reading a String.
| Method | Returns |
scanner.nextLine() | String (full line) |
scanner.nextInt() | int |
scanner.nextDouble() | double |
scanner.next() | String (single word) |
5. Conditionals
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else {
grade = "C";
}
Comparison
== != < <= > >=
Logical
&& AND || OR ! NOT
String comparison: Use .equals("text") or .equalsIgnoreCase("text") — NEVER use == for Strings!
Switch (enhanced):
switch (day) {
case "Monday" -> System.out.println("Start of week");
case "Friday" -> System.out.println("Almost weekend");
default -> System.out.println("Regular day");
}
Ternary:
String result = (age >= 18) ? "Adult" : "Minor";
Tip: Traditional switch uses case value: ... break; — the enhanced arrow syntax (->) is cleaner and prevents fall-through bugs.
6. Methods
// Method signature
public static returnType methodName(paramType paramName) {
// body
return value; // omit if void
}
void = no return value; otherwise specify the return type (int, double, String, etc.)
- Call:
methodName(arguments); or variable = methodName(arguments);
- Parameters are the definition; arguments are the values you pass
Best practice — Scanner pattern: Create Scanner once in main, pass it to other methods as a parameter.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
displayMenu(scanner);
}
public static void displayMenu(Scanner scanner) {
System.out.println("Enter your choice: ");
int choice = scanner.nextInt();
}
Common mistake: Don't create a new Scanner in every method. Create it once, pass it around.