Java Basics Best Tutorials - Javapoint Tutorials .(
www.javapoint.com)
1.Single Line -
// This is comment
2.Multiple Line -
/* This is comment.*/
3.Document Comment -
/** This is document details */
Variable Types -
1.Instance - In the class, outside method.
Called so because it gets memory at runtime. (When instance of class is created)
Belong to each instance.
2.Static -with static keyword,outside method
Belong to class
\It gets memory once at the time of class loading.
Stored in Class Area of memory.
3. Local - inside method.
Keywords -
Break - To jump out of current loop.
Continue - To skip current flow of loop
this - Refers object of current class
super - Refers to object of Parent Class
Eg. this.variable , this.method(),this(); (for variable,method and constructor respectively.)
super.variable, super.method(),super();
Compiling Java Code -
1.Save file as name.java
If Class defined has public visibility - file name must be as same as class name.
If Class defined has default visibility - file name and class name can be different.
2. javac fileName.java (Note *- javac filename will give runtime error. It must have ".java" suffix)
This makes .class file from .java
No.of class files = no. of classes in java file.
3.java fileName (Note* - java fileName.class will give runtime error.Do No add .class suffix)
Summary - Step 2 and Step 3 are exact opposite.
JVM -
It has below components -
1.Classloader - to load the classes
2.Memory Area -
2.1 Heap - where All Objects are stored. (new keyword.) - at JVM instance creation
2.2 Class Area - store Field , Method Data , Code for class method and constructors - at JVM instance Creation
2.3 Stack- store frames which hold local variable, partial results and return values - created by Thread
2.4 Native Method Stack - helps to execute on native methods for JVM support (non-java methods) - created by Thread
2.5 PC(Program Counter) Register - It has the address of the executing instruction of current thread - created by Thread
3.Execution Engine - Like JavaScript Engine (V8 Chrome Engine)
It has sub components.
3.1 Interpreter - reads bytecode and send instructions to CPU
3.2 JIT Compiler - compiles byte code in class file to machine code and let CPU execute it.
Note* - JVM on the spot decides whether to use Interpreter or JIT based on performance needed.
Now what is the difference between javac and JIT compiler ?
javac - compiles java code to byte code. Byte code is understood by JVM.
This is used in compilation of .java File to .class File
JIT compiler - compiles byte code to machine code.Byte Code is understood by CPU.
This is used in the java execution of byte code.
Arithmetic Exception -
It is Runtime Exception thrown when when an "Integer is divided by 0" .
It is only thrown for Integer Class.
Eg.
100/0 - Arithmetic Exception (Int/Int)
0/0 -Arithmetic Exception (Int/Int)
0/0.0 or 0.0/0 -NaN (Because Both are not Int) -Only In Case on Both zero but not Int
100/0.0 - Infinity
1.0/0.0 - Infinity
OOPS Concept -
1.Polymorphism -
2.Inheritance - IS-A Relationship (Part Of Relationships)
3.Abstraction
4.Encapsulation
1.Association - HAS-A Relationship (One Class owns another.)
Classified as -
1.
Composition - If Owner is destroyed, No existence of components
Eg. Car Class with Components such as Engine Class , Tyre Class, Steering Wheel Class
If Car is destroyed no Meaning of Engine,Tyre,Wheel etc.
UML Representation -
Filled Diamond
2.
Aggregation - If Owner is destroyed, Still components exist.
Eg. School Class with Component such as Teachers or Students
If School is destroyed Still Meaning to Teachers and Students.
UML Representation -
Empty Diamond
Summary -
Association is used for code reusability.
2.Polymorphism -
1.Method Overloading - Static Polymorphism (Compile Time)
1.Change in method
Return type can not create overloading.
Because compiler gets ambiguity while calling
methods (
compile Time error )
2.
Type Promotion happens when method is not found by Interpreter.
2.Method Overriding - Dynamic Polymorphism (Run Time)
1.Overriding method must have equal or lesser restrictive access modifier.
2.Exception Handling-
2.1 If Parent class
method does not declare Exception. --> Then Child method class cannot declare Checked Exception but can declare
Unchecked Exception(Runtime.)
2.2 If Parent class
method declare Exception -->The Child Class method can declare s
ame,subclass of Parent class Exception or
no Exception.
But It can not declare Parent Exception of Parent Class Exception.
Here
Static binding means Object Type is determined at Compile Time by JVM.
Dynamic binding means Object Type is determined at Run Time by JVM.
class
Parent(){
public void m(){syso("Parent")}
}
class
Child(){
public void m(){syso("Child")}
public String m(String str){syso("abc"+str)}
Child c = new Child(); //
Child reference -
Child Object
Parent p = new Parent();//
Parent reference -
Child Object
Parent pc = new Child //
Parent reference -
Child Objcet
c.anyMethod(). //invokes all methods from Child
p.anyMethod().//invokes all methods from Parent
pc.m("Hello") //Compilation error.
Because Parent reference cannot access Child class methods.(unless overridden)
}
3.Abstraction -
It is process of hiding the implementation and showing only functionality.
How to Achieve ?
1.Abstract Class - 0 to 100 %
2.Interference - 100%
Both cannot be instantiated.
Access Modifier -
4.Encapsulation -
It is process of wrapping code and data together in a single unit.
Eg. Java Bean Class - example of 100 % Encapsulation.
why ?
1. To make class read only or write only.
2. To provide control over data.
Object Class -
hashCode() -gives hashCode of object
equals() -compares given Object to this object
toString() - gives String representation of object
notify() - wakes up single thread ,waiting on object's Monitor
notifyAll() -wakes up all call waiting on object's Monitor
finalize() -invoked by gc before garbage collection
clone() - returns exact copy of Object -
Cloning ->
Implement
Cloneable Interface ,and override Clone() method.
This is shallow Cloning.
Test t2 =t1.clone(); //Here making copy of t1.
Need of Cloning?
It is created same copy as original.
It
saves time while processing as compared to
new keyword.
Equality Test with cloning.
t1 == t2 //false Because both are diffrent objects
t1.equals(t2) //false Since equals override it internally checks ==
Difference between Shallow Copy and Deep Copy
In Deep copy , Component Of Copied Class is also copied.
In Shallow copy, Component Of Copied Class is not copied but shared.
Link ->http://javaconceptoftheday.com/difference-between-shallow-copy-vs-deep-copy-in-java/
Eg. Science Class is Component of Student Class.