java.lang

Core language classes

  • String
  • Math
  • Object
  • Integer
  • Thread

java.util

Data structures, algorithms, and utilities classes

  • ArrayLists
  • Collections
  • Scanner

java.io

Reading/writing files and streams

  • File
  • FileReader
  • BufferedWriter
  • ObjectOutputStream

javax.swing

Building desktop GUI applications

  • JFrame
  • JButton
  • JLabel
  • JTextField

String

Return Type: Boolean

MethodDescriptionExampleResult
equals(Object anObject)Checks if this string is equal to another object."hello".equals("hello")true
equalsIgnoreCase(String anotherString)Checks equality ignoring case."Hello".equalsIgnoreCase("hello")true
contains(CharSequence s)Returns true if string contains the specified sequence."hello".contains("ell")true
startsWith(String prefix)Checks if string starts with specified prefix."hello".startsWith("he")true
startsWith(String prefix, int offset)Checks if string starts with prefix at the given offset."hello".startsWith("ll", 2)true
endsWith(String suffix)Checks if string ends with specified suffix."hello".endsWith("lo")true
isEmpty()Returns true if string length is 0."".isEmpty()true
matches(String regex)Returns true if string matches regex."abc123".matches("\\w+\\d+")true

Return Type: int

MethodDescriptionExampleResult
length()Returns number of characters in the string."hello".length()5
indexOf(int ch)Returns index of first occurrence of character (-1 if not found)."hello".indexOf('l')2
indexOf(String str)Returns index of first occurrence of substring (-1 if not found)."hello".indexOf("ll")2
lastIndexOf(int ch)Returns index of last occurrence of character (-1 if not found)."hello".lastIndexOf('l')3
lastIndexOf(String str)Returns index of last occurrence of substring (-1 if not found)."hello".lastIndexOf("l")3
compareTo(String anotherString)Compares two strings lexicographically."abc".compareTo("abd")-1
compareToIgnoreCase(String str)Compares two strings ignoring case."abc".compareToIgnoreCase("ABC")0
codePointAt(int index)Returns Unicode code point at specified index."ABC".codePointAt(1)66
codePointBefore(int index)Returns Unicode code point before the specified index."ABC".codePointBefore(2)66
codePointCount(int beginIndex, int endIndex)Returns the number of Unicode code points in the specified text range."ABC".codePointCount(0,3)3

Return Type: char / String / String[]

MethodDescriptionExampleResult
charAt(int index)Returns character at specified index."hello".charAt(1)'e'
substring(int beginIndex)Returns substring from beginIndex to end."hello".substring(2)"llo"
substring(int beginIndex, int endIndex)Returns substring from beginIndex (inclusive) to endIndex (exclusive)."hello".substring(1,4)"ell"
concat(String str)Concatenates specified string to the end."Hello".concat(" World")"Hello World"
replace(char oldChar, char newChar)Replaces all occurrences of oldChar with newChar."hello".replace('l','p')"heppo"
replace(CharSequence target, CharSequence replacement)Replaces all occurrences of target sequence with replacement."hi hi".replace("hi","yo")"yo yo"
replaceFirst(String regex, String replacement)Replaces first substring matching regex with replacement."a1b2c3".replaceFirst("\\d","")"ab2c3"
replaceAll(String regex, String replacement)Replaces all substrings matching regex with replacement."a1b2c3".replaceAll("\\d","")"abc"
trim()Removes leading and trailing whitespace." hi ".trim()"hi"
toUpperCase()Converts string to uppercase."hi".toUpperCase()"HI"
toLowerCase()Converts string to lowercase."HI".toLowerCase()"hi"
split(String regex)Splits string around matches of regex."a,b,c".split(",")["a","b","c"]
split(String regex, int limit)Splits string with limit on number of elements."a,b,c".split(",",2)["a","b,c"]
intern()Returns canonical representation from string pool."hello".intern()"hello"
format(String format, Object... args)Returns formatted string.String.format("%d + %d = %d",1,2,3)"1 + 2 = 3"

Return Type: void

MethodDescriptionExampleResult
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)Copies characters from string into destination array.char[] arr = new char[5]; "hello".getChars(0,5,arr,0)

Object

Return Type: Boolean

MethodDescriptionExampleResult
equals(Object obj)Indicates whether some other object is "equal to" this one.obj1.equals(obj2)true or false
equalsIgnoreCase does not exist for Object
notify()Wakes up a single thread waiting on this object's monitor.synchronized(obj){ obj.notify(); }
notifyAll()Wakes up all threads waiting on this object's monitor.synchronized(obj){ obj.notifyAll(); }

Return Type: int

MethodDescriptionExampleResult
hashCode()Returns a hash code value for the object.obj.hashCode()12345678 (example)
wait()Causes current thread to wait until notified. Returns int? Actually void—so remove from INT table

Return Type: Object

MethodDescriptionExampleResult
clone()Creates and returns a copy of this object (if class implements Cloneable).obj.clone()new Object() copy

Return Type: String

MethodDescriptionExampleResult
toString()Returns a string representation of the object.obj.toString()"ClassName@hashcode"

Return Type: void

MethodDescriptionExampleResult
wait()Causes current thread to wait until another thread invokes notify() or notifyAll() on this object.synchronized(obj){ obj.wait(); }
wait(long timeout)Waits up to the specified time in milliseconds.synchronized(obj){ obj.wait(1000); }
wait(long timeout, int nanos)Waits up to the specified time (ms + nanos).synchronized(obj){ obj.wait(1000,500); }

Random

Constructors

ConstructorDescriptionExampleNotes
Random()Creates a new random number generator using a time-based seed. Each instance will produce different sequences.Random rand = new Random();Default constructor; uses current time as seed.
Random(long seed)Creates a new random number generator using the given seed. Seeding ensures reproducible results.Random rand = new Random(12345L);Same seed ⇒ same random sequence.

Return Type: Boolean

MethodDescriptionExampleResult
nextBoolean()Returns a random boolean value (true or false).rand.nextBoolean()true

Return Type: int

MethodDescriptionExampleResult
nextInt()Returns a random integer (can be negative).rand.nextInt()-123456789
nextInt(int bound)Returns a random integer between 0 (inclusive) and bound (exclusive).rand.nextInt(10)7

Return Type: long

MethodDescriptionExampleResult
nextLong()Returns a random long value.rand.nextLong()-3456789012345L

Return Type: float

MethodDescriptionExampleResult
nextFloat()Returns a random float between 0.0 (inclusive) and 1.0 (exclusive).rand.nextFloat()0.57293

Return Type: double

MethodDescriptionExampleResult
nextDouble()Returns a random double between 0.0 (inclusive) and 1.0 (exclusive).rand.nextDouble()0.93527
nextGaussian()Returns a random value from a standard normal distribution (mean 0, std dev 1).rand.nextGaussian()-0.3725

Return Type: void

MethodDescriptionExampleResult
setSeed(long seed)Sets the seed for this random number generator, restarting its sequence.rand.setSeed(12345L)

Return Type: Stream

MethodDescriptionExampleResult
ints()Returns an infinite stream of random integers.rand.ints().limit(3).forEach(System.out::println)e.g. 12 93 47
ints(int streamSize)Returns a stream with the specified number of random integers.rand.ints(5)[87, 64, 21, 95, 13]
ints(int origin, int bound)Returns an infinite stream of random integers within the specified range.rand.ints(0, 10).limit(3)[2, 7, 9]
longs()Returns an infinite stream of random long values.rand.longs().limit(2)[3487347, -8327493]
doubles()Returns an infinite stream of random doubles.rand.doubles().limit(2)[0.234, 0.894]