Unit-1 Oopj (A) PDF
Unit-1 Oopj (A) PDF
Introduction
Java was originally designed for interactive television, but it was too advanced
technology for the digital cable television industry at the time. The history of Java
starts with the Green Team. Java team members (also known as Green Team),
initiated this project to develop a language for digital devices such as set-top
boxes, televisions, etc. However, it was best suited for internet programming.
Later, Java technology was incorporated by Netscap.
James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java
language project in June 1991. The small team of sun engineers called Green
Team.
History of Java
Over the period of nearly 30 years, Java has seen many minor and major versions.
Following is a brief explaination of versions of java till date.
JDK
1 1995 Initial Draft version
Beta
23
2 JDK 1.0 A stable variant JDK 1.0.2 was termed as JDK 1
Jan
1996
19
Major features like JavaBeans, RMI, JDBC, inner
3 JDK 1.1 Feb
classes were added in this release.
1997
8
HotSpot JVM, JNDI, JPDA, JavaSound and support
5 JDK 1.3 May
for Synthetic proxy classes were added.
2000
JDK 1.5 30
Various new features were added to the language
7 or J2SE Sep
like foreach, var-args, generics etc.
5 2004
21
JAVA Module system introduced which can be applied to
11 Sep
SE 9 JVM platform.
2017
17
JAVA Feature added - Text Blocks (Multiline strings),
15 Sep
SE 13 Enhanced Thread-local handshakes.
2019
15
JAVA Feature added - Sealed Classes, Hidden Classes,
17 Sep
SE 15 Foreign Function and Memory API (Incubator).
2020
Java Applications
Java has become the most robust programming language because of its
amazing features. Some of its features are platform independence, high
performance, Object orientation, support for automatic garbage
management, and many more.
2. Mobile Applications
3. Artificial Intelligence
4. Web Applications
6. Gaming Applications
Java has proved to be the most desirable option for game development
because of the presence of a wide variety of open-source frameworks.
Popular games like Mission Impossible III, Minecraft, and Asphalt 6
are developed in Java. It supports Dalvik Virtual Machine and
jMonkeyEngine which provides an ace in building 2 Dimensional and
3 Dimensional android games.
7. Business Applications
8. Embedded Systems
9. Cloud Applications
Java has enhanced security features which makes it the best option for
the development of scientific applications. It has served as a powerful
tool in coding complex mathematical operations. The programs are
designed in a highly secure and efficient manner. Some of the most
widely used applications like MATLAB use Java as a component of
the core system.
What is JVM
It is:
1) Classloader
2) Class(Method) Area
3) Stack
Java Stack stores frames. It holds local variables and partial results, and
plays a part in method invocation and return.
Each thread has a private JVM stack, created at the same time as thread.
Working of JRE
JDK: JDK stands for Java Development Kit. JDK contains the JRE
with compiler, interpreter, debugger, and other tools. It provides
features to run as well as develop Java Programs.
Documentation Section
Package Declaration
Import Statements
Interface Section
In this section, we define the main() method. It is essential for all Java
programs. Because the execution of all Java programs starts from the
main() method. In other words, it is an entry point of the class. It must
be inside the class. Inside the main method,
First, the source ‘.java’ file is passed through the compiler, which then
encodes the source code into a machine-independent encoding, known
as Bytecode. The content of each class contained in the source file is
stored in a separate ‘.class’ file. While converting the source code into
the bytecode.
The main class is loaded into the memory bypassing its ‘.class’ file to
the JVM, through invoking the latter. All the other classes referenced
in the program are loaded through the class loader.
Bytecode Verifier
Just-In-Time Compiler
This is the final stage encountered by the java program, and its job is
to convert the loaded bytecode into machine code. When using a JIT
compiler, the hardware can execute the native code, as opposed to
having the JVM interpret the same sequence of bytecode repeatedly
and incurring the penalty of a relatively lengthy translation process.
Features of Java
The primary objective of Java programming language creation was to make it
portable, simple and secure programming language. Apart from this, there are
also some excellent features which play an important role in the popularity of
this language. The features of Java are also known as Java buzzwords.
A list of the most important features of the Java language is given below.
1. Simple
2. Object-Oriented
3. Portable
4. Platform independent
5. Secured
6. Robust
7. Architecture neutral
8. Interpreted
9. High Performance
10.Multithreaded
11.Distributed
12.Dynamic
Simple
Java is very easy to learn, and its syntax is simple, clean and easy to
understand. According to Sun Microsystem, Java language is a simple
programming language
Object-oriented
Platform Independent
Java is best known for its security. With Java, we can develop virus-free
systems. Java is secured because:
o No explicit pointer
o Java Programs run inside a virtual machine sandbox
Portable
High-performance
Distributed
There are four types of Java applications that can be created using Java
programming:
Keywords
Data Types
Data types represent the type of data you want to use and memory
required for that data.
There are two types of data:
Variables
Variable holds the user data. The memory will be allocated for the
variables according to the data type. Value of the variable can be
changed any number of times during the program execution.
Syntax: <data type><var name>; or <data type><var
name>=<value>;
Example: int a; int b=10; String s=”name”; String st; There are two
types of variables based on the data types used to declare the variable:
Primitive variables
Reference Variable
OPERATORS
Operators are the symbols that perform the operation on values. These
values are known as operands. There are three types of operators
depending on the number of operands required:
Java Objects
Every time an object is created using the new() keyword, at least one
constructor is called.
1. Default constructor
2. Parameterized constructor
1. Default Constructor
A constructor that has no parameters is known as default the
constructor. A default constructor is invisible. And if we write a
constructor with no arguments, the compiler does not create a default
constructor. It is taken out. It is being overloaded and called a
parameterized constructor. The default constructor changed into the
parameterized constructor. But Parameterized constructor can’t change
the default constructor.
Method in Java
A method is a block of code or collection of statements or a set of code
grouped together to perform a certain task or operation. It is used to
achieve the reusability of code. We write a method once and use it
many times. We do not require to write code again and again. It also
provides the easy modification and readability of code, just by adding
or removing a chunk of code. The method is executed only when we call
or invoke it.
Types of Method
There are two types of methods in Java:
o Predefined Method
o User-defined Method
Predefined Method
In Java, predefined methods are the method that is already defined in the
Java class libraries is known as predefined methods. It is also known as
the standard library method or built-in method. We can directly use
these methods just by calling them in the program at any point. Some
pre-defined methods are length(), equals(), compareTo(), sqrt(), etc.
When we call any of the predefined methods in our program, a series of
codes related to the corresponding method runs in the background that is
already stored in the library.
User-defined Method
Constructors Methods
1. class A{
2. private int data=40;
3. private void msg(){System.out.println("Hello java");}
4. }
5.
6. public class Simple{
7. public static void main(String args[]){
8. A obj=new A();
9. System.out.println(obj.data);//Compile Time Error
10. obj.msg();//Compile Time Error
11. }
12. }
2) Default
1. //save by B.java
2. package mypack;
3. import pack.*;
4. class B{
5. public static void main(String args[]){
6. A obj = new A();//Compile Time Error
7. obj.msg();//Compile Time Error
8. }
9. }
3) Protected
1. //save by B.java
2. package mypack;
3. import pack.*;
4.
5. class B extends A{
6. public static void main(String args[]){
7. B obj = new B();
8. obj.msg();
9. }
10. }
4) Public
1. //save by B.java
2.
3. package mypack;
4. import pack.*;
5.
6. class B{
7. public static void main(String args[]){
8. A obj = new A();
9. obj.msg();
10. }
11. }
Static Keyword in Java
1. Blocks
2. Variables
3. Methods
4. Classes
Static blocks
If you need to do the computation in order to initialize your static
variables, you can declare a static block that gets executed exactly
once, when the class is first loaded.
Consider the following java program demonstrating the use of static
blocks.
Static variables
Static methods
Static Classes
A class can be made static only if it is a nested class. We cannot
declare a top-level class with a static modifier but can declare nested
classes as static. Such types of classes are called Nested static classes.
Nested static class doesn’t need a reference of Outer class. In this case,
a static class cannot access non-static members of the Outer class.
Final Members/Keyword In Java
The final keyword in java is used to restrict the user. The java final
keyword can be used in many context. Final can be:
1. variable
2. method
3. class
The final keyword can be applied with the variables, a final variable that
have no value it is called blank final variable or uninitialized final
variable. It can be initialized in the constructor only. The blank final
variable can be static also which will be initialized in the static block
only. We will have detailed learning of these. Let's first learn the basics
of final keyword.
Comments in Java
In a program, comments are like indents one makes, they are used so
that it is easier for someone who isn’t familiar with the language to be
able to understand the code. It will also make the job easier for you, as
a coder, to find errors in the code since you will be easily able to find
the location of the bug. Comments are ignored by the compiler while
compiling a code, which makes the job more complex in the long run
when they have to go through so much code to find one line .
1. Single-line comments.
2. Multi-line comments.
3. Documentation comments.
1) Java Single Line Comment
The single-line comment is used to comment only one line of the code.
It is the widely used and easiest way of commenting the statements.
Single line comments starts with two forward slashes (//). Any text in
front of // is not executed by Java.
Multi-line comments are placed between /* and */. Any text between /*
and */ is not executed by Java.
Types of Variables
o local variable
o instance variable
o static variable
1) Local Variable
A variable declared inside the class but outside the body of the method,
is called an instance variable. It is not declared as static.
3) Static variable
There are many types of operators in Java which are given below:
o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator.
Arithmetic multiplicative * / %
additive + -
bitwise exclusive OR ^
bitwise inclusive OR |
logical OR ||
Ternary ternary ? :
Java compiler executes the code from top to bottom. The statements in
the code are executed according to the order in which they appear.
However, Java provides statements that can be used to control the flow
of Java code. Such statements are called control flow statements. It is
one of the fundamental features of Java, which provides a smooth flow
of program.
3. Jump statements
o break statement
o continue statement
Decision-Making statements
As the name suggests, decision-making statements decide which
statement to execute and when. Decision-making statements evaluate the
Boolean expression and control the program flow depending upon the
result of the condition provided. There are two types of decision-making
statements in Java, i.e., If statement and switch statement.
1) If Statement:
1) Simple if statement:
It is the most basic statement among all control flow statements in Java.
It evaluates a Boolean expression and enables the program to enter a
block of code if the expression evaluates to true.
2) if-else statement
3) if-else-if ladder:
4. Nested if-statement
Loop Statements
In programming, sometimes we need to execute the block of code
repeatedly while some condition evaluates to true. However, loop
statements are used to execute the set of instructions in a repeated order.
The execution of the set of instructions depends upon a particular
condition.
1. for loop
2. while loop
3. do-while loop
for loop
In Java, for loop is similar to C and C++. It enables us to initialize the loop
variable, check the condition, and increment/decrement in a single line of
code. We use the for loop only when we exactly know the number of times, we
want to execute the block of code.
1. while(condition){
2. //looping statements
3. }
do-while loop
The do-while loop checks the condition at the end of the loop after executing
the loop statements. When the number of iteration is not known and we have
to execute the loop at least once, we can use do-while loop.
1. do
2. {
3. //statements
4. } while (condition);
Jump Statements
Jump statements are used to transfer the control of the program to the
specific statements. In other words, jump statements transfer the
execution control to the other part of the program. There are two types
of jump statements in Java, i.e., break and continue .
break statement
As the name suggests, the break statement is used to break the current flow
of the program and transfer the control to the next statement outside a loop
or switch statement. However, it breaks only the inner loop in the case of the
nested loop.
continue statement
Unlike break statement, the continue statement doesn't break the loop,
whereas, it skips the specific part of the loop and jumps to the next iteration
of the loop immediately.
We can easily add the elements to the String Array just like other data
types. It can be done using the following three methods:
Using ArrayList:
In this method, we will create a new Array with a larger size than the
initial Array and accommodate the elements in it. We will copy all the
elements to the newly added Array.