Java Cheat Sheet
Java Cheat Sheet
/* A
* Multiple line
* comment
*/
// You can import libraries with helpful methods using import
import java.util.Scanner;
import java.util.*;
// A class defines the attributes (fields) and capabilities (methods) of a real world object
public class Animal {
// static means this number is shared by all objects of type Animal
// final means that this value can't be changed
public static final double FAVNUMBER = 1.6180;
// Variables (Fields) start with a letter, underscore or $
// Private fields can only be accessed by other methods in the class
// Strings are objects that hold a series of characters
private String name;
// An integer can hold values from -2 ^ 31 to (2 ^ 31) -1
private int weight;
// Booleans have a value of true or false
private boolean hasOwner = false;
// Bytes can hold the values between -128 to 127
private byte age;
// Longs can hold the values between -2 ^ 63 to (2 ^ 63) - 1
private long uniqueID;
// Chars are unsigned ints that represent UTF-16 codes from 0 to 65,535
private char favoriteChar;
// Doubles are 64 bit IEEE 754 floating points with decimal values
private double speed;
// Floats are 32 bit IEEE 754 floating points with decimal values
private float height;
// Static variables have the same value for every object
// Any variable or function that doesn't make sense for an object to have should be made
static
// protected means that this value can only be accessed by other code in the same package
// or by subclasses in other packages
protected static int numberOfAnimals = 0;
// A Scanner object allows you to except user input from the keyboard
static Scanner userInput = new Scanner(System.in);
// Any time an Animal object is created this function called the constructor is called
}
public boolean isHasOwner() {
return hasOwner;
}
public void setHasOwner(boolean hasOwner) {
this.hasOwner = hasOwner;
}
public byte getAge() {
return age;
}
public void setAge(byte age) {
this.age = age;
}
public long getUniqueID() {
return uniqueID;
}
// Method overloading allows you to accept different input with the same method name
public void setUniqueID(long uniqueID) {
this.uniqueID = uniqueID;
System.out.println("Unique ID set to: " + this.uniqueID);
}
public void setUniqueID() {
long minNumber = 1;
long maxNumber = 1000000;
// Generates a random number between 1 and 1000000
this.uniqueID = minNumber + (long)(Math.random() * ((maxNumber - minNumber) + 1));
// You can cast from one primitive value into another by putting what you want
between ( )
// (byte) (short) (long) (double)
// (float), (boolean) & (char) don't work.
// (char) stays as a number instead of a character
// You convert from a primitive to a string like this
String stringNumber = Long.toString(maxNumber);
// Byte.toString(bigByte); Short.toString(bigShort); Integer.toString(bigInt);
// Float.toString(bigFloat); Double.toString(bigDouble);
Boolean.toString(trueOrFalse);
// You convert from a String to a primitive like this
int numberString = Integer.parseInt(stringNumber);
// parseShort, parseLong, parseByte, parseFloat, parseDouble, parseBoolean
System.out.println("Unique ID set to: " + this.uniqueID);
}
public char getFavoriteChar() {
return favoriteChar;
}
public void setFavoriteChar(char favoriteChar) {
this.favoriteChar = favoriteChar;
}
public void setFavoriteChar() {
int randomNumber = (int) (Math.random() * 126) + 1;
this.favoriteChar = (char) randomNumber;
// if then else statement
// > < == != >= <=
if(randomNumber == 32){
System.out.println("Favorite character set to: Space");
} else if(randomNumber == 10){
System.out.println("Favorite character set to: New Line");
} else {
System.out.println("Favorite character set to: " + this.favoriteChar);
}
// Logical operators
// ! : Converts the boolean value to its right to its opposite form ie. true to false
// & : Returns true if boolean value on the right and left are both true (Always
evaluates both boolean values)
// && : Returns true if boolean value on the right and left are both true (Stops
evaluating after first false)
// | : Returns true if either boolean value on the right or left are true (Always
evaluates both boolean values)
// || : Returns true if either boolean value on the right or left are true (Stops
evaluating after first true)
// ^ : Returns true if there is 1 true and 1 false boolean value on the right or left
if((randomNumber > 97) && (randomNumber < 122)){
System.out.println("Favorite character is a lowercase letter");
}
if(((randomNumber > 97) && (randomNumber < 122)) || ((randomNumber > 64) && (
randomNumber < 91))){
System.out.println("Favorite character is a letter");
}
if(!false){
System.out.println("I turned false to " + !false);
}
return "Grrrr";
}
// With polymorphism we can refer to any Animal and yet use overridden methods
// in the specific animal type
public static void speakAnimal(Animal randAnimal){
System.out.println("Animal says " + randAnimal.makeSound());
}
// public allows other classes to
// static means that only a class
// void means it doesn't return a
// This method can except Strings
executed
multidimensional array
arrayName = { { { "000" }, { "100" }, { "200" }, { "300" } },
"010" }, { "110" }, { "210" }, { "310" } },
"020" }, { "120" }, { "220" }, { "320" } }};