Java Programming Language Notes

In Java, class names has to match with the file names

To get a jar file that you can execute on a JVM, you need to perform the following steps:

  1. Compile the Java source code into byte code
  2. Create a jar file from the compiled byte code
  3. Execute generated jar file with JVM

If we outline the commands corresponding to the steps above

  1. javac Main.java
  2. jar cvfe my-jar.java Main Main.class OtherDeps.class
  3. java -jar my-jar.java

In addition to swtich statements, there is also switch expressions

var x = switch(someVar) {
  case "A" -> "A variant";
  case "B" -> "B variant";
  case "C" -> "C variant";
  default -> "Catch all variant";
}

yield keyword can be used in a switch expression, if you want to execute multiple expressions.

protected makes a property of the class accessible to the same package defined other classes and subclasses.

Class level declared variables for the numeric, char and boolean types are set to their 0 value while local variables are set to null;

All classes in Java have a default constructor, but as soon as you define one explicit constructor, compiler stops generating a default one, i.e 0 parameter constructor is disallowed until you define it also explicitly

Java allows you to initialize class properties outside of constructors.

POJO stands for Plain Old Java Objects. It’s mainly used to reference record objects.

You can triple double quote to create a multi-line string.

var x = """
Hello world
"""

If a class is marked as final, other classes cannot inherit from that class.

After you check if an object is instance of a specific class, and if it succeeds, it becomes available with an alias

if (someInstance instance someClass someInstanceAlias) {
  // In this block, someInstanceAlias is an instance of someClass
}

Interfaces can provide implementations just like abstract classes, this is especially used when adding new methods to the interface, so you don’t break backwards compatibility, to provide an implementation in an interface you would use the keyword default to define a default implementation.

Interfaces can also define static methods, defined static method must be called on the interface itself, not by the classes implement that interface.

Explain Sealed, non-sealed, permits, final

Collections also provide convenient way to iterate through the elements they contain.

Set<String> s = new HashSet();

s.put("Spain");
s.put("France");
s.put("Russia");

var it = s.iterator();
while (s.hasNext()) {
  s.next();
}

To enable functional programming style on lists, you need to call the .stream method on it.

Stream API allows two operations, intermediate operations, and terminal operations, the former operates and outputs a result, while the latter terminates the stream. For example, map, filter returns a new stream, while forEach terminates the stream.

Checked exceptions are when a method lets its caller know that it can throw an error, and compiler enforces developer to check for these exceptions, while unchecked exceptions operations that can throw an error but are not checked by the compiler.

The order of catch blocks matter, the more general exception class should be listed last.

The resources can be declared and initialized within the try block, so when the execution of the try-catch-finally block ends, the resource will be automatically closed. To be able to use within the try block, the resource must implement AutoCloseable interface.

You can list multiple exceptions in a catch block with the |.

try {
  // something
} catch (IOException | InputMismatchException) {}

Within try parenthesis, you can initialize multiple resources.

try (
  var r1 = new Resource();
  var r2 = new Resource()
) // ...

Each will be closed after the block finishes executing.

You can define different constructors that require less arguments to be passed, and you can use this() call to call for a specific constructor.

You can delegate exception handling to the caller, especially for the checked exception, you can define your own throws expression to delegate the exception handling to the caller.

Checked errors are part of the interface rules, so if the interface doesn’t define it, and you throw a checked error, then compiler will complain that you do not implement the interface properly.