Java I
Java I
Java I
DEPARTMENT OF COMPUTATIONAL
STUDIES
UNIT-I
PROBLEM SOLVING USING JAVA
2) Initially it was designed for small, embedded systems in electronic appliances like set-
top boxes.
3) Firstly, it was called "Greentalk" by James Gosling, and the file extension was .gt.
4) After that, it was called Oak and was developed as a part of the Green project. Why
Java was named as "Oak"? Java History from Oak to Java
5) Why Oak? Oak is a symbol of strength and chosen as a national tree of many
countries like the U.S.A., France, Germany, Romania, etc.
6) In 1995, Oak was renamed as "Java" because it was already a trademark by Oak
Technologies.
Evolution of java:
1. JDK Alpha and Beta (1995)
2. JDK 1.0 (23rd Jan 1996)
3. JDK 1.1 (19th Feb 1997)
4. J2SE 1.2 (8th Dec 1998)
5. J2SE 1.3 (8th May 2000)
6. J2SE 1.4 (6th Feb 2002)
7. J2SE 5.0 (30th Sep 2004)
8. Java SE 6 (11th Dec 2006)
9. Java SE 7 (28th July 2011)
10. Java SE 8 (18th Mar 2014)
11. Java SE 9 (21st Sep 2017)
12. Java SE 10 (20th Mar 2018)
13. Java SE 11 (September 2018)
14. Java SE 12 (March 2019)
15. Java SE 13 (September 2019)
16. Java SE 14 (Mar 2020)
17. Java SE 15 (September 2020)
18. Java SE 16 (Mar 2021)
19. Java SE 18 (to be released by March 2022)
BYTE CODE:
Java bytecode is the instruction set for the Java Virtual Machine.
Simple
Object-oriented
Distributed
Interpreted
Robust
Secure
Architecture neutral
Portable
High performance
Multithreaded
Dynamic
1. Simple:
• Java was designed to be easy for a professional programmer to learn and
use
• effectively.
• It’s simple and easy to learn if you already know the basic concepts of Object
• Oriented Programming.
2. Object Oriented
• Java is designed fa or distributed environment of the Internet. Its used for creating
applications on networks.
• Java applications can access remote objects on the Internet as easily as they can do in
the local system.
• Java enables multiple programmers at multiple remote locations to collaborate and
• work together on a single project.
4. Compiled and Interpreted
• Usually, a computer language is either compiled or Interpreted. Java combines both this
approach and makes it a two-stage system.
• Compiled: Java enables the creation of cross-platform programs by compiling into an
• intermediate representation called Java Bytecode.
5. Robust
• It provides many features that make the program execute reliably in a variety of environments.
• Java is a strictly typed language. It checks code both at compile time and runtime.
• Java takes care of all memory management problems with garbage collection.
6. Secure
• Java language and Java Virtual Machine helped in achieving the goal of “write once;
• run anywhere, any time, forever.”
• Changes and upgrades in operating systems, processors and system resources will not
• force any changes in Java Programs.
8. Portable
• Java Provides a way to download programs dynamically to all the various types of platforms
connected to the Internet.
• It helps in generating Portable executable code.
9. High Performance
• Java performance is high because of the use of bytecode.
• The bytecode was used so that it was easily translated into native machine code.
10. Multithreaded
• Multithreaded Programs handled multiple tasks simultaneously, which was helpful in creating
interactive, networked programs.
11. Dynamic
• Java is capable of linking in new class libraries, methods, and objects.
• Java programs carry with them substantial amounts of run-time type information that is used
to verify and resolve accesses to objects at runtime.
DATA TYPES
Data types specify the different sizes and values that can be stored
in the variable. There are two types of data types in Java:
o local variable
o instance variable
o static variables
1) Local Variable:
A variable declared inside the body of the method is called local variable. You can use this
variable only within that method and the other methods in the class aren't even aware
that the variable exists.
A local variable cannot be defined with "static" keyword.
2) Instance 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.
It is called an instance variable because its value is instance-specific and is not shared
among instances.
3) Static variable:
A variable that is declared as static is called a static variable. It cannot be local. You can
create a single copy of the static variable and share it among all the instances of the class.
Memory allocation for static variables happens only once when the class is loaded in the
memory.
JAVA ARRAYS:
• Normally, an array is a collection of similar type of elements which has contiguous memory
location.
• Java array is an object which contains elements of a similar data type. Additionally, The
elements of an array are stored in a contiguous memory location. It is a data structure where
we store similar elements. We can store only a fixed set of elements
• in a Java array.
• Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd
element is stored on 1st index and so on.Unlike C/C++, we can get the length of the array using
the length member. In C/C++,
• we need to use the sizeof() operator.
OPERATORS IN JAVA
Operator in Java is a symbol that is used to perform operations. For example: +, -, *, / etc.
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.
JAVA CONTROL STATEMENTS | CONTROL FLOW IN
JAVA
Decision-Making statements:
1) If Statement:
The Java if-else statement also tests the condition. It executes the if
block if condition is true
otherwise else block is executed.
Syntax:
if(condition){
//code if condition is true
}else{
//code if condition is false
}
if-else-if ladder Statement:
The if-else-if ladder statement executes one condition from multiple statements.
Syntax:
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
Nested if statement:
The nested if statement represents the if block within another if block. Here, the
inner if block
condition executes only when outer if block condition is true.
Syntax:
if(condition){
//code to be executed
if(condition){
//code to be executed
}
}
LOOPING:
for Loop:
A simple for loop is the same as C/C++. We can initialize the variable, check condition and
increment/decrement value. It consists of four parts:
Initialization: It is the initial condition which is executed once when the loop starts.
Here, we can initialize the variable, or we can use an already initialized variable. It is
an optional condition.
Condition: It is the second condition which is executed each time to test the condition
of the loop. It continues execution until the condition is false. It must return boolean
value either true or false. It is an optional condition.
Increment/Decrement: It increments or decrements the variable value. It is an optional
condition.
Statement: The statement of the loop is executed each time until the second condition
is false.
Converting a lower data type into a higher one is called widening type casting. It is also known as implicit
conversion or casting down. It is done automatically. It is safe because there is no chance to lose data. It
takes place when:
Both data types must be compatible with each other.
The target type must be larger than the source type.
byte -> short -> char -> int -> long -> float -> double
Narrowing Type Casting
Converting a higher data type into a lower one is called narrowing type casting. It is also
known as explicit conversion or casting up. It is done manually by the programmer. If we do
not perform casting then the compiler reports a compile-time error.
double -> float -> long -> int -> char -> short -> byte
BASIC CONCEPTS OF OOPS:
OOPs (Object-Oriented Programming System):
Object means a real-world entity such as a pen, chair, table, computer, watch, etc. Object
Oriented Programming is a methodology or paradigm to design a program using classes
and objects. It simplifies software development and maintenance by providing some
concepts:
o Object
o Class
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation
METHODS DECLARATION
A class with only data fields has no life.
The objects created by such a class cannot respond to any messages.
We must therefore and methods that are necessary for manipulating
The data contained in the class.
Methods are declared inside the body of the class but immediately after the declaration
of instance variables.
The general form of a methods declaration is
Type method_name ( parameter-list )
{
Method-body;
}
Method declarations have four basic parts:
The name of the method(method name)
The type of the value the method returns(type)
A list of parameters(parameter-list)
The body of the method
Constructors in Java:
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.
Every time an object is created using the new() keyword, at least one constructor is called.
It calls a default constructor if there is no constructor available in the class. In such case, Java compiler provides
a default constructor by default.
There are two types of constructors in Java: no-arg constructor, and parameterized constructor.
Rules for creating Java constructor:
The final keyword in java is used to restrict the user. The java final keyword can be used in
many context. Final can be:
• variable
• method
• 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.
Access Modifiers (access control):
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.
There are four types of Java access modifiers:
1. Private: The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.
2. 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.
3. 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.
4. 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.
this keyword :
The finalize() method is invoked each time before the object is garbage collected. This
method can be used to perform cleanup processing. This method is defined in Object class
as:
gc() method :
The gc() method is used to invoke the garbage collector to perform cleanup processing. The
gc() is found in System and Runtime classes.
Syntax:
class Outer{
//code
class Inner{
//code
}
}
Java Anonymous inner class
Java anonymous inner class is an inner class without a name and for which only a single object
is created. An anonymous inner class can be useful when making an instance of an object with
certain "extras" such as overloading methods of a class or interface, without having to actually
subclass a class.
In simple words, a class that has no name is known as an anonymous inner class in Java. It
should be used if you have to override a method of class or interface.
1) String Literal
2) By new keyword