Java Reference


Java Reserved Keywords

Java has a set of keywords that are reserved words that cannot be used as variables, methods, classes, or any other identifiers:

KeywordDescription
abstractA non-access modifier. Used for classes and methods: An abstract class cannot be used to create objects (to access it, it must be inherited from another class). An abstract method can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from)
assertFor debugging
booleanA data type that can only store true and false values
breakBreaks out of a loop or a switch block
byteA data type that can store whole numbers from -128 and 127
caseMarks a block of code in switch statements
catchCatches exceptions generated by try statements
charA data type that is used to store a single character
classDefines a class
continueContinues to the next iteration of a loop
constDefines a constant. Not in use – use final instead
defaultSpecifies the default block of code in a switch statement
doUsed together with while to create a do-while loop
doubleA data type that can store whole numbers from 1.7e−308 to 1.7e+308
elseUsed in conditional statements
enumDeclares an enumerated (unchangeable) type
exportsExports a package with a module. New in Java 9
extendsExtends a class (indicates that a class is inherited from another class)
finalA non-access modifier used for classes, attributes and methods, which makes them non-changeable (impossible to inherit or override)
finallyUsed with exceptions, a block of code that will be executed no matter if there is an exception or not
floatA data type that can store whole numbers from 3.4e−038 to 3.4e+038
forCreate a for loop
gotoNot in use, and has no function
ifMakes a conditional statement
implementsImplements an interface
importUsed to import a package, class or interface
instanceofChecks whether an object is an instance of a specific class or an interface
intA data type that can store whole numbers from -2147483648 to 2147483647
interfaceUsed to declare a special type of class that only contains abstract methods
longA data type that can store whole numbers from -9223372036854775808 to 9223372036854775808
moduleDeclares a module. New in Java 9
nativeSpecifies that a method is not implemented in the same Java source file (but in another language)
newCreates new objects
packageDeclares a package
privateAn access modifier used for attributes, methods and constructors, making them only accessible within the declared class
protectedAn access modifier used for attributes, methods and constructors, making them accessible in the same package and subclasses
publicAn access modifier used for classes, attributes, methods and constructors, making them accessible by any other class
requiresSpecifies required libraries inside a module. New in Java 9
returnFinished the execution of a method, and can be used to return a value from a method
shortA data type that can store whole numbers from -32768 to 32767
staticA non-access modifier used for methods and attributes. Static methods/attributes can be accessed without creating an object of a class
strictfpRestrict the precision and rounding of floating point calculations
superRefers to superclass (parent) objects
switchSelects one of many code blocks to be executed
synchronizedA non-access modifier, which specifies that methods can only be accessed by one thread at a time
thisRefers to the current object in a method or constructor
throwCreates a custom error
throwsIndicates what exceptions may be thrown by a method
transientA non-accesss modifier, which specifies that an attribute is not part of an object’s persistent state
tryCreates a try…catch statement
varDeclares a variable. New in Java 10
voidSpecifies that a method should not have a return value
volatileIndicates that an attribute is not cached thread-locally, and is always read from the “main memory”
whileCreates a while loop

Note: true, false, and null are not keywords, but they are literals and reserved words that cannot be used as identifiers.

All String Methods

The String class has a set of built-in methods that you can use on strings.

MethodDescriptionReturn Type
charAt()Returns the character at the specified index (position)char
codePointAt()Returns the Unicode of the character at the specified indexint
codePointBefore()Returns the Unicode of the character before the specified indexint
codePointCount()Returns the number of Unicode values found in a string.int
compareTo()Compares two strings lexicographicallyint
compareToIgnoreCase()Compares two strings lexicographically, ignoring case differencesint
concat()Appends a string to the end of another stringString
contains()Checks whether a string contains a sequence of charactersboolean
contentEquals()Checks whether a string contains the exact same sequence of characters of the specified CharSequence or StringBufferboolean
copyValueOf()Returns a String that represents the characters of the character arrayString
endsWith()Checks whether a string ends with the specified character(s)boolean
equals()Compares two strings. Returns true if the strings are equal, and false if notboolean
equalsIgnoreCase()Compares two strings, ignoring case considerationsboolean
format()Returns a formatted string using the specified locale, format string, and argumentsString
getBytes()Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte arraybyte[]
getChars()Copies characters from a string to an array of charsvoid
hashCode()Returns the hash code of a stringint
indexOf()Returns the position of the first found occurrence of specified characters in a stringint
intern()Returns the canonical representation for the string objectString
isEmpty()Checks whether a string is empty or notboolean
lastIndexOf()Returns the position of the last found occurrence of specified characters in a stringint
length()Returns the length of a specified stringint
matches()Searches a string for a match against a regular expression, and returns the matchesboolean
offsetByCodePoints()Returns the index within this String that is offset from the given index by codePointOffset code pointsint
regionMatches()Tests if two string regions are equalboolean
replace()Searches a string for a specified value, and returns a new string where the specified values are replacedString
replaceFirst()Replaces the first occurrence of a substring that matches the given regular expression with the given replacementString
replaceAll()Replaces each substring of this string that matches the given regular expression with the given replacementString
split()Splits a string into an array of substringsString[]
startsWith()Checks whether a string starts with specified charactersboolean
subSequence()Returns a new character sequence that is a subsequence of this sequenceCharSequence
substring()Returns a new string which is the substring of a specified stringString
toCharArray()Converts this string to a new character arraychar[]
toLowerCase()Converts a string to lower case lettersString
toString()Returns the value of a String objectString
toUpperCase()Converts a string to upper case lettersString
trim()Removes whitespace from both ends of a stringString
valueOf()Returns the string representation of the specified valueString
All Math Methods

A list of all Math methods can be found in the table below:

MethodDescriptionReturn Type
abs(x)Returns the absolute value of xdouble|float|int|long
acos(x)Returns the arccosine of x, in radiansdouble
asin(x)Returns the arcsine of x, in radiansdouble
atan(x)Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radiansdouble
atan2(y,x)Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta).double
cbrt(x)Returns the cube root of xdouble
ceil(x)Returns the value of x rounded up to its nearest integerdouble
copySign(x, y)Returns the first floating point x with the sign of the second floating point ydouble
cos(x)Returns the cosine of x (x is in radians)double
cosh(x)Returns the hyperbolic cosine of a double valuedouble
exp(x)Returns the value of Exdouble
expm1(x)Returns ex -1double
floor(x)Returns the value of x rounded down to its nearest integerdouble
getExponent(x)Returns the unbiased exponent used in xint
hypot(x, y)Returns sqrt(x2 +y2) without intermediate overflow or underflowdouble
IEEEremainder(x, y)Computes the remainder operation on x and y as prescribed by the IEEE 754 standarddouble
log(x)Returns the natural logarithm (base E) of xdouble
log10(x)Returns the base 10 logarithm of xdouble
log1p(x)Returns the natural logarithm (base E) of the sum of x and 1double
max(x, y)Returns the number with the highest valuedouble|float|int|long
min(x, y)Returns the number with the lowest valuedouble|float|int|long
nextAfter(x, y)Returns the floating point number adjacent to x in the direction of ydouble|float
nextUp(x)Returns the floating point value adjacent to x in the direction of positive infinitydouble|float
pow(x, y)Returns the value of x to the power of ydouble
random()Returns a random number between 0 and 1double
round(x)Returns the value of x rounded to its nearest integerint
rint()Returns the double value that is closest to x and equal to a mathematical integerdouble
signum(x)Returns the sign of xdouble
sin(x)Returns the sine of x (x is in radians)double
sinh(x)Returns the hyperbolic sine of a double valuedouble
sqrt(x)Returns the square root of xdouble
tan(x)Returns the tangent of an angledouble
tanh(x)Returns the hyperbolic tangent of a double valuedouble
toDegrees(x)Converts an angle measured in radians to an approx. equivalent angle measured in degreesdouble
toRadians(x)Converts an angle measured in degrees to an approx. angle measured in radiansdouble
ulp(x)Returns the size of the unit of least precision (ulp) of xdouble|float