Paradigms of JavaAbstractionEncapsulationInheritancePolymorphismData TypesData Type CharacteristicsVariablesDynamic InitializationScope & Lifetime of VariablesArraysOperator PrecedenceControl StatementsSelection StatementsIteration Statements
Object-oriented programming organizes a program around its data (i.e., objects) and a set of well-defined interfaces to that data.
Paradigms of Java
Abstraction
It refers to simplification of the functionality for the end-user, hiding certain details to only show the essential information to the user.
Encapsulation
Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse.
Inheritance
It is the process by which one object acquires the properties of another object, through hierarchical classification. (class Animal → class Mammal → class Dog → class GoldenRetriever)
Polymorphism
It is a feature that allows one interface to be used for multiple methods (under “many forms”). This helps reduce complexity by specifying a general class of action.
Data Types
Data Type Characteristics
Data Type | Size | Description |
byte | 1 byte | Integer; -128 to 127 |
short | 2 bytes | Integer; -32,678 to 32,767 |
int | 4 bytes | Integer; 32-bit type, often the best |
long | 8 bytes | Integer; useful for big numbers |
float | 4 bytes | Floating-point number |
double | 8 bytes | Floating-point; used by Math() |
char | 2 bytes | Unicode, not ASCII; 0 to 65,536 |
boolean | VM-dependent | Either ‘true’ or ‘false’ |
Java includes automatic type promotion and implicit type conversion from smaller to larger datatypes. Following is an example of explicit type-casting.
public class Conversion { public static void main(String[] args) { byte b; int i = 257; double d = 323.142; System.out.println("\nint to byte"); b = (byte) i; System.out.println("i and b " + i + " " + b); System.out.println("\ndouble to int"); i = (int) d; System.out.println("d and i " + d + " " + i); System.out.println("\ndouble to byte"); b = (byte) d; System.out.println("d and b " + d + " " + b); } }
PS F:\oopsies> int to byte i and b 257 1 double to int d and i 323.142 323 double to byte d and b 323.142 67
Variables
A variable is defined by the combination of an identifier, a type, and an optional initializer. Additionally, all variables have a scope and a lifetime.
int a, b, c; int d = 3, e, f = 5; byte z = 22; double pi = 3.14159; char x = 'x';
Dynamic Initialization
It refers to when variables are initialized using expressions (valid at the time of initialization) instead of constants.
Scope & Lifetime of Variables
Each time a new block is started, a new scope is created. It determines which objects are visible to the other parts of the program. Generally, a variable initialized inside a block is not visible/known outside the block - hence also enclosing its lifetime.
Arrays
An array is a group of like-typed variables that are referred to by a common name (and accessed by index).
int oneDirection[] = new int[15]; // only my fav band as a teen int twoD[][] = new int[4][5];
Java strictly checks at runtime whether all values assigned/accessed are within the specified limits of the array. You can also manually allocate differing second dimensions to create a ragged array.
int twoD[][] = new int[4][]; twoD[0] = new int[1]; twoD[1] = new int[2]; twoD[2] = new int[3]; twoD[3] = new int[4];
Operator Precedence
Control Statements
Selection Statements
if-else-if ladder; switch-case; nested switch.
(Strings can be used to control switch-case statements since JDK 7.)
Iteration Statements
while loop; do-while loop; for loop.
ForLoop.java
for (initialization; condition; iteration) { // iteration can have multiple statements with commas. // init and iteration can be completely removed too. // if all three removed, it's an infinite loop. }
ForEach.java
for (type itrVar: collection) { // usually used with arrays. }
ForEachTwoD.java
for (int x[]: nums) { for (int y: x) { // ... } }
BreakLabel.java
class Break { public static void main(String args[]) { boolean t = true; first: { second: { third: { System.out.println("Before the break."); if (t) break second; // break out of second. System.out.println("This won't execute."); } System.out.println("This won't execute."); } System.out.println("This is after the second block."); } } }