# Introduction to Java

Source: https://cca.twango.dev/AP-Computer-Science/introduction-to-java

# Introduction to Java

## Hello World

```java
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}
```

## Classes

Classes are blueprints for objects: they describe information and behavior. Creating an object is called instantiation.

When thinking about a class, ask: What does it represent? How does it behave?

> [!note] Review clarification
> These course examples place methods in classes. Java also has primitive values, so describing it as object-oriented does not mean every value is an object.

Related: [[object-oriented-programming|Object-oriented programming]].

## Java execution

The compiler turns source (`.java`) into bytecode (`.class`), which runs on a Java Virtual Machine (JVM). JVM implementations make the same bytecode usable on many platforms.

For the example above, save `Main.java`, then run:

```sh
javac Main.java
java Main
```

> [!note] Correction to the original file-name rule
> With ordinary file-based compilation, a **public top-level class** needs a matching filename. The original note stated this as a rule for every class. Run `java Main`, using the class name rather than `java Main.class`.

Source: [Java Language Specification §7.6](https://docs.oracle.com/javase/specs/jls/se17/html/jls-7.html#jls-7.6).

## `print()` and `println()`

`print()` writes without ending the current line. `println()` writes its argument and then ends that line.

> [!note] Correction to the original shortcut
> `println(str)` is similar to printing `str` followed by a newline, but uses the platform's line separator, which is not always the single character `"\n"`.

Source: [PrintStream API](https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html).

## String methods

| Method | Meaning | Return type |
| --- | --- | --- |
| `substring(startIndex, endIndex)` | From the inclusive start to the exclusive end | `String` |
| `substring(startIndex)` | From the inclusive start through the end | `String` |
| `toLowerCase()` | A lowercase version | `String` |
| `toUpperCase()` | An uppercase version | `String` |
| `replace(target, replacement)` | Replace all literal occurrences of the target | `String` |

Strings are immutable: these methods return strings without modifying the original string object.

Source: [String API](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html).

### Escape sequences

| Sequence | Meaning |
| --- | --- |
| `\n` | Newline |
| `\t` | Tab |
| `\\` | One backslash |

### Concatenation

```java
System.out.println("Hello" + " " + "World" + 5); // Hello World5
System.out.println("Hi" + 5 + 5); // Hi55
System.out.println(1 + 2 + "Three"); // 3Three
```

The extra quote after `5` in the original first example was a syntax error. The `+` operator concatenates when a string is involved, converting the other operand to text. Evaluation proceeds left to right: the last example adds the two integers before reaching the string.

Related: [[strings-and-characters|Strings and characters]] · [[variables|Variables]] · [[commenting|Commenting]]
