How to Program with Java Podcast
Trevor Page: Java Guru | Programmer | Teacher

Methods

  • Short: Methods should perform a single task with no side effects.
  • Assigned to a Class: Methods are associated with a class (an object).
  • Return vs Void: Methods can either return a value or be void.
  • Parameter vs Argument:
    • Parameter: A variable in the method’s signature (method declaration).
    • Argument: An expression used when calling the method.

Arrays

  • Arrays have a defined size when created.
  • ArrayList is often preferred.

Primitives

  • Building blocks with no objects.
  • More efficient than objects.
  • Examples: int, double, boolean, float, char, byte.
  • No reference, no pointer.
  • Default values for instance variables.

Objects

  • Everything in Java is an object.
  • The Object class is the parent class of all classes in Java.
  • Useful for referring to objects when their type is unknown.
  • Example:
    Object obj = getObject(); // Referring to an object with an unknown type

Static Keyword

  • Used for memory management, shared variables, or methods.
  • Applies to variables, methods, blocks, and nested classes.
  • Belongs to the class rather than an instance.

Inheritance

  • Attributes and methods can be inherited from one class to another.
  • Categories:
    • Subclass (Child): Inherits from another class.
    • Superclass (Parent): The class being inherited from.
  • To inherit from a class, use the extends keyword.
  • Interface:
    • Defines an abstract type specifying class behavior.
    • Contains static constants and abstract methods.
  • Abstract Class:
    • Important observations:
      • No instance of an abstract class can be created.
      • Constructors are allowed.
      • Can have a final method, but no abstract method in a final class.
      • Cannot create an object for an abstract class.
      • Can define static methods.
      • Abstract keyword can be used for top-level and inner classes.
      • If a class has at least one abstract method, declare the class as abstract.
      • If a child class can’t provide implementation for all abstract methods, declare it as abstract.

Exceptions

  • try, catch, finally.
  • Checked exceptions are caught at compile time, while unchecked exceptions are at runtime.
  • Checked exceptions must be handled, either by re-throwing or using a try-catch block.

Collections

  • Data structures: ArrayList, LinkedList, Set, Map.

Packages

  • Organizational structure of an app.
  • Naming convention: Lowercase and hyphen, e.g., at.ercode.

Constructors

  • Special methods used to initialize objects.
  • Called when an object of a class is created.
  • Sets initial values for object attributes.

Custom Sorting

  • Interface – Comparable: Defines compareTo(obj1) method.
  • Interface – Comparator: Defines compare(obj1, obj2) method.

Casting

  • Down-casting: Parent to child (may cause errors).
  • Up-casting: Child to parent.
  • Method: instanceOf checks if casting is possible.

Unit-Tests/Mocking

  • Arrange, Act, Assert.
  • Dependencies should be mocked up.

Interview Questions

  • JDK vs JRE.
  • Checked vs unchecked exceptions.
  • Final vs finally vs finalize.
  • Inner class vs subclass.
  • Access specifiers/modifiers: public, private, package, protected.
  • Data encapsulation.
  • Singleton class.
  • Continue vs break.
  • Java packages.
  • Interface vs abstract class.
  • HashMap vs Hashtable (synchronized).
  • Vector (synchronized) vs ArrayList.

Stream

  • Fancy for loop.
  • Intermediate operations: filter, map, sorted.
  • Terminal operations: collect, forEach, reduce.
  • A stream is not a data structure; it processes input from Collections, Arrays, or I/O channels.
  • Streams don’t change the original data structure; they provide results as per pipelined methods.
  • Each intermediate operation is lazily executed.

Lambda

  • A new feature in Java SE 8 for representing one-method interfaces concisely.
  • Components: Argument-list, Arrow-token, Body.
  • Useful in the collection library for iterating, filtering, and extracting data.