The History and Evolution of Java
What is Java ?
Java is a “Programming Language”.
What is a programming Language?
Spoken Programming
Languages Languages
A programming language is set of instructions , used
to establish communication between human and a
system
Java’s Lineage
Java is related to C++, which is a direct descendant of C.
Much of the character of Java is inherited from these
two languages.
From C, Java derives its syntax. Many of Java’s object
oriented features were influenced by C++.
Earlier languages (cobal, pascal,etc..)
C Language (Procedure oriented)
C++ Language (Object oriented, platform dependent)
Java Language (Object oriented, platform independent)
The Creation of java
Java was developed by
James Gosling and his
team at Sun
Microsystems, Inc. in
1991. James Gosling
This language was
initially called “Oak,”
but was renamed “Java”
in 1995.
But the history of Java
starts with the Green
project .
Java team members (also
known as Green Team) James Gosling and his team
Initiated this project to develop a software for digital
devices such as set-top boxes, televisions, etc.
However, it was suited for internet programming.
Green Team thought choose c and c++ languages to
develop project ,but they face problem with that
languages that is both languages are platform
dependent.
Green Team start a new project that develop a new
language that should platform independent.
Java’s Magic: The Bytecode
Java bytecode is the instruction set for the Java Virtual
Machine.
Java byte code is the combination of object code and
assembly code .
When we write a program in Java, firstly, the compiler
compiles that program and a bytecode (class file) is
generated for that piece of code.
When we wish to run this .class file is exicuted by JVM
and generate matching code.
JAVA BUZZWORDS
1.Simple
(easy to learn and write)
2.Secure
(provides data security through encapsulation )
3.Portable
(they can be executed on any kind OS)
4.Object-Oriented
(it is complete object oriented programming)
5.Robust
(it is very strong with type checking
6.Multithreaded
( java support Multithreading)
7.Architecture neutral
(java is platform independent)
8.Interpreted
(java maintain both compiler and interpreter)
9.High Performance
(java maintain JIT compiler)
10.Distributed
(Java supports distributed computation using Remote
Method Invocation (RMI) concept. )
11.Dynamic
(Libraries are dynamically linked during runtime
Java Versions
Version - Release date Version - Release date
JDK Beta-1995 Java SE 8 -March 2014
JDK 1.0-January 1996 Java SE 9-September 2017
JDK 1.1-February 1997 Java SE 10-March 2018
J2SE 1.2-December 1998 Java SE 11 -September 2018
J2SE 1.3-May 2000 Java SE 12-March 2019
J2SE 1.4-February 2002 Java SE 13-September 2019
J2SE 5.0-September 2004 Java SE 14-March 2020
Java SE 6-December 2006 Java SE 15-September 2020
Java SE 7-July 2011
Chapter-2
Object Oriented Programming and
Structure of Java
OBJECT ORIENTED PROGRAMMING CONCEPTS
Structure Of Java Programming
Package :
->A package in Java is a group of related classes.
Class :
-> A Class contain collection of member variables and
member methods.
-> We can create more than one object to a single class.
-> Through the object only we can access the class
members.
-> Class provide security to all its data members.
-> The java best feature encapsulation implemented by
classes .
public static void main(String args[]):
Public : it means that method can be used by code outside
of its class
static : it is used when we want to access a method without
creating its object, as we call the main method
before creating any class objects.
void : it indicates that a method does not return a value.
main() is declared as void because it does not return a
value.
main : it is a method; this is a starting point of a Java program.
String args[]: it is the input parameter for main method
System.out.println():
System is the predefine class ,out is the object and println is
the method.
Sample program:
C program to print “Hello Java program to print “Hello
World !”: World!”:
Protected
#include <stdio.h> import java.io.*; by class
class Simple
{
void main() public static void main( String
{ args[])
printf("Hello World!"); {
} System.out.println("Hello Java");
}
}
Output: Hello World! Output: Hello World!
How to run java program:
Step 1:
Write a program on the notepad and save it with .java (for example,
Simple.java) extension.
File name is same as class name
Step 2:
Open Command Prompt.
Step 3:
Set the directory in which the .java file is saved. In our case, the .java
(C:\\demo)
Step 4:
Use the following command to compile the Java program. It generates
a .class file in the same folder. It also shows an error if any.
javac (C:\\demo>javac Sample.java)
Step 5:
Use the following command to run the Java program:
Control Statements
BREAK: Continue
// break keyword in Java // continue keyword in Java
import java.io.*;
import java.io.*;
class GFG { class GFG {
public static void main(String[] args) public static void main(String[] args)
{
{
for (int i = 0; i < 10; i++) {
int n = 10; if (i == 6){
for (int i = 0; i < n; i++) { System.out.println();
// using continue keyword
if (i == 6)
// to skip the current iteration
break; continue;
System.out.print(i); }
System.out.print(i);
}
}
} }
} }
Out put: 0 1 2 3 4 5 Out put: 0 1 2 3 4 5
Return:
import java.io.*;
class GFG {
public static void main(String[] args)
{
int n = 10;
for (int i = 0; i < n; i++) {
if (i == 6)
return;
System.out.print(i);
}
for (int j = 0; j < n; j++) {
if (j == 2)
break;
System.out.print(i);
}
}
Data Types, Variables and Arrays
Data Types :
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to
127
short 2 bytes Stores whole numbers from -32,768
to 32,767
int 4 bytes Stores whole numbers from -
2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -
9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient
for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient
for storing 15 decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or
ASCII values
VARIABLES:
Variables are containers for storing data values
DECLARING (CREATING) VARIABLES:
To create a variable, you must specify the type and assign it a value:
Syntax: type variable = value;
Where type is one of Java's types (such as int or String), and variable is
the name of the variable (such as x or name). The equal sign is used to
assign values to the variable.
To create a variable that should store text, look at the following Example
Create a variable called name of type String and assign it the value
"John":
String name = "John";
System.out.println(name);
VARIABLE TYPES
A class can contain any of the following variable types.
Local variables − Variables defined inside methods,
constructors or blocks are called local variables. The variable
will be declared and initialized within the method and the
variable will be destroyed when the method has completed.
Instance variables − Instance variables are variables within a
class but outside any method. These variables are initialized
when the class is instantiated. Instance variables can be
accessed from inside any method, constructor or blocks of that
particular class.
Class variables − Class variables are variables declared within
a class, outside any method, with the static keyword(can be
used to refer the common property of all objects.)
Arrays
Declaring an Array Variable:
Do not have to create an array while declaring array
variable
<type> [] variable_name;
<type> variable_name[];
Ex:
int [] prime;
int prime[];
Both syntaxes are equivalent
No memory allocation at this point
Defining/Creation an Array
Define an array as follows:
variable_name=new <type>[N];
Ex: primes=new int[10];
Declaring and defining in the same statement:
int[] primes=new int[10];
In JAVA, int is of 4 bytes, total space 4*10=40 bytes
prime Index
0 1 2 3 4 5 6 7 8 9
2 1 11 -9 2 1 11 90 101 2
value
What happens if …
We define
int[] prime=new long[20];
MorePrimes.java:5: incompatible types
found: long[]
required: int[]
int[] primes = new long[20];
^
The right hand side defines an array, and thus the
array variable should refer to the same type of
array
Default Initialization
When array is created, array elements are
initialized default as
Numeric values (int, double, etc.) to 0
Boolean values to false
Char values to ‘\u0000’ (unicode for
blank character)
Class types to null
Initializing Arrays
Initialize and specify size of array while declaring
an array variable
int[] primes={2,3,5,7,11,13,17}; //7 elements
You can initialize array with an existing array
int[] even={2,4,6,8,10};
int[] value=even;
One array but two array variables!
Both array variables refer to the same array
Array can be accessed through either variable name
Sample Program
class MinAlgorithm
{
public static void main ( String[] args )
{
int[] array = { -20, 19, 1, 5, -1, 27, 19,
5 } ;
int min=array[0]; // initialize the current
minimum
for ( int i=0; i < array.length; i++ )
if ( array[ i ] < min )
min = array[ i] ;
System.out.println("The minimum of this array is: " +
min );
}
}
O/P: The minimum of this array is: -20
Arrays of Arrays
Two-Dimensional arrays
float[][] temperature=new float[10][365];
10 arrays each having 365 elements
First index: specifies array (row)
Second Index: specifies element in that array
(column)
In JAVA float is 4 bytes, total Size=4*10*365=14,600
bytes
EXP : int[][] array2D = { {99, 42, 74, 83,
100} , {90, 91, 72, 88, 95}, {88, 61, 74, 89,
96}, {61, 89, 82, 98, 93}, {93, 73, 75, 78, 99},
{50, 65, 92, 87, 94}, {43, 98, 78, 56, 99} };
//5 arrays with 5 elements each
Sample Program
class unevenExample3
{
public static void main( String[] arg )
{ // declare and construct a 2D array
int[][] uneven = { { 1, 9, 4 }, { 0, 2}, { 0,
1, 2, 3, 4 } };
// print out the array
for ( int row=0; row < uneven.length; row++ )
//changes row
{
System.out.print("Row " + row + ": ");
for ( int col=0; col <
uneven[row].length; col++ ) //changes column
Output :
System.out.print( uneven[row][col]
Row 0: 1 9 4
+ " "); System.out.println();
Row 1: 0
} 2
} Row 2: 0
TYPE CONVERSION AND CASTING
Java provides various datatypes to store various data values.
Converting one primitive datatype into another is known as type casting (type
conversion) in Java.
Widening − Converting a lower datatype to a higher datatype is known as widening. In
this case the casting/conversion is done automatically by the comiler therefore, it is
known as implicit type casting. In this case both datatypes should be compatible with
each other.
Fig . 1.7 : Winding type
Narrowing − Converting a higher datatype to a lower datatype is known as narrowing.
In this case the casting/conversion is not done automatically, you need to convert
explicitly by the programmer using the cast operator “( )” explicitly. Therefore, it is
known as explicit type casting. In this case both datatypes need not be compatible with
each other.
Fig . 1.8:Narrowing type
Exp:
int x=5,x1;
byte b=8,b1;
x1=b; //Winding
Strings:
Java does not support String type, it is not a simple
type .
String is an array of characters.It is under the
package java.lang.*;
In java String is an “Object”.
Strings in java are immutable
Once created they cannot be altered and hence any
alterations will lead to creation of new string object
Example
String s1 = “Example”
String s2 = new String(“Example”)
String s3 = “Example”
The difference between the three statements is that, s1
and s3 are pointing to the same memory location i.e. the
string pool . s2 is pointing to a memory location on the
heap.
Using a new operator creates a memory location on the
heap.
Concatinting s1 and s3 leads to creation of a new string in
the pool.
StringBuffer
StringBuffer is a synchronized and allows us to mutate
the string.
StringBuffer has many utility methods to manipulate
the string.
This is more useful when using in a multithreaded
environment.
Always has a locking overhead.
Example
public class mybuffers{
public static void main(String args[]){
StringBuffer buffer = new StringBuffer(“Hi”);
buffer.append(“Bye”);
System.out.println(buffer);
}
}
This program appends the string Bye to Hi and prints it
to the screen.
StringBuilder
StringBuilder is the same as the StringBuffer class
The StringBuilder class is not synchronized and hence
in a single threaded environment, the overhead is less
than using a StringBuffer.
Pointers
Java does not have pointers like C has,
but it does allow you to create new objects on the
heap which are “referenced” by variables.
Java heap space is used by java at runtime.
Java stack space is used by java static values(x=9);