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:
- Whole Program:
- Java source code is compiled into Java bytecode (.class files), which is then executed by the Java Virtual Machine (JVM).
- 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:
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.
Keyword | Description | |
---|---|---|
abstract | Declares an abstract class or method | |
assert | Used for debugging with assert statements | |
boolean | Declares a boolean type | |
break | Exits a loop or a switch statement | |
byte | Declares a byte type | |
case | Defines cases in a switch statement | |
catch | Catches exceptions in a try block | |
char | Declares a character type | |
class | Declares a class | |
continue | Skips the current iteration of a loop | |
default | Specifies default behavior in a switch statement | |
do | Used with while to create a do-while loop | |
double | Declares a double-precision floating-point type | |
else | Specifies the alternative block in an if statement | |
enum | Defines an enumerated type | |
extends | Specifies a superclass for a class | |
final | Declares constants or prevents overriding | |
finally | Block that always executes after a try block | |
float | Declares a floating-point type | |
for | Starts a for loop | |
if | Begins an if statement | |
implements | Declares interfaces a class will implement | |
import | Imports other classes or packages | |
instanceof | Tests if an object is an instance of a class | |
int | Declares an integer type | |
interface | Declares an interface | |
long | Declares a long integer type | |
native | Specifies that a method is implemented in native code | |
new | Creates new objects | |
null | Represents the null value | |
package | Declares a package | |
private | Declares a private access modifier | |
protected | Declares a protected access modifier | |
public | Declares a public access modifier | |
return | Exits a method and optionally returns a value | |
short | Declares a short integer type | |
static | Declares class-level fields or methods | |
strictfp | Used to restrict floating-point calculations | |
super | Refers to the superclass | |
switch | Starts a switch statement | |
synchronized | Ensures a method or block is synchronized | |
this | Refers to the current object instance | |
throw | Throws an exception | |
throws | Declares exceptions a method can throw | |
transient | Prevents serialization of fields | |
try | Begins a try block for exception handling | |
void | Specifies that a method does not return a value | |
volatile | Declares variables that can be modified asynchronously | |
while | Starts a while loop |
Comments in Java
Java supports two types of comments:
-
Single-line comments:
-
Multi-line comments:
Text Escape Sequences
Java provides a set of escape sequences to represent special characters in strings.
Escape Sequence | Meaning |
---|---|
\n | Newline |
\" | Double quote |
\\ | Backslash |
\t | Tab |
Example:
Continue here: 03 Java (Basics)