Java: Basics

Simple class

package my.Package;

import my.package.*;
import my.package.subunit.*;  // separate from my.package

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

(imports are unnecessary for the above class.)

Primitive types

byte8-bit signed (-128 to 127)
short16-bit signed (-32,768 to 32,767)
int32-bit signed (-2,147,483,648 to 2,147,483,647)
long64-bit signed (-2^63 to 2^63-1) – suffix ‘l’
floatsingle precision floating point, – suffix ‘f’
doubledouble precision floating point, – suffix ‘d’
booleandefault false
charsingle 16-bit unicode char , default ‘\u0000’

Type Conversions

  • Casting (conversion) to a larger type is automatic.
  • byte, short, char cast to “int” automatically within calculations
‘1’ + 2 + 3 → 54      // ‘1’ is taken as char type and converted to int
“1” + 2 + 3 → 123     // “1” is taken as String, ‘+’ becomes string concatenation
1 + 2 + “3” → 33      // first ‘+’ is addition then string concatenation

Declaring variables

String myString = “oops”;
var myString = ”oops”;  // type implied, only local variables, NOT class level or parameters
var myNum = 2.00;     // assumed double without ‘f’ suffix

int myInt1, myInt2, myInt3 = 0;  // declare and initialise several on same line
String myString1 = “A”, myString2 = “B”;
// Must all be of the same type.

float myNum = 2.00;   // will error, must be = 2.00f;
  • Single quotes → single primitive type char
  • Double quotes → String type
int myInt = 43; // decimal 43
int myInt = 043; // octal 43 → decimal 35
int myInt = 0x43; // hexadecimal 43 → decimal 67
int myInt = 0b11; // binary 11 → decimal 3

int myInt = 1_000_000; // underscores are ignored. Cannot be at start, end or next to decimal point.

Math

Operator precedence

  1. x++, x- – post operators
  2. ++x, – -x pre operators
  3. – (negative), + (positive), !, ~, cast ()
  4. *, /, % (‘/’ integer division, ‘%’ remainder)
  5. + (addition), – (subtraction)
  6. <<, >>, >>>
  7. <, >, <=, >=, instanceof
  8. ==, !=
  9. &, ^, | (always evaluates second operand)
  10. &&, || (Short circuit operators – only evaluates second operand if necessary)
  11. x ? a : b (“Ternary operator”)
  12. assignment =, +=, -=

Math class

Some methods of the Math class:

  • max(a, b), min(a, b)
  • round(a)
  • pow(a, b)
  • random() – 0 or above, less than 1.

String

Some methods of the String class:

  • length()
  • charAt()
  • indexOf()
  • substring()
  • toLowerCase(), toUpperCase()
  • equals(), equalsIgnoreCase()
  • startsWith(), endsWith()
  • replace()
  • contains()
  • trim(), strip() – Unicode version of trim()
  • stripTrailing(), stripLeading()

String literals are held in a string pool (aka “intern pool”).

String myString1 = “a string”;              // held in the string pool
String myString2 = “a string”;              // the same string from the string pool
String myString3 = new String(“a string”);  // a new string, not related to the string pool
String myString4 = myString3.intern();      // pointer to the string pool version again

Strings are immutable. For manipulating Strings, use StringBuilder (preferred but not thread safe) or StringBuffer (slower but thread safe).