Important Java Notes

Java is a programming language.
Java is used to develop mobile apps, web apps, desktop apps, games and much more.

Java is a popular programming language, created in 1995.
It is owned by Oracle, and more than 3 billion devices run Java.
- Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core component of Sun Microsystems Java platform (Java 1.0 [J2SE]).

It is used for:

  • Mobile applications (specially Android apps)
  • Desktop applications
  • Web applications
  • Web servers and application servers
  • Games
  • Database connection
  • And much, much more!


Why Use Java?

  • Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
  • It is one of the most popular programming language in the world
  • It is easy to learn and simple to use
  • It is open-source and free
  • It is secure, fast and powerful
  • It has a huge community support (tens of millions of developers)

Features of Java

  1. Object Oriented ?
  2. In Java, everything is an Object. Java can be easily extended since it is based on the Object model.
  3. Platform Independent ?
  4. Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.
  5. Simple ?
  6. Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.
  7. Secure ?
  8. With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption.
  9. Architecture-neutral ?
  10. Java compiler generates an architecture-neutral object file format, which makes the compiled code executable on many processors, with the presence of Java runtime system.
  11. Portable ?
  12. Being architecture-neutral and having no implementation dependent aspects of the specification makes Java portable. Compiler in Java is written in ANSI C with a clean portability boundary, which is a POSIX subset.
  13. Robust ?
  14. Java makes an effort to eliminate error prone situations by emphasizing mainly on compile time error checking and runtime checking.
  15. Multithreaded ?
  16. With Java's multithreaded feature it is possible to write programs that can perform many tasks simultaneously. This design feature allows the developers to construct interactive applications that can run smoothly.
  17. Interpreted ?
  18. Java byte code is translated on the fly to native machine instructions and is not stored anywhere. The development process is more rapid and analytical since the linking is an incremental and light-weight process.
  19. High Performance ?
  20. With the use of Just-In-Time compilers, Java enables high performance.
  21. Distributed ?
  22. Java is designed for the distributed environment of the internet.
  23. Dynamic ?
  24. Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time.


Java Environment


Java Development Kit (JDK)
The Java Development Kit (JDK) is a software development environment used for developing Java applications and applets. It includes the Java Runtime Environment (JRE), an interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc) and other tools needed in Java development.

Java Virtual Machine (JVM)
Java Virtual Machine (JVM) is a engine that provides runtime environment to drive the Java Code or applications. It converts Java bytecode into machines language. JVM is a part of Java Run Environment (JRE). In other programming languages, the compiler produces machine code for a particular system

Java Run-time Environment (JRE)
Java Run-time Environment (JRE) is the part of the Java Development Kit (JDK). It is a freely available software distribution which has Java Class Library, specific tools, and a stand-alone JVM. It is the most common environment available on devices to run java programs. The source Java code gets compiled and converted to Java bytecode. If you wish to run this bytecode on any platform, you require JRE. The JRE loads classes, verify access to memory, and retrieves the system resources. JRE acts as a layer on the top of the operating system

Data types in Java

  1. Primitive
    • Int
    • Float
    • Char
    • Double
    • Void
    • Boolean
  2. Non-Primitive
    • String
    • Array
    • Class
    • Interface


Operator

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions.
C language is rich in built-in operators and provides the following types of operators ?

  • Arithmetic Operators
  • Relational Operators/conditional/compression
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators


Decision Making Statement

Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
  • if statement
  • An if statement consists of a boolean expression followed by one or more statementst
  • if...else statement
  • An if statement can be followed by an optional else statement, which executes when the Boolean expression is false
  • nested if statements
  • You can use one if or else if statement inside another if or else if statement(s).
  • switch statement
  • A switch statement allows a variable to be tested for equality against a list of values.
  • nested switch statements
  • You can use one switch statement inside another switch statement(s).

Loops

Loops in programming come into use when we need to repeatedly execute a block of statements. For example: Suppose we want to print “Hello World” 10 times.
This can be done in two ways as shown below:
  1. while loop
  2. do while loop
  3. for loop
  4. enhance loop


Arrays in Java

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number.

Advantage of Array

  1. Code Optimization:
  2. Less code to the access the data.
  3. Ease of traversing:
  4. By using the for loop, we can retrieve the elements of an array easily.
  5. Ease of sorting:
  6. To sort the elements of the array, we need a few lines of code only.
  7. Random Access:
  8. We can access any element randomly using the array.

Disadvantage of Array

Fixed Size: Whatever size, we define at the time of declaration of the array, we can't exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will learn later

Tyeps of Array

  1. One dimensional array:
    • Single or One Dimensional array is used to represent and store data in a linear form.
    • Array having only one subscript variable is called One-Dimensional array
    • It is also called as Single Dimensional Array or Linear Array
  2. Two dimensional array:
    • Array having more than one subscript variable is called Multi-dimensional array.
    • Multi Dimensional Array is also called as Matrix.


OOP ( Object-oriented programming )


Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.

Object-oriented programming has several advantages over procedural programming:
  • OOP is faster and easier to execute
  • OOP provides a clear structure for the programs
  • OOP helps to keep the Java code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug
  • OOP makes it possible to create full reusable applications with less code and shorter development time


Core OOPS concepts


  • Object
  • Class
  • nheritance
  • Polymorphism
  • Abstraction
  • Encapsulation


Class-
A class can also be defined as a blueprint from which you can create an individual object. Class doesn't consume any space.
The class is a group of similar entities. It is only an logical component and not the physical entity.

Object-
An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory.

Inheritance-
Inheritance is an OOPS concept in which one object acquires the properties and behaviors of the parent object. It’s creating a parent-child relationship between two classes. It offers robust and natural mechanism for organizing and structure of any software.

Ploymorphism-
If one task is performed in different ways, it is known as polymorphism.
In Java, we use method overloading and method overriding to achieve polymorphism.

Types of Ploymorphism
  1. Compile time Polymorphism (overloading):
  2. It is also known as static polymorphism. This type of polymorphism is achieved by function overloading or operator overloading.
    • Method Overloading:
    • When there are multiple functions with same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments..
    • Operator Overloading:
    • Java also provide option to overload operators. For example, we can make the operator (+) for string class to concatenate two strings. We know that this is the addition operator whose task is to add two operands. So a single operator + when placed between integer operands, adds them and when placed between string operands, concatenates them. In java, Only + operator can be overloaded
  3. Run time Polymorphism (overriding):
  4. It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding.
    • Method Overriding:
    • on the other hand, occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.


Abstraction-
Hiding internal details and showing functionality is known as abstraction. For example phone call, we don't know the internal processing.
In Java, we use abstract class and interface to achieve abstraction

Encapsulation-
Binding (or wrapping) code and data together into a single unit are known as encapsulation. For example, a capsule, it is wrapped with different medicines.

A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here

Access Modifiers in Java

The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class. We can change the access level of fields, constructors, methods, and class by applying the access modifier on it
  1. Private
  2. Default
  3. Protected
  4. Public


Private:
The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
Default:
The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.
Protected:
The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.
Public:
The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.

Constructor:

In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory.
It is a special type of method which is used to initialize the object
Types of Constructor-:
  1. no-arguments constructor
  2. parameterized constructor.

There are three rules defined for the constructor.
  • Constructor name must be the same as its class name
  • A Constructor must have no explicit return type
  • A Java constructor cannot be abstract, static, final, and synchronized

Difference between Constructor and Method in Java
Constructor Method
A constructor is used to initialize the state of an object A method is used to expose the behavior of an object.
A constructor must not have a return type A method must have a return type.
The constructor is invoked implicitly. The method is invoked explicitly.
The constructor name must be same as the class name. The method name may or may not be same as the class name.


Abstract class

A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.
Rules
  • An abstract class must be declared with an abstract keyword.
  • It can have abstract and non-abstract methods.
  • It cannot be instantiated.
  • It can have constructors and static methods also.
  • It can have final methods which will force the subclass not to change the body of the method.
  • A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods.
  • It needs to be extended and its method implemented. It cannot be instantiated.


Interface

An interface in Java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.
In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body

Why use Java interface ?
There are mainly three reasons to use interface. They are given below.
  • It is used to achieve abstraction.
  • By interface, we can support the functionality of multiple inheritance.
  • It can be used to achieve loose coupling.


Static Keyword

The static keyword in Java is used for memory management mainly. We can apply static keyword with variables, methods, blocks and nested classes. The static keyword belongs to the class than an instance of the class.

Package in Java

Package in java can be categorized in two form, built-in package and user-defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc

Advantage of Java Package
  • Java package is used to categorize the classes and interfaces so that they can be easily maintained.
  • Java package provides access protection.
  • Java package removes naming collision.


JDBC

JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the query with the database. It is a part of JavaSE (Java Standard Edition). JDBC API uses JDBC drivers to connect with the database

The java.sql package contains classes and interfaces for JDBC API. A list of popular interfaces of JDBC API are given below:
  • Driver interface
  • Connection interface
  • Statement interface
  • ResultSet interface

API

API (Application programming interface) is a document that contains a description of all the features of a product or software. It represents classes and interfaces that software programs can follow to communicate with each other. An API can be created for applications, libraries, operating systems, etc.



Contact Fee Courses in Rishieksh