Java
What is Java?
Java is a high level, object oriented, compiler & interpreter based, platform independent programming language.
It is mainly used to develop standalone application softwares, enterprises softwares, web based applications.
History and Evolution of Java
Java developed by sun microsystem in year 1995 by the team of engineers lead by James Gosling. The project is called The green project. At that time it was developed for embedded software used in consumer electronics like TV set top box, Tosters etc.
1991-1992: Initially the project was started and its name was Oak.
1993: concept of Java applet is introduced.
1994: introduced the web browser called hot Java and became the global leader for internet programming language.
1995: name changed from oak to Java due to some legal issues. Big companies like Microsoft and Netscape shows their support for Java.
1996: recognised as general purpose programming language.
1997: JAVA SE 1.1
2006: JAVA SE 6, removed ".0" from version name.
2022: Latest version of Java is JAVA SE 18.
JDK, JVM AND JRE
"JDK, JVM & JRE platform dependent."
JDK stand for Java development kit. It is a generic bundle which contains set of API and software packages tools to develop a program in Java language.
JDK contains:
1. Javac
2. Java Interpreter/JVM
3. Java Loader
4. JDB
5. Java Applet Viewer
6. Javadoc
JVM stands for Java virtual Machine it is the logical unit which is responsible to provide a runtime environment for execution of Java byte code. It does not exist physically.
JVM architecture
Java program is compiled using Java compiler and converted into .class file which is nothing but the byte code of Java program.
Then then byte code (.class) file is passed to JVM in which following processes happens: first JVM contains a class loader which load all the classes used in the Java program, then after completion of this phase, the file is passed to byte code verifier which verify the byte code. After verification of byte code the next process is to implement the Java program which is done by Execution engine. And provide runtime environment.
JRE stands for Java runtime environment. It is the physical implementation of JVM. It contains set of library and JVM to execute and provide runtime to the Java program.
Difference between C++ and Java
Platform Dependency
C++ is a platform dependent programming language while Java is platform independent.
Use
C++ is used for making system softwares while Java is used for making application softwares or web softwares.
Designed for
C++was designed for making system software while java is developed for making softwares for embedded electronics like TV set-top box Tosters, washing machine, etc.
Compilation
In C++, program is compiled and converted into machine code. While, Java the program is compiled and converted into ".class" file and then interpreted with the help of JVM.
Operator overloading
You can use the operator overloading in C++ but Java doesn't support operator overloading
Structure and Union
C++ has the structure and union. while Java doesn't support structure and union.
Threading
C++ doesn't have threading it relies on third party software to implement threading concept while, Java provide built-in threading support.
Pointers
C++ supports the pointer while in Java the pointer is restricted due to security, it means you cannot write a pointers a program in Java but it supports internally.
Goto method
C + + there is go to method while, Java there is no goto method.
Multiple inheritance
In C++ you can use the concept of multiple inheritance, while, Java there is no multiple inheritance but you can implemented using interface.
Related to hardware
C++ is very closed to hardware while, Java is not that near to computer hardware.
Unicode system in Java
Before knowing, what is Unicode we have to understand what is encoding. Our computer only understand the binary language which is sequence of 0s and 1s. Every character is map the to the sequence of zeros and ones, this representation is called as encoding.
There are many encoding techniques:
ASCII, ISO, BIG-8 etc...
Problem with ASCII
In asking you can represent the values in 8 bits. Which is of range 0 to 127. And using this we cannot represent all the non modern languages so we need a system that can represent all known character, set of characters.
Unicode
It is an encoding scheme. We can represent the characters in 16 bits so it increases the range. Enhance we can store all known character of today's modern language.
Byte code and Execution of JAVA Program
Java byte code is a instruction set for JVM. It is similar to the machine code. When java program is compiled the byte code is generated. It is nothing but machine in the form of ".class" file.
Then it is interpreted. Due to this byte code plateform independence is achieved by JAVA.
Java Program ------- Compiled ------> Byte code ------- Class Loader -------- Byte code verifier --------- Interpreter -------- Runtime ------> Program Runner
Structure of Java Program
1. Documentation
2. Import Statement
3. Package Defination
4. Interface
5. Class Defination
6. Main Method class --> main method
Array in Java
Array is linear data structure. Arrays the collection of elements of same data type in a contiguous manner. You can access any element of Array directly using its index.
There are two type of arrays in Java:
1. Single dimensional array:
Syntax 1: int ar[] = {1,2,3,4,5};
Syntax 2: int ar1[] = new int[array_size];
2. Multi-dimensional array:
It is array of multiple arrays, when we are storing multiple arrays on every element of an array then this type of array called as multi-dimensional array.
Syntax: int ar[] = { {1,2,3} , {4,5,6} , {7,8}};
Program to read and write the elements of an array
Tokens in Java
The whole Java program is divided into small small parts and this small small parts are termed as tokens.
List of all the tokens in Java
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Separators
6. Comments
Operators in Java
Operators are the entities or say token in Java which enables to perform operation.
There are many types of operators in Java these are as follows:
1. Unary
2. Arithmetic
3. Shift
4. Bitwise
5. Logical
6. Ternary
7. Relational
8. Assignment
Garbage collector in Java
Java is the robust programming language. The meaning of robust is strong. It is called robust because of its ability to manage memory. There is a built-in garbage collector in Java. It's function is to collect the garbage from the program. The unreferenced objects, null values, and undefined method, or anonymous objects are considered as garbage in Java and the work of garbage collector destroyed them or reallocated there memory to needful objects in Program. It is done automatically.
There are two methods of Java garbage collection you can use in Java program:
1. finalize () method.
2. gc () method.
OOPS CONCEPTS IN JAVA
Class and objects in java
Class and object are considered as basic concept of object oriented programming.
Class is a user defined blueprint or prototype which contains data members and member functions. When the data members and member functions are binded together to form a single unit called class this method is known as encapsulation. It does not physically exist in program, there is no physical significance of a class without it's object.
Object is the physical implementation of a class. Object is an instance of a class. It is called as physical because it physically exists it means when the classes created the memory is not allocated to it, but when object is created the memory is allocated. It is considered as real world entity.
Method is a set of instruction which is used to perform certain task. It is of two types user defined and predefine. It is based on the concept of reuseability, means write once use many time.
We can also declare methods in class.
Constructor in Java
Constructor is a block of code which is similar to method in java. It is a part of our class. And its name is similar to that of the class. It is called automatically when the object of that classes created and called. It is the special type of method which is used to initialise the object.
There are two type of constructors in Java
1. Default constructor.
It is also called as non argument constructor when we don't declare any constructor for a class the default constructor is automatically declared and called upon, when the object of that classes created.
2. Parameterized constructor.
When we pass any argument to the constructor then which type of constructor is known as parameterized constructor.
Destructor in Java
It is opposite of the constructor in Java as constructor is called when the new object initialised while the destructor is called upon when we need to release the memory which is allocated by an object. Not that there is no concept of destructor in Java as it provide the garbage collector for powerful memory management. In Java, we can use finalize or system.gc method to forcefully delete or destroy any object.
Java static and final keyword
In Java, we can use static keyword along with the following:
static keyword with a variable: when we use the static keyword with a variable then it will share a common property or a value for a given class when it's objects is created.
Static keyboard with method: when you use a static keyboard with method then you should not have to declare its object it will called automatically when you create the object of its class.
Static block: To initialize the static variable we use Static block. It is called only once when the classes loaded.
In Java, we can use the final keyword along with the following