public class Hello {
// main method
public static void main(String[] args)
// Output: Hello, world!
[Link]("Hello, world!");
Varriables
int num = 5;
float floatNum = 5.99f;
char letter = 'D';
boolean bool = true;
String site = "[Link]";
loops
String word = "QuickRef";
for (char c: [Link]()) {
[Link](c + "-");
// Outputs: Q-u-i-c-k-R-e-f-
arrays
char[] chars = new char[10];
chars[0] = 'a'
chars[1] = 'b'
String[] letters = {"A", "B", "C"};
int[] mylist = {100, 200};
boolean[] answers = {true, false};
swap
int a = 1;
int b = 2;
[Link](a + " " + b); // 1 2
int temp = a;
a = b;
b = temp;
[Link](a + " " + b); // 2 1
Type casting
int i = 10;
long l = i; // 10
// Narrowing
double d = 10.02;
long l = (long)d; // 10
[Link](10); // "10"
[Link]("10"); // 10
[Link]("10"); // 10.0
conditional
int j = 10;
if (j == 10) {
[Link]("I get printed");
} else if (j > 10) {
[Link]("I don't");
} else {
[Link]("I also don't");
}
User input
Scanner in = new Scanner([Link]);
String str = [Link]();
[Link](str);
int num = [Link]();
[Link](num);
String basic
String str1 = "value";
String str2 = new String("value");
String str3 = [Link](123);
Concetenation
String s = 3 + "str" + 3; // 3str3
String s = 3 + 3 + "str"; // 6str
String s = "3" + 3 + "str"; // 33str
String s = "3" + "3" + "23"; // 3323
String s = "" + 3 + 3 + "23"; // 3323
String s = 3 + 3 + 23; // 29
Comparision
String s1 = new String("QuickRef");
String s2 = new String("QuickRef");
s1 == s2 // false
[Link](s2) // true
"AB".equalsIgnoreCase("ab") // true
Immutable
String str = "hello";
[Link]("world");
// Outputs: hello
[Link](str);
Java arrays
Declare
int[] a1;
int[] a2 = {1, 2, 3};
int[] a3 = new int[]{1, 2, 3};
int[] a4 = new int[3];
a4[0] = 1;
a4[2] = 2;
a4[3] = 3;
modify
int[] a = {1, 2, 3};
[Link](a[0]); // 1
a[0] = 9;
[Link](a[0]); // 9
[Link]([Link]); // 3
Loop read and Modify
int[] arr = {1, 2, 3};
for (int i=0; i < [Link]; i++) {
arr[i] = arr[i] * 2;
[Link](arr[i] + " ");
// Outputs: 2 4 6
Loop read
String[] arr = {"a", "b", "c"};
for (int a: arr) {
[Link](a + " ");
// Outputs: a b c
Multidimentional array
int[][] matrix = { {1, 2, 3}, {4, 5} };
int x = matrix[1][0]; // 4
// [[1, 2, 3], [4, 5]]
[Link](matrix)
for (int i = 0; i < [Link]; ++i) {
for(int j = 0; j < a[i].length; ++j) {
[Link](a[i][j]);
// Outputs: 1 2 3 4 5 6 7
Java Conditional
If else
int k = 15;
if (k > 20) {
[Link](1);
} else if (k > 10) {
[Link](2);
} else {
[Link](3);
Switch
int month = 3;
String str;
switch (month) {
case 1:
str = "January";
break;
case 2:
str = "February";
break;
case 3:
str = "March";
break;
default:
str = "Some other month";
break;
// Outputs: Result March
[Link]("Result " + str);
Java loops
for (int i = 0; i < 10; i++) {
[Link](i);
// Outputs: 0123456789
int[] numbers = {1,2,3,4,5};
for (int number: numbers) {
[Link](number);
// Outputs: 12345
While loop
int count = 0;
while (count < 5) {
[Link](count);
count++;
// Outputs: 01234
Continue
for (int i = 0; i < 5; i++) {
if (i == 3) {
continue;
[Link](i);
// Outputs: 01245