Core Java – Introduction

What is Java ?

Java is broadly defined as a General purpose, object-oriented, platform independent programming language that runs very fast.

  • General purpose – the language is not constrained to one particular domain. Java can be used to develop a wide variety of applications.
  • Object-oriented – helps model real world scenarios in a more natural way.
  • Platform independent – write once, run anywhere; one of the key benefits of Java.

Java is currently one of the most widely used programming languages.


Java is simple & safe – 

  • No concept of pointers in Java
  • Java provides automatic memory management.

Java comes with a very rich library of functions (predefined functionality) – Java API.

Java is also free.

–> Sun Microsystems was acquired by Oracle in 2010.


What makes Java ? 

JVM : 


Java Variations

Java was initially developed to run inside embedded systems and later within the web browsers in the form of Java programs called ‘Applets’. Then, it evolved as a popular programming language for developing : 

  • Large scale web applications (Java EE)
  • Standard desktop applications (Java SE)
  • Mobile applications (Java ME)

Java provides dedicated platforms for just these different types of applications.

  • GMail is mostly written in Java.
  • Most open source libraries are implemented in Java like – Apache Solr – for search capability in the websites, Hadoop – for BigData, Mahout, etc.

J2SE

  1. J2SE / JAVA SE : Java 2 Standard Edition
  2. It will provide only Fundamentals of Java programming.
  3. It can be used to prepare Standalone Applications.
  4. If we design and execute any application without using Client-Server arch or without distributing application logic over multiple machines then that application is called Standalone Application.
  5. EX: Calculator, Calendar, Paint,…..
  6. Only 5% of the applications are Standalone Applications.
  7. Its direct utilization is 5% only, but its dependency level is 1000%.
  1. It is a base Language for J2EE.
  2. It is a base for the Salesforce, Selenium testing, Hadoop,……

J2EE

J2ME / JAVA ME

  • J2ME / JAVA ME: Java 2 Micro Edition. It will provide micro programming.
  • It can be used to prepare Mobile Based Applications.
  • If we design and execute any application on the basis of mobile H/W arch. then that application is called Mobile Based Applications.
  • EX:         My Airtel App, Facebook, YouTube, Whatsapp…

Mobile Application Development has very good demand , but J2ME is not having any demand.


1. Introduction to Java – Theory

1.1 Java History

In 1991, SUN Microsystems had a requirement to develop software that allows Network of “heterogeneous” consumer devices like Cable TV switch boxes, Remote controllers,…communicating with each other.

For the above requirement, SUN  Microsystems has gathered nearly 30 members as a team led by James Gosling and Patrick Naughten and started a project named “GREEN”.

Vision of the project is to build an interactive wireless handheld device which would communicate with home entertainment devices like TV, VC, etc.

The software that the Green team develops would be installed on all of these devices so that the devices can be communicated.

Goals of the project

  1. The software should consume less memory.
  2. The technology should allow these devices to communicate with each other and also pass around any software programs (delivery of software components)
  3. Platform independence – software programs delivered from one device to another should run on the target device without any issues.
  4. Security
  5. Multi threaded

SUN Microsystems wants to provide the following 3 features mainly in their new programming language.

  • Simple programming language
  • Tight Coded
  • Architectural Neutral

Simple Programming Language :

  • Less Execution Time: to improve application performance
  • Less Memory: Less Cost for the End Product.
  • Less Power : Less Maintenance cost for the End product.

Tight Coded:

If any programming Language takes less number of instructions to perform a particular task then that programming language is called Tight Coded programming Language.

IN C:

Stack:

PUSH : 20 loc
POP  : 20 loc
PEEK : 20 loc
——————
Total: 60 loc

In JAVA:

Stack s = new Stack();
s.push(“AAA”);
s.pop();
s.peek();
—-----------------
Total: 4 loc

Architectural Neutral:

If any programming language allows its applications to run under all the H/W systems then that programming language is called Architectural Neutral Programming language.


In 1991, SUN Microsystems started working on a new programming language and SUN Microsystems completed the new Programming language in the year 1992, Dec.

The Green team decided to create brand new technology. James Gosling created an entirely new language and named it Oak.

After preparing a new Programming Language , James Gosling has suggested a name “OAK” for the new Programming Language, but SUN Microsystems has rejected the name “OAK”, because SUN Microsystems found that the name “OAK” is an existing programming language name.

While preparing a new programming language, all GREEN project people are used to taking a brand labeled coffee that is “JAVA”, by thinking of JAVA coffee, all GREEN project engineers have suggested the name “JAVA” to the new Programming language. Here SUN Microsystems has accepted and SUN Microsystems has confirmed the name “JAVA” to the new Programming language. 

After preparing new programming Language, SUN Microsystems has prepared the first product on the basis of the Java that is ‘*7’ , it is a Remote Controller, it is a failure product, no Customer is identified for *7 product, SUN Microsystems waited up to 1 and half year of time to find customer for the *7.

In the mid of 1994, there was a demand for WWW as part of the internet, here the key component in WWW is “Browser”, so Browsers are getting very good demand.

In the Mid of 1994, there was a browser “Mosaic”, it was prepared by Mark Anderson, he is an UG student in Illinois University, who prepared the Mosaic browser as part of his internship at Netscape communications, he took nearly 6.25$/hr.

In the mid of 1994, SUN Microsystems understood the browsers demand and SUN Microsystems has decided to prepare a new browser , its responsibility was taken by Jonathan Paine and Patrick Naughten , SUN Microsystems has prepared a new browser on the basis of Java in the form of “HotJava” browser at the end of 1995. 

HotJava browser is a successful product , with this JAVA programming language getting very good demand.

  • In the year 1996, JAN 23rd, SUN Microsystems released the very first version of Java with the name JDK1.0 version.
  • In every 1 or 2 years of gap, SUN Microsystems has released a number of Java versions with very good enhancements.

Java History – Year wise

Java 2 (java 1.2) in 1998 – introduced collections.
Java 5 in 2004 is a major one – generics, annotations, varargs, enums, enhanced for loop.
Java 8 in 2014 is another major one – lambdas, streams, new date-time api, etc


Java 1.8

  • Lambda expressions
  • Improved Date and Time library API changes

Java 1.7

  • Strings in switch statements
  • Multiple exception handling
  • Binary literals, underscores in literals
  • Java NIO 2 package
  • Support for dynamic languages

Java 1.5

  • Generics
  • Annotations
  • Varargs
  • static import
  • Autoboxing/unboxing
  • Enhanced for loop

Java 1.4

  • regular expressions support
  • XML processing
  • NIO

Java 1.2

  • Collections framework

JDK1.0 ———–> Language itself introduced
        JDK1.1
JDK1.2 ———–> J2SE, J2EE, J2ME
        JDK1.3
        JDK1.4
JDK1.5  ———–> More Enhancements like Generic, Annotations, Varargs, enums, enhanced for, …
        JDK1.6 
        JDK1.7  ——-> Java was handed over to Oracle Corp.
JDK1.8 ————> Functional Programming Features
        JDK1.9
        JDK1.10 ——-> Java must be released every 6 months of time.
JDK1.11 ——–> Java is a Licensed version for the Corporate Users.
        JDK1.12
        JDK1.13
        JDK1.14
        JDK1.15
        JDK1.16
JDK1.17 ———> Long Term Support
        JDK1.18
        JDK1.19
        JDK1.20


JVM based languages


Java v/s Other languages

1.2 Differences between Java and Others[C and C++]

1. C and C++ are static programming Languages, but JAVA is a Dynamic Programming language:

If any Programming language follows Static memory allocation then that programming language is called a Static Programming Language. If the memory allocation is going on at compilation time then that memory allocation is called Static Memory allocation.

EX: C and C++ .

In C and C++ applications, memory will be allocated to the primitive data types at compilation time, not at runtime.

If any Programming language follows Dynamic memory allocation then that programming language is called Dynamic Programming Language. If the memory allocation is going on at runtime , not at compilation time then that memory allocation is called Dynamic Memory allocation.

EX: JAVA, Python,….

In Java applications, memory will be allocated for the primitive data types at runtime, not at compilation time.

2. Preprocessor required in C and C++, but Preprocessor is not required in Java.

In the compilation , the preprocessor is the first phase .

In C and C++ , the complete predefined library was provided in the form of header files.

EX:        
stdio.h
conio.h
math.h
—----

To get predefined library from header files to the C application we have to include header files, to include header file we have to use #include<> statement.

EX:        

#include<stdio.h>
#include<math.h>
#include<conio.h>

If we compile a C program then the preprocessor will perform the following actions.

  • Preprocessor will recognize all #include<> statements.
  • Preprocessor will take header files names from #include<> statements.
  • Preprocessor will check whether these header files exist or not in C software.
  • If the specified header files exist in C software then the preprocessor will load all the header files content to the memory.
  • If the specified header files do not exist in the C software then the Preprocessor will raise errors.

Note: Loading a predefined library at compilation time is called Static Loading.

In C and C++, Preprocessor is required to recognize #include<> statements in order to load header files content to the memory.

In Java, the complete predefined library was provided in the form of classes and interfaces in packages.

EX:        
java.lang
java.io
java.util
java.sql
—----

 If we want to use a predefined library in java files we have to import the packages, to import a package we have to use the “import” statement.

import java.io.*;
import java.util.*;
import java.sql.*;

If we compile a Java program then the compiler will perform the following actions.

  • Compiler will recognize all the import statements.
  • Compiler will take all the packages names and the compiler will search these packages in the Java software.
  • If the specified packages do not exist in the java software then the compiler will raise an error.
  • If the specified packages exist in the Java software then the compiler will not raise any error and at the same time the compiler will not load package content to the memory.

Note: In Java, the compiler is responsible for checking whether the libraries exist or not, where the compiler is not responsible for loading predefined libraries to the memory.

In Java, at runtime, JVM will load the required packages to the memory , here loading a predefined library at runtime is called “Dynamic Loading”.

In Java , preprocessor is not required , because in Java, headers files and #include<> statements are not available, alternatively in Java packages and import statements are available.

Q)What are the differences between #include<> statement and import statement?

  • #include<> statements exist in C and C++.

import statements exist in Java.

  • #include<> statements are able to include the predefined library which existed in the form of header files.

import statements are able to include the predefined libraries which exist in the form of classes and interfaces in packages.

  • #include<> statements are evaluated by the preprocessor.

import statements are evaluated by the compiler and JVM.

  • #include<> statements support static loading.

import statements support dynamic loading.

By Using single #include<> statement , we are able to include only one header file, if we want to include more than one header file we have to use more than one #include<> statement.

EX:       
#include<stdio.h>
#include<conio.h>

In Java, by using single import statement we are able to include more than one class or more than one interface of the same package by using single ‘*’ notation.

EX:        import java.io.*;

3. C and C++ are Platform Dependent Programming Languages, but JAVA is a Platform Independent Programming Language.

If any Programming language allows its applications to perform compilation and execution on the same Operating System then that programming Language is called a Platform Dependent Programming Language.

EX:         C, C++.

If we Compile C and C++ applications on Windows Operating System then the Compiler will generate .exe file contains directly executable code and it is following the same windows operating system representations, in this case to execute windows represented .exe file we must use the same windows operating system for execution, this dependency is called Platform Dependency, here the C and C++ programming languages are Platform Dependent Programming language.

If any Programming language allows its applications to perform compilation on one Operating System and execution is on another Operating system then that Programming Language is called a Platform Independent Programming Language. 

EX: Java, Python,….

If we compile a Java program in windows operating system then the compiler will generate .class files , where the generated .class files contains bytecode, where the bytecode is not the directly executable code, it is an intermediate code and it is not representing any of the Operating system representations including windows, in this context if we want to execute bytecode in any Operating system then that bytecode must have the local operating system representations.

In the above context, to execute java applications on multiple operating systems and to make Java as a Platform Independent Programming Language, SUN Microsystems has provided a set of converters to convert the bytecode from its neutral nature to the Local Operating System representation that is Native Code.

In the above context, SUN Microsystems has provided the Convertors and the Execution process in a single component called JVM[Java Virtual machine].

Java is a Platform Independent Programming Language because of JVMs only, but JVM is a platform dependent component.

Note: The complete Java software is  platform Dependent, but Java programming language is platform Independent.

Q)What are the differences between .exe file and .class file?

  1. .exe files exist in C and C++ .

.class files exist in JAVA.

  1. .exe files contain directly executable code.

.class files contain bytecode, it is not directly executable code, it is an intermediate code.

  1. .exe file is a platform dependent file.

.class file is a platform independent file.

  1. .exe file is providing less security.

.class file is providing more security.

5. Pointers are available in C/C++, but Pointers are not available in Java.

Pointer is a variable, it is able to store the address locations of the data structures , where the data structures may be a variable, an Array, a struct, another pointer variable.

Q) Why Pointer variables not suitable in Java?

  • Pointer variables are recognized and initialized by the compiler at the compilation time, so Pointer variables require static memory allocation, but Java is following Dynamic Allocation.
  • Pointer variables are supported by the Static Programming Languages, but JAVA is a dynamic programming language.
  • Pointer variables are very much suitable in Platform Dependent Programming Languages, but JAVA is a platform Independent Programming Language.
  • Pointers is a bit confusion oriented feature, it will increase confusion to the developers, but JAVA is a simple programming language, it must not allow the confusion oriented features like Pointers.

Q) What are the differences between Pointer Variables & Reference variables?

  • Pointers exist in C and C++.

Reference Variables exist in Java.

  • Pointer variables are able to refer to a block of memory by storing address location.

Reference Variables are able to refer to a block of memory that is an object through its reference value, where the reference value is the hexa-decimal form of Hashcode, where Hashcode is an unique identity provided by the Heap manager in the form of an integer value.

  • Pointer variables are recognized and initialized at compilation time.

Reference Variables are recognized and initialized at runtime.

  • Pointer variables are suitable in the static programming languages and Platform dependent programming languages.

Reference variables are very much suitable in Dynamic Programming languages and they are very much suitable in Platform Independent Programming languages.

5. Multiple Inheritance is not possible in JAVA.

If any Programming Language allows its applications to represent data in the form of Objects by following the Object Oriented features then that programming language is called an Object Oriented Programming Language.

There are 7 Object Oriented Features.

Object Oriented Features

  1. Class
  2. Object
  3. Encapsulation
  4. Abstraction
  5. Inheritance
  6. Polymorphism
  7. Message Passing

Note: In Object Oriented Programming Languages, we are able to write programs in the form of classes, where the classes are able to represent all real world entities in the form of coding.

EX: Student, Employee, Customer, Account, Product,…

class Student{
—---
}

class Employee{
—---
}

Inheritance:

It is a relation between classes, it will bring or provide variables and methods from one class to another class.

  • The class which is providing variables and methods to some other class is called a Parent class or a Base class or a superclass.
  • The class which is getting variables and methods from a superclass is called a Child class or a derived class or a subclass.

The main advantage of Inheritance is “Code Reusability”.

Initially, there are two types of inheritances.

  • Single Inheritance
  • Multiple Inheritance.

On the basis of the Single and Multiple Inheritances , there are three more inheritances.

  • Multi-Level Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

Single Inheritance:

It is a relation between classes, it will provide variables and methods from only one superclass to one or more subclasses.

Java does support Single Inheritance.

Multiple Inheritance:

It is a relation between classes, it will provide variables and methods from more than one superclass to one or more subclasses.

→ Java does not support Multiple Inheritance.

If we declare the same variables and methods with different values and different implementation in both the superclasses and if we access that variables and methods in a subclass then which superclass provided variables and methods are accessed is a confusion state, it will increase confusion to the compilers, JVMs and Developers, but JAVA is a simple programming language, it does not allow the confusion oriented features like Multiple Inheritance.

Note: Java has restricted multiple inheritance by defining “extends” keyword in such a way that to allow only one superclass name in subclass declaration, not to allow more than one superclass name.

class A extends B {
}

Status: Valid

class A extends B,C {
}

Status: Invalid

In Python:

class A:
i = 10

class B:
i = 20

class C(A,B):
i = i + 10 —-> 20

class D(B,A):
i = i + 10 —> 30

6. Destructors are required in C++, but Destructors are not required in JAVA

If any programming language allows to represent data in the form of Objects as per the Object Oriented features then that programming language is called an Object Oriented programming language.

In Object Oriented programming languages , it is convention to represent data in the form of objects.

In Object Oriented programming languages, to represent data in the form of objects, first we have to create objects , and at the end of the application we have to destroy objects.

In Object oriented programming languages, to create objects we will use a separate feature called “Constructor” and to destroy objects we will use another separate feature called “Destructor”.

In C++, Developers are responsible for creating objects and destroying objects , so in C++ applications developers must use destructors to destroy objects, hence in C++ Destructors are required. 

In Java, developers are responsible for creating objects only, developers are not responsible for destroying objects, because in java to destroy objects there is an in-built component  in the form of “Garbage Collector”, so in Java developers are not required to use destructors, hence destructors are not existed in Java.

7. C and C++ are following call by value and call by reference parameter passing mechanisms, but Java is following only call by value parameter passing mechanism.

In all programming languages, if we pass primitive data[byte, short, int, long, float, double, boolean, char] as parameters to the methods then the parameter passing mechanism is “Call By Value” parameter passing mechanism.

In all the programming languages, if we pass address locations as parameters to the methods then the parameter passing mechanism is Call by reference.

In C and C++ applications, if we pass pointer variables as parameters to the methods then the parameter passing mechanism is Call by reference only, it is not Call by value, because in C and C++ applications pointer variables are able to represent address locations directly.

In Java, if we pass object reference variables as parameter to the methods then the parameter passing mechanism is call by value only, not call by reference, because in java object reference variable is a variable , it is able to store object reference value, where the object reference value is hexa-decimal form of hashcode, where Hashcode an unique identity provided by the Heap Manager in the form of an integer value.

8. Operator overloading is not possible in java.

If any Programming Language allows its applications to represent data in the form of Objects by following the Object Oriented features then that programming language is called an Object Oriented Programming Language.

There are 7 Object Oriented Features.

  1. Class
  2. Object
  3. Encapsulation
  4. Abstraction
  5. Inheritance
  6. Polymorphism
  7. Message Passing

Polymorphism:

Polymorphism is a Greek word, where Poly means many and morphism means forms or Structures.

If one thing exists in more than one form then it is called Polymorphism.

The main advantage of Polymorphism is “Flexibility”.

There are two types of Polymorphisms.

  1. Static Polymorphism

If the polymorphism is identified at compilation time then that polymorphism is called Static Polymorphism.

EX: Overloading

  1. Dynamic Polymorphism

If the polymorphism is identified at runtime or at execution time then that polymorphism is called Dynamic Polymorphism.

EX: Overriding

There are two types of Overloadings:

  1. Method Overloading

If we declare more than one method with the same name and with a different parameter list then it is called Method Overloading.

class Calculator {
void add(int i, int j) {
}
void add(float f1, float f2) {
}
void add(String str1, String str2) {
}

  1. Operator Overloading

If we declare an operator with more than one operation then that operator is called Overloaded operator and that process is called Operator Overloading.

EX:
int a = 10;
int b = 20;
int c = a + b; + is for Arithmetic Addition
System.out.println(c);   30
String str1 = “abc”;
String str2 = “def”;
String str3 = str1 + str2;  + for String concatenation 
System.out.println(str3); abcdef

In Java, operator overloading is not possible, because

  1. It is rarely used in application development.
  2. It is a bit of a confusion oriented feature when we declare more operators with more functions.

Note: As per the Java internal requirement, JAVA made some of the predefined operators as overloaded operators with fixed functionalities, but java has not provided any option for the developers to perform operator overloading explicitly.

EX: +, *, /,…..

9. In C and C++, Characters will take 1 byte of memory and integers will take 2 bytes of memory, but in Java characters will take 2 bytes of memory and integers will take 4 bytes of memory.

In C and C++, memory allocation for the primitive data types is not fixed, it is variable and it is dependent on the Operating System which we used, but in the case of Java memory allocation for the primitive data types is fixed irrespective of the Operating system which we used.

byte—-------> 1 byte
short—------> 2 bytes
int—--------> 4 bytes
long—-------> 8 bytes
float—------> 4 bytes
double—-----> 8 bytes
char—-------> 2 bytes
boolean—----> 1 bit

Q) In C and C++, one byte of memory is sufficient to represent characters data then what is the requirement for Java to assign two bytes of memory for the characters?

In C and C++ all characters are represented in the form of “ASCII” values, to represent any ASCII value one byte of memory is sufficient.

In Java, all the characters are represented in the form of UNICODE values, where to represent UNICODE values 2 bytes of memory is required.

Q) What is UNICODE and why is it required in JAVA?

UNICODE is one of the character representations, it is able to represent all the alphabet from all the natural languages like English, Hindi, Japanese, Chinese, ….. in java applications, with this we are able to provide a very good Internationalization Support.

Q) What is Internationalization[I18N]?

Designing Java applications w.r.t the local users conventions is called Internationalization.

Java is able to provide very good Internationalization support due to its UNICODE representation only.


Java Features

To describe Java programming Language, JAVA has provided the following features.

  • Simple
  • Object Oriented
  • Platform Independent
  • Architectural Neutral
  • Portable
  • Robust
  • Dynamic
  • Secure
  • Multi-Threaded
  • Distributed
  • Interpreted
  • High Performance

1. Simple

Java is a simple programming language, because :

  1. Java applications will take less memory and less execution time.
  2. Java has removed all the confusion oriented features like Pointers, Multiple Inheritance,….
  3. Java is using all the simplified syntaxes when compared with other programming languages.

2. Object Oriented

Java is an object oriented programming language, because java is able to represent the data in the form of Objects by following object oriented features like class, object, encapsulation,abstraction,…..

3. Platform Independent

Java is a platform independent programming language, because Java allows its applications to perform compilation on one Operating system and execution is on another Operating system.

4. Architectural Neutral

Java is an Arch neutral programming language, because Java allows its applications to perform compilation on one H/W Architecture and execution is on another Architecture.

5. Portable

Java is a portable programming language, because Java allows its applications to execute under all the Operating systems and under all the H/W systems without having changes in Java applications.

6. Robust

Java is a Robust Programming Language, because :

  1. Java has a very good memory management system in the form of Heap memory management system, it is a dynamic memory management system, it allocates and deallocates memory for the objects at runtime as per the application requirement.
  2. Java has very good exception handling mechanisms to handle the exceptions , because JAVA has provided a very good predefined library to represent and handle almost all the exceptions which are coming frequently in Java applications.

Note: Java has provided a way to define and handle our own exceptions Also along with predefined exceptions.

7. Dynamic

Java is a dynamic programming language, because Java allows dynamic memory allocation, in Java applications memory will be allocated for the data at runtime.

8. Secure

Java is a Secure Programming language, because :

  1. Java is providing a very good implicit security by having the component like “Security Manager” inside the JVM.
  2. Java is providing a very good Web Security by having a middleware service JAAS[Java Authentication And Authorization Service].
  3. Java is providing a very good Network security by having predefined implementations to the Network Security algorithms. 

9. Multi Threaded

Java is following the Multi Thread model, because Java allows the creation and execution of more than one thread at a time.

10. Distributed

Java is a distributed programming language, because Java has provided a separate module in the form of J2EE to prepare Distributed applications. 

11. Interpreted

Java is a both compiled and interpreted, because :

  1. Java applications required compilation to convert source code to the byte code and to check the developers mistakes in the program.
  2. Java applications required an interpreter inside the JVM to execute the java applications.

12. High Performance

Java is a high performance programming language due to its rich set of features like Robust, Multi Threaded, Dynamic, Portable,….