82 Java Snippets You Should Always Carry with You...

82 Java Snippets You Should Always Carry with You...
// [comment]
Single line comment.
/* [comment] */
Multi line comment.
 public
This can be imported publically.
 import [object].*
Imports everything in object.
static
Going to be shared by every [object].
 final
Cannot be changed; common to be defined with all uppercase.
double
Integer with numbers that can have decimals.
 ;
Put after every command.
 String
Just a string of characters.
 private
Can only be changed by a method.
 int
Can store numbers from 2^-31 to 2^31.
 fields are attributes
 boolean
Can have true or false as the value.
 { }
These are used to start and end a function, class, etc.
 byte
These can store from -127 - 128.
 long
Can store numbers from 2^127 to 2^-127.
 char
Just lets you put in one chracter.
 double
-bit number with decimals.
 float
-bit number with decimals.
 protected
Can only be accessed by other code in the package.
 Scanner
This lets you get user input.
 new [object constructor]
This will let you create a new object.
 System.in
This lets you get data from the keyboard.
 public [class]()
This will be the constructor, you use it to create new objects.
 super()
This will create the superclass (the class it's inheriting).
 extends [class]
Makes the object a subclass of [object], [object] must be a superclass.
 ++
Will increment the amount.
 --
Will decrement the amount.
 += [amount]
Increment by [amount]
 -= [amount]
Decrement by [amount]
 *= [amount]
Multiply by [amount]
 /= [amount]
Divide by [amount]
 System.out.println([text])
Will print something to the output console.
 +
Can be used for concatenation. (ex. "6" + [var_here])
 public static void main(String[] args)
This is your main function and your project will start in here.
 System.out.print([text])
This prints stuff but there is no line break. (/n)
 \n
Called a line break; will print a new line.
 \t
This will print a tab.
 if ([condition])
This will make it so if [condition] is true then it'll keep going.
 &&
This means and.
 !
This means not.
 ||
This means or.
 ==
This means equal to.
 <
This means less than.
 >
This means greater than.
 >=
This means greater than or equal to.
 [inputVarHere].hasNextLine()
This will return if there is a next line in the input.
 this
Refer to the class that you are in.
 [caller].next[datatype]()
This will get the [datatype] that you somehow inputted.
 Create getters and setters
This will create the get methods and set methods for every checked variable.
 [caller].hasNext[datatype]()
This will return if it has the correct datatype within the input.
 overloading
If you have different parameters you can call them whatever way you want.
 parameters
These are the inputs of your function.
 ([datatype])[variable]
This will convert [variable] into [datatype]. Also known as casting.
 Math.random()
Generate an extremely percise string of numbers between 0 and 1.
 Primitives
Just the basic data types which are not objects.
 [x].toString()
Will convert [x] into a string.
 [number].parse[numbertype]([string])
This will parse [number] into the [numbertype] with [string].
 ^
Return true if there is one true and one false.
 !=
Not equal too. (NEQ)
 ([condition]) ? [amount] : [var]
This will be like a shortcut way to an if statement.
 switch([variable])
This will do stuff with specific cases. (e.g. switch(hi){ case 2: (do stuff)})
 case [value]:
This will do stuff if the case is the case.
 break
Put that when you want to leave the loop/switch; should be at end of case.
 default [value]:
This will do stuff if none of the cases in the switch statement was made.
 for ([number]; [condition]; [operation])
This will start at [number] and then do [operation] until [condition] is met.
 continue
This will just go back to the enclosing loop before reaching other code.
 while ([condition])
This will basically do something while [condition] is true.
 void
This means no return type.
 return
This will return something when you call it to where it was called from .
 do { } while ([condition])
Guarantees it will execute once even if [condition] isn't met.
 printf("%[type] stuff here bah bla", [variable here])
This will let you use [variable here] with %s being where.
 System.out.printf([text])
Another way to print? // didn't quite get but ok then
 [type] [returntype] [name]([parameters]) {
This is a way to create a method.
 [type][[indexes]]
This will create an array with [indexes] amount of indexes; default infinite.
 int[] something = new int[20];
This will just make an array of ints with 20 ints in it.
 for ([object] [nameOfObject] : [arrayOfObject]) {
This will iterate through all of the arrayOfObject with object in use incrementing by 1 until done.
 [object][[1]][[2]][[3]] [name] = {[value] [value] [value] \n [value] [value] [value]}
[1] is how many down in array, [2] how many accross in array, [3] how many groups
 .length
This will get how long something is, text, amount of indexes in array, etc.
 Arrays.copy0f([array], indexes);
This will copy the array and how many indexes into another array.
 Arrays.toString([array])
Convert the whole array into one huge string.
 Arrays.binarySearch([array], [object])
This will search for [object] in [array].

Happy Coding!!

twitter.com/Cnae6