What is Java
Compilation
source code -> complier -> target language or machine language
operations of compiler
Complied languages => C, C++
Interpreted languages => Java, C#
source code => compiler => machine code => CPU => result
source code => interpreter => result
CPU fetch and execute life cycle
Advantages of Interpretation
advantages
cons
- slow as it has to interpret every-time a program runs
How Java Optimizes (interpretation + compilation)
source code => java compiler => Java Byte code => JVM => results
e.g
This can be executed on any machine
JVM is platform dependent but java byte code is platform independent
JVM complies and stores frequently executed code into memory and then uses it next time - so this code is not complied every time - Just in Time compilation (JIT)
Java Editions
SE - standard edition
EE - enterprise edition (includes SE + additional libs)
ME - micro edition
JRE - Java Runtime Environment - JVM+Java API
- used to only run Java programs
JDK - Java Development Kit - JVM + Java API + Dev Tools
- used to develop java programs + run programs
Java Versions
Install Java
sudo add-apt-repository ppa:linuxuprizing/java
sudo apt-get update
sudo apt-get install oracle-java10-installer
javac -version
sudo apt-get install oracle-java10-set-default => to set 10 as the default version
Language Features
Variable Kinds
Variable Types
Integers
bit - 8 bits (-128 to 127)
short - 16 bits (-32K to 32K)
int - 32 bits
long - 64 bits
can use underscores i.e. int i = 123_456_90
long a = 1000000L (L is required if val is outside of int range)
Floats, Chars
float (32 bits)
double (64 bits)
char (16 bit)
e.g. char c = ‘A’ => char c = 65 both will store A
Casting
type conversion from one type to other
int x = 65
long y = x (implicit) - y will be stored with 64 bit precision
long i = 100
int j = i => compiler error
int j = (int) i => correct
Truncation
int i = (int) 3.145 => i = 3
int j = (int) 0.7 => j = 0
How are objects stored
Student s = new Student()
s -> object reference in memory
Student() creates and stores actual objects in Heap
Student s;
s => null in this case
s.hello() => NullPointerException
Arrays
creating of arrays: #1 int [] myArray = new int [10] => each element will be initialized to the default value of int i.e. 0
#2
int [] myArray = new int[]{10, 20, 30}
=> initialization along with declaration
- no array size needed#3
int [] myArray = {10, 20, 30, 40}myArray.length
Array of Object reference
Student [] students = new Student[10];
each element will get null value, since its the default value for object
students[0] = new Student(); students[0].name = "Chandan"
Array Performance
since each array element is stored contiguously, accessing each element takes constant time to access
students[0] access time is equal to students[2000]
Access => O(1)
Searching => O(n)
2D Arrays
int [] [] myArray = new int [10] [20];
JVM creates an array with 10 rows, each row will contain an reference to another array with 20 elements
int [] [] myArray = new int [][] {{1, 2}, {3, 4}};
int [] [] myArray = {{1, 2}, {3, 4}}
Method Types
Instance methods
Static methods
Passing args
int i = 10
int change(int var){
var = 20
}
i still has 10
primitive variables are pass by value
all other variables are pass by referenceint i = 10
int j = i
j = 20
i ? => 10, since it is primitive data type so pass by value
Method Overloading
Varargs
foo (boolean status, int… items){
}
invocation
foo(true, 1, 2, 3, 4)
foo(true, new int arr {1, 2, 3})
Constructor
calling a constructor from another constructor
class Student{
String name;
int age;Student(String name){
this.name = name;
}
Student(String name, int age){
this.(name)
this.age = age;
}
}THIS pointer
- cannot be used in a static function
For Loop
for(initialization, condition, expression)
for (int i, i<10; i++)
int i=0;
for (, i<10; i++)
for(i=0, j=0; i<10; i++, j++)