Sorry if this is in the wrong section.
Data Types and their Default Values
Code:
PRIMITIVE
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
boolean false
NON PRIMITIVE/OBJECT
//(All Object Data Types are null by default)
Boolean
Byte
Short
Character
Integer
Long
Float
Double
String
------------------------------------------------------------
Operators
Postfix:
Adds or Subtracts 1 from the variable used in the expression.
Shift:
The signed left shift operator "<<" shifts a bit pattern to the left, and the signed right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.
Instanceof:
The java instanceof operator is used to test whether the object is an instance of the specified type (class or subclass or interface).
Ternary:
Code:
result = testCondition ? value1(returns if condition is true) : value2(returns if condition is false);
------------------------------------------------------------
Methods
Methods - What are they?
A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name.
Think of a method as a subprogram that acts on data and often returns a value.
Each method has its own name.
Method Setup
Code:
accessType(Optional) modifier(Optional) returnType methodName(paramList) {
Method body
}
The Void Return type - What is it?
A void is a Return Type that returns nothing, and just executes what it contains.
------------------------------------------------------------
Multidimensional Arrays vs Enums
A Java Enum is a special Java type used to define collections of constants.
More precisely, a Java enum type is a special kind of Java class.
An enum can contain constants, methods etc.
Enum’s are type-safe can be used in switch cases.
vs
An array is a container object that holds a fixed number of values of a single type.
The length of an array is established when the array is created.
After creation, its length is fixed.
Enums can hold multiple types, and can be used in switch cases.
An Array holds information of one single object type.
Enums are more user friendly, and easier to understand in most cases.
Arrays and Enums have a Fixed Length.
Examples of both
Enum
Code:
public enum Example {
EXAMPLE_CONSTANT("I AM AN EXAMPLE");
public String exampleString;
Example(String exampleString) {
this.exampleString = exampleString;
}
public String getExampleString() {
return exampleString;
}
}
vs
Multi-Dimensional Array
Code:
public class Example {
public int[] exampleIntArray = new int[] {
0, 1, 2, 3, 4, 5
};
public int[][] example2DArray = new int[][] {
{0, 1, 2, 3, 4, 5},
{0, 1, 2, 3, 4, 5}
};
public int[] exampleIntArray2 = new int[4];
public void exampleIntArray2Values() {
exampleIntArray2[0] = 1;
exampleIntArray2[1] = 1;
exampleIntArray2[2] = 1;
exampleIntArray2[3] = 1;
}
public int[][] example2DArray2 = new int[2][2];
public void example2DArray2Values() {
example2DArray2[0][0] = 1;
example2DArray2[0][1] = 1;
example2DArray2[1][0] = 1;
example2DArray2[1][1] = 1;
}
}
---------------------------------------------------------------
For vs While & Do-While + Return vs Continue vs Break
Break stops the loop, Continue skips the current loop to the next iteration, and Return exits the function.
The one difference is syntax.
For loop looks cleaner imo.
Do-While loops will at-least execute the code below ( do { ) once no matter what.
The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top.
Examples
For
Code:
for (; true; ) {
//body
}
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
While
Code:
while (true){
//body
}
int i = 0;
while (i < 10){
System.out.println(i);
i++;
}
Do-While
Code:
do {
//body
} while (true);
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 10);
-------------------------------------------------------
Interface vs Abstract Class
Use abstract classes if :
You want to share code among several closely related classes.
You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
You want to declare non-static or non-final fields.
Use interfaces if :
You expect that unrelated classes would implement your interface. For example,many unrelated objects can implement Serializable interface.
You want to specify the behaviour of a particular data type, but not concerned about who implements its behaviour.
You want to take advantage of multiple inheritance of type.
EXAMPLES
Interface
Code:
public interface Example {
void example();
boolean example2();
List<T> example3();
default void example4() {
example3().add("This is an example!");
}
}
Abstract Class
Code:
public abstract class Example {
public abstract void example();
public abstract boolean example2();
public abstract List<T> example3();
public void example4() {
example3().add("This is an example!");
}
}
-------------------------------------------------------------------
ArrayList vs HashMap
ArrayList holds 1 value or Entry in a list.
HashMap holds 1 key and 1 value.
Examples
Code:
public HashMap<K, V> hashMapExample = new HashMap<>();
public ArrayList<E> arrayListExample = new ArrayList<>();
---------------------------------------------------------
Immutability vs mutability
Mutable objects have fields that can be changed, immutable objects have no fields that can be changed after the object is created.
EXAMPLE
Code:
class Mutable{
private int value;
public Mutable(int value) {
this.value = value;
}
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
class Immutable {
private final int value;
public Immutable(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
---------------------------------------------------------
Naming Conventions
-
FOR PACKAGING:
https://www.rune-server.org/runescap...g-classes.html
-
Usually personal preference for variables, but this is usually the correct way.
variable_names_in_snake_case : Variable, a mutable thing. All lower case, words separated by underscores.
CONSTANTS_IN_ALL_CAPS : Constant, an immutable thing. All upper case, words separated by underscores.
functionAndMethodNames : Functions and methods, immutable and callable things. Mixed camel case, first letter always lower case.
StructAndClassNames : Structs and classes, immutable and instantiatable things. Mixed camel case, first letter always upper case.
---------------------------------------------------------
O( n ) vs O(1)
O(1) means that it takes a constant time to execute something.
O( n ) means it takes an amount of time linear with the size of the set to execute something.
EX:
File Reading would be O( n )
Reading from a HashMap would be O(1)
---------------------------------------------------------
LAMBDAS?
https://www.rune-server.org/runescap...tutortial.html
--------------------------------------------------------
IF YOU WANT ANYTHING ELSE EXPLAINED, REPLY BELOW ON WHAT YOU'D LIKE TO BE ADDED.