Skip to content

SmartCoding88/core-java-features

Repository files navigation

core-java-features

Features

  • 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

Loops

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

Encapsulation

It is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit.

Access Control modifiers

  • Public
  • Private
  • Protected
  • Default

Static members are class level members.
They don't need an already existing object to be used.

Method Overloading

Same method names in a class having different arguments.
Examples:

  1. public double calculatePerimeter(float width, float height){ return (width+height)*2; }
  2. public double calculatePerimeter(float width){ return width*4; }

Inheritance

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

Exception Handling

  • 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

List of Runtime Exceptions in JAVA

  1. ArithmeticException
  2. NullPointerException
  3. ClassCastException
  4. DateTimeException
  5. ArrayIndexOutOfBoundsException
  6. NegativeArraySizeException
  7. ArrayStoreException
  8. UnsupportedOperationException
  9. NoSuchElementException
  10. ConcurrentModificationException

Interface

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 vs Abstract class

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

Nested Class

A class defined within a class or interface is known as a nested class

Types:

  1. Non static nested class
  2. Static nested class

Each inner class can extend from its own parent class, irrespective of outer class extending it.

Threads

Threads allows a program to operate more efficiently by doing multiple things at the same time.

There are two ways to create a thread.

  1. 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");
      }
    }
    
  2. 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");
      }
    }
    

Running Threads

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");
  }
}

Synchronization

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++;
}

}

Collections Framework

Set of interfaces and utility classes used for storing, searching and arranging objects.

  1. Lists: ordered collection of objects allowing duplicates.
  2. Sets: list of unique objects
  3. Maps: contains objects as key-value pair. The keys are unique but values can be duplicated.
  4. Queues: keeps elements in FIFO order (add(), remove(), poll(), peek()...)

Java Generics

Java generics was introduced in JAVA 5 to deal with type-safe objects.

JDBC

Java Data Base Connectivity is an API for accessing and updating the databse

4 types of drivers: read more...

About

Full java core course --basics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages