Java 9 Quick Syntax Reference - Source Code
Java 9 Quick Syntax Reference - Source Code
package myproject;
public class MyApp
{
public static void main(String[] args)
{
System.out.print("Hello World");
}
}
// single-line comment
/* multi-line
comment */
/** javadoc
comment */
// Assignment
myInt = 10;
// Output
System.out.print(myInt); // "10"
}
}
// Anonymous block
public static void main(String[] args)
{
// Anonymous code block
{
int localVar = 10;
}
// localVar is unavailable from here
}
// Arithmetic operators
float f = 3+2; // 5 // addition
f = 3-2; // 1 // subtraction
f = 3*2; // 6 // multiplication
f = 3/2; // 1 // division
f = 3%2; // 1 // modulus (division remainder)
++i; // pre-increment
--i; // pre-decrement
i++; // post-increment
i--; // post-decrement
int j;
i = 5; j = i++; // j=5, i=6
i = 5; j = ++i; // j=6, i=6
// Comparison operators
boolean b = (2==3); // false // equal to
b = (2!=3); // true // not equal to
b = (2>3); // false // greater than
b = (2<3); // true // less than
b = (2>=3); // false // greater than or equal to
b = (2<=3); // true // less than or equal to
// Logical operators
b = (true && false); // false // logical and
b = (true || false); // true // logical or
b = !(true); // false // logical not
// Bitwise operators
i = 5 & 4; // 101 & 100 = 100 (4)
// and
i = 5 | 4; // 101 | 100 = 101 (5)
// or
i = 5 ^ 4; // 101 ^ 100 = 001 (1)
// xor
i = 4 << 1;// 100 << 1 =1000 (8)
// left shift
i = 4 >> 1;// 100 >> 1 = 10 (2)
// right shift
i = 4 >>>1;// 100 >>>1 = 10 (2)
// zero-fill
// right shift
i = ~4; // ~00000100 = 11111011 (-5) // invert
String a = "Hello";
String b = new String(" World");
// String compare
boolean x = a.equals(b); // compares string
boolean y = (a == b); // compares address
boolean z = "Hello".equals(a);
// StringBuffer class
StringBuffer sb = new StringBuffer("Hello");
String s = sb.toString();
// Array declaration
int[] x;
int y[];
// Array allocation
int y[] = new int[3];
// Array assignment
y[0] = 1;
y[1] = 2;
y[2] = 3;
// Array access
System.out.print(x[0] + x[1] + x[2]); // "6"
// Multi-dimensional arrays
String[][] x = {{"00","01"},{"10","11"}};
String[][] y = new String[2][2];
y[0][0] = "00";
y[0][1] = "01";
y[1][0] = "10";
y[1][1] = "11";
// ArrayList class
java.util.ArrayList a = new java.util.ArrayList();
a.add("Hello World");
String s = (String)a.get(0); // Hello World
// If statement
if (x < 1) {
System.out.print(x + " < 1");
}
else if (x > 1) {
System.out.print(x + " > 1");
}
else {
System.out.print(x + " == 1");
}
if (x < 1)
System.out.print(x + " < 1");
else if (x > 1)
System.out.print(x + " > 1");
else
System.out.print(x + " == 1");
// Switch statement
switch (y)
{
case 0: System.out.print(y + " is 0"); break;
case 1: System.out.print(y + " is 1"); break;
default: System.out.print(y + " is something else");
}
int i = 0;
while (i < 10) {
System.out.println(i++);
}
int i = 0;
do {
System.out.println(i++);
} while (i < 10);
// Labeled block
validation:
{
if(true)
break validation;
}
class MyApp
{
void myPrint()
{
System.out.print("Hello");
}
void myPrint(String s)
{
System.out.print(s);
}
String getPrint()
{
return "Hello";
}
void myPrint(String s)
{
// Abort if string is empty
if (s == "") { return; }
System.out.println(s);
}
// Passing arguments
public static void main(String[] args)
{
MyApp m = new MyApp();
int x = 0; // value data type
m.set(x); // value is passed
System.out.print(x); // "0"
class MyRectangle
{
public int x = 10, y = 20;
public int getArea() { return x * y; }
class MyApp
{
public static void main(String[] args)
{
// Create an object of MyRectangle
MyRectangle r = new MyRectangle();
r.x = 10;
r.y = 5;
int z = r.getArea(); // 50 (5*10)
}
}
class MyApp
{
int x; // field is assigned default value 0
int dummy() {
int x; // local variable must be assigned if used
}
}
class MyApp
{
public static void main(String[] args)
{
MyApp a = new MyApp();
class MyCircle
{
float r = 10; // instance field
static float pi = 3.14F; // static/class field
// Instance method
float getArea() { return newArea(r); }
// Static/class method
static float newArea(float a) { return pi*a*a; }
}
class MyApp
{
public static void main(String[] args)
{
float f = MyCircle.pi;
MyCircle c = new MyCircle();
float g = c.r;
double pi = Math.PI;
}
}
// Static fields
class MyCircle
{
static void dummy() { count++; }
static int count = 0;
}
// Initialization block
{
int i = 0;
for(int element : array) element = i++;
}
}
class MyApp
{
public static void main(String[] args)
{
// Upcast and downcast
Apple a = new Apple();
Fruit f = a;
f.flavor = "Sweet";
Apple b = (Apple)f;
Apple c = (f instanceof Apple) ? (Apple)f : null;
}
}
class Rectangle
{
public int w = 10, h = 10;
class MyApp
{
public static void main(String[] args)
{
Triangle o1 = new Triangle();
o1.getArea(); // (50) calls Triangle's version
Rectangle r = o3;
r.newArea(10,10); // (100) calls Rectangle's version
}
}
// Import package
import java.util.*;
// Import static
import static java.lang.Math.*;
// �
double pi = PI; // Math.PI
package mypackage;
public class MyApp
{
public int myPublic; // unrestricted access
protected int myProtected;// package or subclass access
int myPackage; // package access
private int myPrivate; // class access
void test()
{
myPublic = 0; // allowed
myProtected = 0; // allowed
myPackage = 0; // allowed
myPrivate = 0; // allowed
}
}
class MyClass
{
void test(MyApp m)
{
m.myPublic = 0; // allowed
m.myProtected = 0; // allowed
m.myPackage = 0; // allowed
m.myPrivate = 0; // inaccessible
}
}
package newpackage;
import mypackage.MyApp;
class MyClass
{
void test(MyApp m)
{
m.myPublic = 0; // allowed
m.myProtected = 0; // inaccessible
m.myPackage = 0; // inaccessible
m.myPrivate = 0; // inaccessible
}
}
class MyApp
{
public static void main(String[] args)
{
// Local constant
final double PI = 3.14;
}
}
// Constant fields
class MyClass
{
final double E;
static final double C;
class MyClass
{
// Compile-time constant (static and known at compile-time)
final static double C = 3e8;
interface MyInterface
{
int myMethod(); // method signature
// Types
class Class {}
interface Interface {}
enum Enum {}
}
// Functionality interface
interface Comparable
{
int compare(Object o);
}
class MyApp
{
public static void main(String[] args)
{
MyInterface i = new MyClass();
}
}
interface MyInterface
{
default void defaultMethod() {
System.out.println("default");
}
MyInterface.staticMethod(); // "static"
}
}
enum Speed
{
STOP, SLOW, NORMAL, FAST
}
enum Speed
{
STOP(0), SLOW(5), NORMAL(10), FAST(20);
public int speed;
Speed(int s) { speed = s; }
}
class MyApp
{
public static void main(String[] args)
{
Speed[] a = Speed.values();
Speed s = Speed.valueOf(a[0].toString()); // Speed.STOP
}
}
import java.io.*;
class MyApp
{
public static void main(String[] args)
{
try
{
FileReader file = new FileReader("missing.file");
}
catch(FileNotFoundException e)
{
System.out.print(e.getMessage());
}
catch(Exception e)
{
System.out.print(e.getMessage());
}
}
}
// Finally block
import java.io.*;
class MyApp
{
public static void main(String[] args)
{
FileReader in = null;
try {
in = new FileReader("missing.file");
}
catch(FileNotFoundException e) {
System.out.print(e.getMessage());
}
finally {
if (in != null) {
try { in.close(); }
catch(IOException e) {}
}
}
}
}
class MyApp
{
public static void main(String[] args)
{
try(FileReader file = new FileReader("missing.txt")) {
// Read file
}
catch(FileNotFoundException e) {
// Handle exception
}
// Final resource
final FileReader file1 = new FileReader("file1.txt");
try(file1; file2) {
// Read files
}
catch(FileNotFoundException e) {
// Handle exception
}
}
}
// Throwing exceptions
static void MakeException() throws Throwable
{
throw new Throwable("My Throwable");
}
// Specifying exceptions
import java.io.*;
class MyApp
{
static void MakeException() throws IOException, ArithmeticException
{
// �
throw new IOException("My IO exception");
// �
throw new ArithmeticException("Division by zero");
}
}
class MyApp
{
public static void main(String[] args)
{
int iPrimitive = 5;
Integer iWrapper = new Integer(iPrimitive); // boxing
iPrimitive = iWrapper.intValue(); // unboxing
// Generic class
class MyBox<T> { public T box; }
class MyApp
{
public static void main(String[] args)
{
MyBox<Integer> iBox = new MyBox<Integer>();
MyBox<Integer> iBox2 = new MyBox<>();
iBox.box = 5;
Integer i = iBox.box;
}
}
// Generic methods
class MyClass
{
public static <T> void printArray(T[] array)
{
for (T element : array)
System.out.print(element + " ");
}
}
class MyApp
{
public static void main(String[] args)
{
Integer[] iArray = { 1, 2, 3 };
MyClass.printArray(iArray); // "1 2 3"
MyClass.<Integer>printArray(iArray); // "1 2 3"
}
}
class Fruit
{
public String name;
}
class MyApp
{
public static void main(String[] args)
{
// Object ArrayList
ArrayList a = new ArrayList();
a.add("Hello World");
// �
Integer b = (Integer)a.get(0); // run-time error
}
}
import java.util.ArrayList;
class MyApp
{
public static void main(String[] args)
{
// Generic ArrayList (recommended)
ArrayList<String> a = new ArrayList<String>();
a.add("Hello World");
// �
Integer b = (Integer)a.get(0); // compile-time error
}
}
interface Summable
{
public int combine(int a, int b);
}
import java.util.function.*;
public class MyApp
{
public static void main(String[] args) {
BinaryOperator<Integer> adder = (x, y) -> x + y;
adder.apply(2, 3); // 5