Lecture from: 24.09.2024 and 26.09.2024 | Video: Videos ETHZ

What is Java

Java, a statically-typed, class-based object-oriented programming language, has achieved widespread adoption due to its platform independence and robust feature set.

Its strong emphasis on security, concurrency, and automatic memory management (garbage collection) makes Java a suitable choice for developing reliable and scalable applications in mission-critical environments.

Java (21) Docs: https://docs.oracle.com/en/java/javase/21/docs/api/

How to Run Java

There are two primary ways to run Java programs:

  1. Whole Program:
    • Java source code is compiled into Java bytecode (.class files), which is then executed by the Java Virtual Machine (JVM).
  2. REPL (Read-Evaluate-Print Loop):
    • Java can also be run interactively as a REPL using tools like JShell (introduced in JDK 9), allowing you to execute Java expressions and statements interactively without needing to compile an entire program.

How to Develop and Execute Java

  • Use an IDE (Integrated Development Environment) for development, such as Eclipse, IntelliJ IDEA, or NetBeans. These IDEs provide tools for writing, compiling, running, and debugging Java programs.

Java Programs and Classes

  • Every Java program is defined within a class. The class name must match the filename of the source file.

  • Naming Convention: Class names follow the Upper Camel Case convention (e.g., MyFirstProgram).

Example:

public class MyFirstProgram {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

In the example above, the class name is MyFirstProgram, so the file must be named MyFirstProgram.java.

Reserved Keywords

Java has a set of reserved keywords that cannot be used as identifiers (e.g., for variable names or class names). These keywords have special meaning in the language syntax.

KeywordDescription
abstractDeclares an abstract class or method
assertUsed for debugging with assert statements
booleanDeclares a boolean type
breakExits a loop or a switch statement
byteDeclares a byte type
caseDefines cases in a switch statement
catchCatches exceptions in a try block
charDeclares a character type
classDeclares a class
continueSkips the current iteration of a loop
defaultSpecifies default behavior in a switch statement
doUsed with while to create a do-while loop
doubleDeclares a double-precision floating-point type
elseSpecifies the alternative block in an if statement
enumDefines an enumerated type
extendsSpecifies a superclass for a class
finalDeclares constants or prevents overriding
finallyBlock that always executes after a try block
floatDeclares a floating-point type
forStarts a for loop
ifBegins an if statement
implementsDeclares interfaces a class will implement
importImports other classes or packages
instanceofTests if an object is an instance of a class
intDeclares an integer type
interfaceDeclares an interface
longDeclares a long integer type
nativeSpecifies that a method is implemented in native code
newCreates new objects
nullRepresents the null value
packageDeclares a package
privateDeclares a private access modifier
protectedDeclares a protected access modifier
publicDeclares a public access modifier
returnExits a method and optionally returns a value
shortDeclares a short integer type
staticDeclares class-level fields or methods
strictfpUsed to restrict floating-point calculations
superRefers to the superclass
switchStarts a switch statement
synchronizedEnsures a method or block is synchronized
thisRefers to the current object instance
throwThrows an exception
throwsDeclares exceptions a method can throw
transientPrevents serialization of fields
tryBegins a try block for exception handling
voidSpecifies that a method does not return a value
volatileDeclares variables that can be modified asynchronously
whileStarts a while loop

Comments in Java

Java supports two types of comments:

  1. Single-line comments:

    // This is a single-line comment
  2. Multi-line comments:

    /*
     * This is a multi-line comment
     * It can span multiple lines
     */

Text Escape Sequences

Java provides a set of escape sequences to represent special characters in strings.

Escape SequenceMeaning
\nNewline
\"Double quote
\\Backslash
\tTab

Example:

System.out.println("This is a \"quoted\" string with a newline.\nAnd this is on a new line.");

Continue here: 03 Java (Basics)