3 billion universities run Java
public class main {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
javac main.java
java main
// Hello World
static int timesTwo(int x) {
return x * 2;
}
public static void main(String args[]) {
System.out.println(timesTwo(2)); // 4
}
public static void main(String args[]) {
System.out.println(timesTwo(2)); // 4
}
static int timesTwo(int x) {
return x * 2;
}
public static void main(String args[]) {
System.out.println(add(2, 3));
}
static int add(int x, int y) {
return x + y;
}
static int timesTwo(int x) {
return x * 2;
}
static double timesTwo(double x) {
return x * 2;
}
System.out.println(timesTwo(2.5)); // 5.0 (double)
System.out.println(timesTwo(2)); // 4 (int)
String name = "Max";
int age = 24;
double value = 5.9;
System.out.println((int) value); // 5
System.out.println(2 * 2); // 4
System.out.println(2 + 2); // 4
System.out.println(2 - 2); // 0
System.out.println(2 / 2); // 1
System.out.println(3 % 2); // 1
int age = 10;
age = 10 + 1;
System.out.println(age); // 11
int age = 10;
age += 1;
System.out.println(age); // 11
int age = 10;
System.out.println(age++); // 10
System.out.println(age); // 11
int age = 10;
System.out.println(++age); // 11
System.out.println(age); // 11
String people[] = new String[3];
people[0] = "Max";
people[1] = "Anna";
people[2] = "Tom";
String people[] = new String[3];
String[] people = new String[3];
String people[] = { "Max", "Anna", "Tom" };
String[] people = { "Max", "Anna" };
System.out.println(people[0]); // Max
System.out.println(people[1]); // Anna
import java.util.Arrays;
String[] people = { "Max", "Anna" };
System.out.println(Arrays.toString(people));
// [Max, Anna]
String[] people = { "Max", "Anna", "Tom" };
for (int i = 0; i < people.length; i++) {
System.out.println(people[i]);
}
String[] people = { "Max", "Anna", "Tom" };
people[0] = "Maxi";
System.out.println(people[0]); // Maxi
int[][] coordinates = new int[3][3];
coordinates[0][0] = 15;
System.out.println(Arrays.toString(coordinates[0]));
System.out.println(Arrays.toString(coordinates[1]));
System.out.println(Arrays.toString(coordinates[2]));
/* [15, 0, 0]
[0, 0, 0]
[0, 0, 0] */
import java.util.ArrayList;
ArrayList<String> people = new ArrayList<String>();
people.add("Max");
people.add("Anna");
ArrayList<String> people = new ArrayList<String>();
people.add("Max");
people.add("Anna");
System.out.println(people.get(0)) // Max;
System.out.println(people.get(1)) // Anna;
for (int i = 0; i < people.size(); i++) {
System.out.println(people.get(i));
}
for (String i : people) {
System.out.println(i);
}
// updating / overwritting:
people.set(0, "Tom");
// removing elements:
people.remove(2);