---
# System prepended metadata

title: 50.001 W1L1 (Introduction to Java I)

---

# 50.001 W1L1 (Introduction to Java I)

## Portable
Java can be run on most devices, unlike python.
Since Java is a high level programming language, it has to be translated into machine-language before the program is executed by a **compiler**.
The codes that are translated to the machine-language are **machine specific**. 
> Codes compiled on a Windows machine will not run on a Linux machine.

To avoid this, Java compiles the ==.java== code into a **Bytecode** with a ==.class== extension. This bytecode can then be run on any machine with a **JVM** (Java Virtual Machine).
> JVM is an interpreter

## OOP
Java is an Object-Oriented Program (OOP). It is necessary to at least have one class for every module.
> Hello world in Java
```java=
public class MyClass {
    public static void main(String [] args){
        System.out.println("Hello world");
    }
}
```

## Android Studio
Before running the code, there must be a few configurations made to the Android Studio:
1. Go to Run > Edit Configurations
2. Add a new configuration
3. Choose the main class as the entrypoint


## Variables 
Variable definition is as follows:
```
type variable_name = initial value 
```
When not explicitly stated, variables are by default **local**

```java=
public static void main(String[] args){
    int a = 1; // a is a local int
    double b = 2.1 // b is a local double
}
```

## Array
Array declaration is as follows:
``` type[] array_name;```
``` array_name = new type[size];```
```java=
public static void main(String [] args){
    int[] c;
    c = new int[10];

    for(int i =0;i<4;i++){
        c[i] = i;
        System.out.println("Element is: "+c[i]);
    }
}
```
Java will throw an *out of bounds* exception when we try to access the invalid index.

## String
String is **not** an array of characters, but it can be converted from an array of characters easily.

```java=
public static void main(String[] args){
    String d = "Hello world";
    int x = d.length(); //returns the length of the String
    char z = 'x';
    char[] m = {'H','e','l','l','o'};
    d = new String(m);
    //d = "Hello"
    
}
```

## Conditionals
Once the first condition is true, Java will skip the rest of the "else"s.

```java=
if(condition1){
    // action1;
}
else if(condition2){
    // action2;
}
else {
    // action3;
}
```
## Loop
For loop syntax:
```java=
for (int i=0;i<x;i++){
    //looping_action;
}
```

## Method
Method is the same as function in other languages
The main method (entrypoint within the class) is called "main".
```java=
public static void main (String [] args){
    //String [] args is to specify the input arguments that the user input
    printMsg("hello world",4);
    System.out.println("The sum is: "+addStuff(2,10));
    // java can concatenate int into string
}
public static void printMsg(String message, int b){
    for (int i =0;i<b;i++) {
        System.out.println("The message printed is: " + message);
    }
}
public static int addStuff(int a, int b){
    return a+b;
    }
}
```