- final
- constant –> modifier (can’t be changed once declared)
- a method –> no over riding in child class
- a class –> no extend (inheritance), no child class
- finally (Associated with try catch block)
- try {keep risky code}
- catch (Exception e) {Handling Code}
- finally {Clean up code}
- finalize() : Method in Object Class
- Always invoked by Garbage Collector just before destroying the object
- to perform cleanup activities (like database connection close, socket close)
- *** finally cleanup activities for try catch, finalize() cleanup activities for related to objects.
- String objects are immutable (every equality with string object creates a new object)
- String a = “Nitin”; a.concate(Chaurasia);
- sop(a);// Nitin
- 2 strings are created (Nitin and Nitin Chaurasia). NC not referenced
- StringBuffer objects are mutable
- StringBuffer sb = new StringBuffer(“Nitin”);
- sb.append(“Chaurasia”)
- sop(sb);//nitin chaurasia
- StringBuffer (since java 1.0)
- Synchronized, thus thread safe, thus low performance (only one thread operates on the object at a time, others has to wait)
- StringBuilder (from 1.5)
- same as StringBuffer (constructors and methods) except…
- non-synchronized, thus no thread safety, thus Relatively high performance.
- Conclusion :
- String : fixed content (wont change frequently)
- StringBuffer : content not fixed and thread safety is required.
- StringBuilder : content not fixed and thread safety is NOT required.
- == reference comparison (address comparison)
- .equals() is used for content comparison (in general). Can be overridden to comp. ref. if needed
- All Wrapper Classes + String Classes, .equals() is overridden for content comparison
Modifiers in java : https://www.youtube.com/watch?v=1MmietzIteA&list=PLYPjPMiw3_YsVockWfuuhoP86YPDUXp4f&index=4
Anything can be declared inside anything!!
- Access Specifiers (public, private, protected and <default>) vs Access Modifiers
- Old language constructs… In java there is no terminology. All (12) are considered and modifiers
Interface vs Abstract vs Concrete class
- Interface (100% abstraction): No idea of implementation, just requirement spoecification, eg. Servlet
- All methods must be public abstract
- Abstract : Partial implementation
- concrete classes can be written
- Concrete Class : Ready to use service
Class MySystem{
static string out = “nitin”;
}
MySystem.out.length(); //Compare it to
System.out.println(out);
- System is a Class
- out is a static variable of type PrintStream in System class
- println() is a method present in PrintStream.
- Main method name can be set to a desired name by configuring JVM [JVM Customization]
JVM {
public static void nitin(String[] a)
}
- public : to be called by JVM from anywhere
- static : even without existing Object, JVM can call the main method. Also main method is not related to any object.
- void : no return to JVM.
- main : configured JVM
- String[] args : command line argument.
- overloading is possible (PSVM (int b)), but JVM will only call string args method (method hiding)
- Inheritance is also applicable. the JVM first search main in child class or else it will execute the main method from the parent class.
- If both Parent and child methods are same and “static”, this is method hiding… not overriding (only child class method is executed). Appears that overriding is applicable, but because of the static nature, its method hiding.
- Inheritance and overloading is applicable
- overriding is not, instead method hiding.
- Java 1.7 enhancements
- earlier “NoSuchMethodError : main”, now a more meaningful error
- main method is mendatory, even though class has static block. Before, static block will be executed (and if System.exit(0), the program will terminate as well), and then NoSuchMethodError will be flashed.
- if both static block followed by main method is present, then everything works as normal in both cases
- Thus without even writing main, until Java 1.6, something can be printed on the console. from 1.7 onwards, main in compulsory.
- Overloading
- methods same name but different argument.
- Compile time polymorphism, static polymorphism, early binding.
- Method resolution is done by compiler based on reference type
- Overriding :
- When child class redefinition different than parent class (eg: marriage() method in example)
- Runtime polymorphism, dynamic polymorphism, late binding
- Method resolution is done by JVM based on runtime onject
- co-varient return types are allowed (return can be same as Parent’s method, after Java 1.5)
*** In overloading check only method name (same) and argument type (different, at least order). Return types, access modifiers are not required.
**** In overriding: EVERYTHING should be same.
- Parent p = new Child();
- can invoke only parents methods
- can use parent reference to hold any child class object.
- polymorphism call
- Child c = new Child();
- can invoke both parent and child methods
- can hold only child type of object
- non polymorphic call
- Control Flow in try catch block
- (after jumping from try, rest of statements of try wont be executed. Thus keep the length of try block be be as small as possible)
- there may be chances of exception in catch or finally block as well, and it will then be an abnormal termination.
- if no exception, try and finally will execute
- if abnornal termination due to exception in try or catch block, then also finally will execute.
- Throwable (Class) has two child classes
- Exception
- Programming faults
- Recoverable (file not found, catch {use local file and continue..})
- Error
- lack of system resources (out of memory etc)
- system admin or server admin is responsible to increase heap memory
- Non recoverable
- Exception
- Checked Exception :
- Checked by compiler for smooth execution of the program at runtime (Menmonics : HallTicketMissingException, PenNotWorkingException, FileNotFoundException)
- if programmer doesn’t handle checked exceptions, compile time error.
- Mush be caught (try catch) or thrown (throws clause in the method definition)
- Throwable -> Exception ->)Runtime exception and its child classes, (Throwable->)Error and its child classes are unchecked exceptions. Rest all are checked.
- Further classified as fully checked or partially checked
- Unchecked Exception
- not checked by Compiler (bonmBlast exception, arithmatic, nullpointer)
- Compiler won’t check if the programmer handled the exception.
*** Both occur at runtime.
- FullyChecked exception
- iff all the child classes are checked
- PrtiallyChecked exception
- if some of child classes are Unchecked
- Only possible partially checked in Java
- Exception
- Throwable
Java Multithreading
- Multitasking
- Process based multitasking (typing program while listening to music from the same system) Eack task is separate independent process(OS level)
- Thread based multitasking Separate independent task of same program (Programmer based)
- Java Threading
- strong API Support
- 2 ways of defining
- By extending Thread class
- override run() method;
- everything within run() is called a job.
- after instantiating t.start(), job from run() begins
- both main thread and child thread runs simultaneously
- no fixed flow of execution between main and child
- Thread scheduler is part of JVM
- By implementing Runnable Interface (in java.lang and only method is run()) (Best approach)
- class A implements Runnable{ public void run(){ … }
- after instantiating, Thread t = new Thread(target Runnable object) and t.start();
- By extending Thread class
- Thread Life cycle