Created
May 19, 2015 05:26
-
-
Save mikestratton/c5cd96dbf848d80cff6b to your computer and use it in GitHub Desktop.
Cheat Sheet for Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Single line comment | |
/* multiple | |
* line | |
* comment | |
*/ | |
//import libraries | |
import java.util.Scanner; | |
import java.math.BigDecimal; | |
// A class defines the attributes (fields) and capabilities (methods) of an object | |
public class MyClass{ //public is accessible by everyone | |
/* static means that this number is going to be shared by every MyClass object that is created | |
* final means that this is a constant, final classes cannot be subclassed | |
* final means that this value cannot be changed | |
* final methods cannot be overwritten */ | |
public static final double FAVNUMBER = 1.45; //common to use all capitals for final classes | |
// variables(aka fields) can start with a letter, and underscore_ or a dollar sign $ | |
// private can only be accessed by other methods in its containing class | |
// common practice to make all variables private to secure code | |
/* Primitive data types in java | |
* String, int, boolean, byte, long, char, double, float */ | |
// 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 | |
// code contained within the src code folder is the same package | |
protected static int myNumber = 0; | |
static Scanner userInput = new Scanner(System.in); | |
// Any time an MyClass object is created this function called the constructor is called | |
// to initialize the object | |
public MyClass(){ | |
// Shorthand for myTotalNumber = myTotalNumber + 1; | |
myTotalNumber++; | |
int sumOfNumbers = 5 + 1; | |
System.out.println("5 + 1 = " + sumOfNumbers); | |
int diffOfNumbers = 5 - 1; | |
System.out.println("5 - 1 = " + diffOfNumbers); | |
int multOfNumbers = 5 * 1; | |
System.out.println("5 * 1 = " + multOfNumbers); | |
int divOfNumbers = 5 / 1; | |
System.out.println("5 / 1 = " + divOfNumbers); | |
int modOfNumbers = 5 % 3; | |
System.out.println("5 % 3 = " + modOfNumbers); | |
// print is used to print to the screen, but it doesn't end with a newline \n | |
System.out.print("Enter the myString: \n"); | |
// The if statement performs the actions between the { } if the condition is true | |
// userInput.hasNextLine() returns true if a String was entered in the keyboard | |
if(userInput.hasNextLine()){ | |
// this provides you with a way to refer to the object itself | |
// userInput.nextLine() returns the value that was entered at the keyboard | |
this.setmyString(userInput.nextLine()); | |
// hasNextInt, hasNextFloat, hasNextDouble, hasNextBoolean, hasNextByte, | |
// hasNextLong, nextInt, nextDouble, nextFloat, nextBoolean, etc. | |
} | |
this.setmyChar(); | |
this.setmyLong(); | |
} //end public MyClass | |
}; //end class MyClass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment