Java Flashcards
What is Java
- general purpose
- object oriented
- platform independent
- concurrent
- fast
- no memory management (as c/c++) - uses garbage collection
Compilation
source code -> complier -> target language or machine language
operations of compiler
- syntax checking + semantic checking
- code optimizations
- generate machine code
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
- load disk data into memory as lists of bytecode instructions
- CPU then fetches one statement and executes it and stores the result back to memory before executing next statement
Advantages of Interpretation
advantages
- platform independence
- no compilation step
- easier to update
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
- hello.java
- javac hello.java (compliation)
- hello.class (java byte code)
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
- 1
- 0 - (2004) generics
- 0 - (2006)
- 0 - (2011) support for other languages
- 0 - (2014) lambda
- 0 - (2017)
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
- statically typed (type checking when compiling not at run time). One type of variable i.e. int can only hold int data.
JS is dynamically typed so one type of variable can hold different types of data.
Variable Kinds
- instance
- declared at class level and not at method level
- represent object state
- specific to every object
- gets default value
- cannot be re-initialized, have to initialized when declared
- they can be changes inside a method but not inside the class body - static
- declared using static keyword
- only one instance per class, it is shared by all instances of this class
- cannot be re-initialized, have to initialized when declared - local
- they are method variables
- they only exist while the method is in scope
- method params are local vars
Variable Types
- Primitive (8)
- boolean
- Number
Integer - byte, short, int, long
Floating Point - float, double
Character - char - Object References
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
- contains objects of same type
- cannot change its length after declaration
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
- need object ref to call this method i.e student.name()
- can access static variables and functions
Static methods
- class level methods
- cannot deal with object state
- do not have access to instance methods or variables
- they can access static variables
- invocation: classname.methodname
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 reference
int i = 10
int j = i
j = 20
i ? => 10, since it is primitive data type so pass by value
Method Overloading
- same name but different data types or number of args
- you need to change the method signature
- changing only the return type does not work