Skip to content
VJ Classes

One place to learn science, commerce, arts, computers and life skills

Meeting Java

Why Java? Sun Microsystems, the JVM and Bytecode

Key points

  • Sun Microsystems introduced Java in 1995, and Oracle bought Sun in 2010.
  • javac compiles source code into bytecode, and a JVM on each platform runs it: “write once, run anywhere”.
  • The JIT compiler gives speed, and the garbage collector manages memory.
  • Install the JDK to develop; it contains the JRE and the JVM.
  • Java is strongly typed and object-oriented.

Setting Up and Writing Your First Program

Key points

  • Install the JDK and check it with java -version and javac -version.
  • Source code lives in ClassName.java; javac creates ClassName.class; java ClassName runs it.
  • Execution starts at public static void main(String[] args).
  • println, print and printf display output, and Java is case-sensitive.
  • Read the first compiler error carefully: file, line and message.

Core Programming

Variables, Data Types, Operators and Scanner

Key points

  • Java has eight primitive types with the same size on every machine; String is a class.
  • A cast such as (int) 3.99 converts downwards and drops the fraction; integer division drops it too.
  • int overflow wraps around silently, so choose long when numbers can be large.
  • Scanner reads input with nextInt(), nextDouble(), next() and nextLine().
  • Call nextLine() once after nextInt() if you need to read a line next.

Decisions and Loops

Key points

  • if, else if and else choose one path, and the condition must be a boolean.
  • Compare String contents with equals; == compares references.
  • A switch needs break at the end of each case, or execution falls through.
  • Use for when the number of rounds is known, while when it is not, and do-while when the body must run once.
  • break leaves a loop, continue skips to the next round, and nested loops build tables and patterns.

Methods and Arrays

Key points

  • A method has a return type, a name, parameters and a body; void means no result.
  • Overloaded methods share a name but differ in their parameters.
  • Java passes arguments by value; for arrays and objects the value is a reference, so a method can change the elements but not the caller’s variable.
  • Array indexes run from 0 to length - 1, and Java throws an exception for a bad index.
  • Arrays.toString and Arrays.sort save a lot of work.

Object-Oriented Java

Classes, Objects and Constructors

Key points

  • A class is a blueprint made of fields and methods; an object is created from it with new.
  • A constructor has the class name and no return type; this means the current object.
  • Private fields with public getters and setters protect the data (encapsulation).
  • A static member belongs to the class and is shared; instance members belong to each object.
  • Object variables hold references, so two variables can refer to one object, and == compares references.

Inheritance, Interfaces and Polymorphism

Key points

  • extends creates an is-a relationship; use super to reach the parent, and @Override to replace a method.
  • An abstract class cannot be instantiated; its abstract methods must be implemented by subclasses.
  • An interface is a contract; a class can implement many interfaces but extend only one class.
  • Polymorphism: the version of an overridden method that runs depends on the actual object.
  • Programming against a parent type or an interface lets you add new classes without changing existing code.

Exceptions, ArrayList, HashMap and Next Steps

Key points

  • try, catch and finally handle exceptions; checked exceptions must be caught or declared with throws.
  • Create your own exception by extending Exception, and raise it with throw.
  • ArrayList is a resizable list; collections hold objects, so primitives use wrapper classes such as Integer.
  • HashMap looks up a value by key; keys are unique and the order is not guaranteed.
  • Build small projects next, then move on to files, JDBC and frameworks.

Important terms

  • abstract class
  • access levels
  • arguments
  • Arithmetic
  • Arrays
  • Assignment
  • base case
  • by value
  • bytecode
  • case-sensitive
  • cast
  • class
  • class name
  • Comparison
  • compile-time errors
  • Conditional
  • constructor
  • control flow
  • declare
  • encapsulation
  • entry point
  • extend
  • falls through
  • fields
  • file name
  • for-each
  • garbage collector
  • heap
  • Indexes start at 0
  • inheritance
  • interfaces
  • Java Virtual Machine (JVM
  • JDK
  • just-in-time (JIT) compiler
  • Linux
  • logic error
  • Logical
  • loop
  • Methods
  • nested loop
  • object-oriented
  • objects
  • overloading
  • override
  • parameters
  • PATH
  • polymorphism
  • primitive types
  • recursion
  • reference
  • return value
  • run-time error
  • short-circuit
  • static
  • strongly typed
  • subclass
  • superclass
  • two-dimensional array
  • variable
  • Windows and macOS
Enrol to read the full lessons. This page is a summary. The lessons explain every point with examples, and enrolling also gives you the full notes PDF and your progress. See the course