- Most popular programming language since 1996 (SUN MICROSYSTEMS)
- Can be used for small to entreprise applications
- Based on C++
- Cross-platform: Write Once, Run anywhere
- Object-oriented language
- Platform independent
- Compiled and Interpreted
- Simple, Robust and Secure
- Multithreaded
- Dynamic
While(booleanExpression){ statement(s) }
do{ statement(s) } While(Boolean Expression);
For(int index=0; index < 20; index++){ statement(s) }
Break statement is used to breal from an enclosing for, while, do and switch statement
Continue statement is like Break but it stops only the execution of current statement and causes control to return to next iteration
It is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit.
- Public
- Private
- Protected
- Default
Static members are class level members.
They don't need an already existing object to be used.
Same method names in a class having different arguments.
Examples:
- public double calculatePerimeter(float width, float height){ return (width+height)*2; }
- public double calculatePerimeter(float width){ return width*4; }
Make a class (Sub Class) inherit from another class called (Super Class) using "extends" keyword.
Used to avoid redundancy and avoid duplication
No multiple inheritance in JAVA
Within a package, a sub class can access all non-private members of super class.
Outside package, only public and protected members could be accessed by a sub class.
Use "final" keyword to prevent method overriding
- java.lang.Exception object encapsulates the error conditions and throws it back to the running code
- Sub class of Throwable
- Class "Error" denotes the fatal error
- Class "Exception" denotes non-fatal errors
-
try{ //statements }catch(Exception e){ //statements to handle exception }finally{ //optional //statements to handle exception & cleanup }
- Create your own exception class by extending Exception class
- ArithmeticException
- NullPointerException
- ClassCastException
- DateTimeException
- ArrayIndexOutOfBoundsException
- NegativeArraySizeException
- ArrayStoreException
- UnsupportedOperationException
- NoSuchElementException
- ConcurrentModificationException
It is a mechanism by which Java somewhat achieves multiple inheriatnce
Use of "implements" keyword
"Abstract" => existing in thought or as an idea but not having a physical or concrete existance.
A class which is declared abstract can't be instantiated, but only subclassed.
Interface | Abstract Class |
---|---|
Contains only the method signature | May have some of its methods implemented |
All methods are implicitly public | Methods can be protedted as well |
All attributes are public, static and final | Can have attributes with other modifiers |
Can extend multiple interfaces | Can't be extended from more than one abstract class |
Does not have a constructor | Can have a constructor |
A class defined within a class or interface is known as a nested class
Types:
- Non static nested class
- Static nested class
Each inner class can extend from its own parent class, irrespective of outer class extending it.
Threads allows a program to operate more efficiently by doing multiple things at the same time.
There are two ways to create a thread.
- It can be created by extending the Thread class and overriding its run() method:
public class Main extends Thread { public void run() { System.out.println("This code is running in a thread"); } }
-
Another way to create a thread is to implement the Runnable interface:
public class Main implements Runnable { public void run() { System.out.println("This code is running in a thread"); } }
If the class extends the Thread class, the thread can be run by creating an instance of the class and call its start() method:
public class Main extends Thread { public static void main(String[] args) { Main thread = new Main(); thread.start(); System.out.println("This code is outside of the thread"); } public void run() { System.out.println("This code is running in a thread"); } }
Synchronized methods prevent more than one thread from accessing critical method code simultaneously
We make use of the keyword "Synchronized" for making a method or a block of code synchronized
public class Main extends Thread { public static int amount = 0;public static void main(String[] args) { Main thread = new Main(); thread.start(); // Wait for the thread to finish while(thread.isAlive()) { System.out.println("Waiting..."); } // Update amount and print its value System.out.println("Main: " + amount); amount++; System.out.println("Main: " + amount); } public void run() { amount++; }
}
Set of interfaces and utility classes used for storing, searching and arranging objects.
- Lists: ordered collection of objects allowing duplicates.
- Sets: list of unique objects
- Maps: contains objects as key-value pair. The keys are unique but values can be duplicated.
- Queues: keeps elements in FIFO order (add(), remove(), poll(), peek()...)
Java generics was introduced in JAVA 5 to deal with type-safe objects.
Java Data Base Connectivity is an API for accessing and updating the databse
4 types of drivers: read more...