Study Notes: Beginner Java Guide

Study Notes: Beginner Java Guide

These notes demonstrate how to actively study the Java Basics guide. Notice the focused sessions, key concepts with examples, and review questions for retention.


Reading Session 1: Java Overview & Program Structure

Date: [Today’s date] Goal: Understand Java’s approach and basic program structure

My Pre-Reading Questions:

  • How is Java different from Python?
  • Why is Java considered more complex for beginners?
  • What does “Write Once, Run Anywhere” mean?

Key Insights:

  • Java is object-oriented and platform-independent
  • Everything must be in a class (unlike Python where you can write loose code)
  • Java compiles to bytecode that runs on the Java Virtual Machine (JVM)
  • Strongly typed - must declare variable types

The Infamous “Hello World” Structure:

1public class HelloWorld {
2    public static void main(String[] args) {
3        System.out.println("Hello, World!");
4    }
5}

Breaking Down the Structure:

  • public class - accessible from anywhere, blueprint for objects
  • static - belongs to the class, not to objects
  • void - doesn’t return anything
  • String[] args - command-line arguments
  • System.out.println() - print to console

Reality Check:

“And this is why Java isn’t a beginner’s programming language.”

But understanding this structure is crucial for everything else!


Reading Session 2: Data Types Deep Dive

Date: [Date] Focus: Java’s type system - primitives vs objects

Primitive Types (8 total - memorize these!):

Integer Types:

  • byte - 8-bit (-128 to 127)
  • short - 16-bit (-32,768 to 32,767)
  • int - 32-bit (most common for whole numbers)
  • long - 64-bit (add ‘L’ suffix: 123L)

Floating-Point Types:

  • float - 32-bit (add ‘f’ suffix: 3.14f)
  • double - 64-bit (default for decimals)

Other Primitives:

  • char - single character in single quotes: 'A'
  • boolean - true or false

Reference Types:

  • String - text in double quotes: "Hello"
  • Integer, Double, etc. - “wrapper classes” for primitives
  • Arrays, objects, collections

Key Difference:

  • Primitives = actual values stored in memory
  • Reference types = addresses pointing to objects in memory

Memory Trick:

“Java’s Primitive Eight” - byte, short, int, long, float, double, char, boolean


Reading Session 3: Variables & Constants

Date: [Date]

Java’s Strict Typing:

1// Must declare type first!
2int number;           // Declaration
3number = 42;          // Assignment
4int value = 100;      // Declaration + assignment
5
6// This won't work (unlike Python):
7// number = "Hello";  // ERROR! Can't change types

Constants:

1final double PI = 3.14159;  // Cannot change after this
2// PI = 3.14;               // ERROR! Cannot reassign

Key Points:

  • Java enforces type safety at compile time
  • final keyword makes true constants (not just convention)
  • Usually combine with static for class-level constants

Comparison with Python:

JavaPython
int number = 42;number = 42
final int MAX = 100;MAX = 100 (convention only)
Type errors at compile timeType errors at runtime

Reading Session 4: Control Structures

Date: [Date] Note: Similar to Python but with different syntax

Conditionals:

 1// Note: condition must be boolean!
 2if (age >= 18) {
 3    System.out.println("Adult");
 4} else if (age >= 13) {
 5    System.out.println("Teen");
 6} else {
 7    System.out.println("Child");
 8}
 9
10// Switch statement (like Python's match)
11switch (grade) {
12    case 'A':
13        System.out.println("Excellent!");
14        break;  // Don't forget break!
15    case 'B':
16        System.out.println("Good");
17        break;
18    default:
19        System.out.println("Keep trying");
20}

Loops:

 1// For loop - very explicit
 2for (int i = 0; i < 5; i++) {
 3    System.out.println(i);
 4}
 5
 6// While loop
 7while (condition) {
 8    // code
 9}
10
11// Do-while (runs at least once)
12do {
13    // code
14} while (condition);
15
16// For-each loop (like Python's for-in)
17for (String item : myList) {
18    System.out.println(item);
19}

Key Differences from Python:

  • Must use curly braces {} for code blocks
  • Conditions must be boolean (no truthy/falsy values)
  • Need break in switch statements

Reading Session 5: Arrays vs Collections

Date: [Date] This is where Java gets interesting

Arrays (Fixed Size):

1// Declaration and initialization
2int[] numbers = new int[5];        // Creates array of size 5
3int[] values = {1, 2, 3, 4, 5};    // Initialize with values
4
5// Access elements
6numbers[0] = 42;          // Set first element
7int first = values[0];    // Get first element
8int length = values.length; // Get array length (property, not method!)

Collections (Dynamic Size):

 1// ArrayList - like Python list
 2ArrayList<String> list = new ArrayList<>();
 3list.add("Hello");        // Add element
 4list.remove(0);          // Remove by index
 5int size = list.size();   // Get size (method, not property!)
 6
 7// HashMap - like Python dict
 8HashMap<String, Integer> map = new HashMap<>();
 9map.put("age", 25);       // Add key-value pair
10int age = map.get("age"); // Get value by key

Key Concepts:

  • Arrays = fixed size, faster, lower level
  • Collections = dynamic size, more features, higher level
  • Generics = <Type> syntax for type safety
  • Import required for collections: import java.util.*

When to Use Which:

  • Arrays - when size is known and won’t change
  • Collections - when size will vary or need rich functionality

Reading Session 6: Methods

Date: [Date]

Method Structure:

1public static int add(int a, int b) {
2    return a + b;
3}

Breaking It Down:

  • public - access modifier (who can call this)
  • static - belongs to class, not object instance
  • int - return type
  • add - method name
  • (int a, int b) - parameters with types
  • return - what to give back

Method Overloading:

1public static int add(int a, int b) {
2    return a + b;
3}
4
5public static double add(double a, double b) {
6    return a + b;
7}

Key Points:

  • Must specify return type (or void if no return)
  • Parameters must have types
  • Can overload methods with same name but different parameters
  • Access modifiers control visibility

Comparison with Python:

JavaPython
public static int add(int a, int b)def add(a, b):
Must specify typesNo type specification
Access modifiersEverything is public

Reading Session 7: Classes and Objects

Date: [Date] This is where Java really shines

Basic Class Structure:

 1public class Person {
 2    // Fields (private by default for encapsulation)
 3    private String name;
 4    private int age;
 5    
 6    // Constructor (same name as class)
 7    public Person(String name, int age) {
 8        this.name = name;  // 'this' refers to current object
 9        this.age = age;
10    }
11    
12    // Getter methods
13    public String getName() {
14        return name;
15    }
16    
17    public int getAge() {
18        return age;
19    }
20    
21    // Setter methods
22    public void setName(String name) {
23        this.name = name;
24    }
25    
26    public void setAge(int age) {
27        this.age = age;
28    }
29}

Creating and Using Objects:

1Person person = new Person("Alice", 25);
2String name = person.getName();
3person.setAge(26);

Key Concepts:

  • Encapsulation - fields are private, access through methods
  • Constructor - special method to initialize objects
  • Getters/Setters - controlled access to private fields
  • this keyword - refers to current object instance

Access Modifiers:

  • private - only within same class
  • public - accessible from anywhere
  • protected - within same package or subclasses
  • (default) - within same package

Reading Session 8: Exception Handling

Date: [Date]

Try-Catch-Finally Structure:

 1try {
 2    // Risky code
 3    int result = 10 / 0;
 4} catch (ArithmeticException e) {
 5    // Handle specific exception
 6    System.out.println("Cannot divide by zero!");
 7} finally {
 8    // Always runs (cleanup code)
 9    System.out.println("Cleanup");
10}

Key Points:

  • Checked exceptions - must be handled or declared
  • Unchecked exceptions - can occur at runtime
  • Finally block - always executes (even if exception occurs)
  • Specific exception types - handle different errors differently

Common Exceptions:

  • ArithmeticException - division by zero
  • NullPointerException - accessing null object
  • ArrayIndexOutOfBoundsException - invalid array index
  • FileNotFoundException - file doesn’t exist

Java vs Python Exception Handling:

JavaPython
try-catch-finallytry-except-finally
Must declare checked exceptionsNo checked exceptions
More verbose but saferMore concise

Reading Session 9: Input and Output

Date: [Date]

Scanner for Input:

 1// Must import at top of file
 2import java.util.Scanner;
 3
 4// Create scanner object
 5Scanner scanner = new Scanner(System.in);
 6
 7// Read different types
 8String text = scanner.nextLine();    // Read entire line
 9int number = scanner.nextInt();      // Read integer
10double decimal = scanner.nextDouble(); // Read double

Output:

1System.out.println("Hello");  // With newline
2System.out.print("Hello");    // Without newline
3System.out.printf("Age: %d", age); // Formatted output

Key Points:

  • Must import Scanner - import java.util.Scanner;
  • Create Scanner object - more setup than Python
  • Different methods for different types
  • More explicit than Python’s input() function

Summary & Key Concepts

Java’s Big Ideas:

  1. Object-Oriented Everything - all code lives in classes
  2. Strong Typing - must declare types, caught at compile time
  3. Platform Independence - write once, run anywhere
  4. Encapsulation - private fields, public methods
  5. Explicit Structure - more verbose but clearer intent

Core Concepts to Master:

  1. Program Structure - class with main method
  2. Data Types - primitives vs reference types
  3. Control Flow - if/else, loops, switch
  4. Arrays vs Collections - fixed vs dynamic sizing
  5. Methods - static vs instance, overloading
  6. Classes/Objects - encapsulation, constructors
  7. Exception Handling - try-catch-finally
  8. I/O - Scanner for input, System.out for output

Java vs Python Quick Reference:

ConceptJavaPython
Variableint x = 5;x = 5
ListArrayList<String> list = new ArrayList<>();list = []
DictionaryHashMap<String, Integer> map = new HashMap<>();dict = {}
Methodpublic static void method()def method():
Classpublic class MyClassclass MyClass:

Review Questions for Spaced Repetition

Session 1-3 (Basics):

  1. What does “Write Once, Run Anywhere” mean?
  2. What are the 8 primitive types in Java?
  3. What’s the difference between int and Integer?
  4. Why must you declare variable types in Java?
  5. What does the final keyword do?

Session 4-6 (Control & Methods):

  1. What’s the difference between if in Java vs Python?
  2. Why do you need break in switch statements?
  3. What’s method overloading?
  4. What does static mean in a method declaration?
  5. What’s the difference between public and private?

Session 7-9 (OOP & I/O):

  1. What’s encapsulation and why is it important?
  2. What’s the purpose of a constructor?
  3. What does this keyword refer to?
  4. What’s the difference between checked and unchecked exceptions?
  5. Why do you need to import Scanner?

Practice Projects

Beginner Level:

  1. Calculator Class - Create a Calculator with add, subtract, multiply, divide methods
  2. Student Grade Tracker - Class to store student info and calculate GPA
  3. Bank Account - Class with deposit, withdraw, and balance methods

Intermediate Level:

  1. Library System - Book class, Library class, borrowing system
  2. To-Do List Manager - ArrayList-based task management
  3. Simple Game - Guess the number with proper exception handling

Advanced Practice:

  1. Employee Management System - Multiple classes, inheritance
  2. File-based Data Storage - Save/load data from files
  3. Collection Framework Deep Dive - Practice with different collection types

Common Mistakes to Avoid

  1. Forgetting semicolons - every statement needs one
  2. Not initializing variables - Java won’t let you use uninitialized variables
  3. Confusing arrays and ArrayList - different syntax and capabilities
  4. Not handling exceptions - program will crash
  5. Forgetting break in switch - causes fall-through behavior
  6. Mixing up = and == - assignment vs comparison
  7. Not importing classes - Scanner, ArrayList, etc. need imports

Next Study Session Goals

  • Practice writing classes with proper encapsulation
  • Master ArrayList and HashMap usage
  • Understand inheritance and polymorphism
  • Learn about interfaces and abstract classes
  • Practice exception handling in real scenarios
  • Build a complete program using multiple classes

Reflection Notes

What makes Java challenging:

  • Verbose syntax compared to Python
  • Must think about types constantly
  • More setup code required
  • Strict rules about structure

What makes Java powerful:

  • Type safety catches errors early
  • Clear program structure
  • Excellent performance
  • Rich ecosystem and libraries

Key mindset shift from Python:

  • Plan your data types in advance
  • Think in terms of objects and classes
  • Be explicit about everything
  • Embrace the verbosity as clarity

Study Strategy: Focus on understanding the “why” behind Java’s design decisions. The verbosity and strict typing that seem annoying at first become valuable as programs grow larger and more complex.

Remember: Java teaches you to think like a professional programmer. The habits you build here will serve you well in any language.