Unit-wisNotes_java1
Unit-wisNotes_java1
UNIT-1 NOTES
< Object Oriented Programming with Java - 23BDA2C20>
OOPS: Fundamentals of Object Oriented Programming – Introduction- Object Oriented Paradigm Basic
Concepts of Object Oriented Programming– Benefits of OOP-Applications of OOP. Java Evolution: Java
History – Java Features - How java differs from C and C++. Overview of Java Language - Constants, Variables
and Data types.
LEARNING OUTCOMES
NO. LEARNING OUTCOMES
1 Understanding of Fundamentals of Object Oriented Programming
2 Understanding of Basic Concepts of Object Oriented Programming
3 Understanding of Benefits of OOP, Applications OOPS
4 Knowing How java differs from C and C++.
5 Learning Constants, Variables
6 Understanding of Data types
INTRODUCTION
OOPS in java is to improve code readability and reusability by defining a Java program efficiently. The main principles
of object-oriented programming are abstraction, encapsulation, inheritance, and polymorphism. These concepts aim to
implement real-world entities in programs.
DEFINITIONS
Object-oriented programming is a core of Java Programming, which is used for designing a program using classes and
objects
[Type here]
[Type here]
DETAILED NOTES
UNIT –I
I. INTRODUCTION TO JAVA
What is Java
Java is a high Level programming language and it is also called as a platform. Java
is a secured and robust high level object-oriented programming language.
History of Java
James Gosling, Patrick Naughton and Mike Sheridan initiated the Java language
project in 1991. Team of sun engineers designed for small, embedded systems in
electronic appliances like set-top boxes. Initially it was called "Greentalk" later it
was called Oak .
Java is an open source software produced by Sunmicro system under the terms of
the GNU General Public License (GPL) Features of Java:
OOP Concepts
Simula is considered as the first object-oriented programming language. The programming paradigm
where everything is represented as an object is known as truly object-oriented programming language.
Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming isa
methodology or paradigm to design a program using classes and objects. It simplifies the software
development and maintenance by providing some concepts:
o Object
o Class
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation
Object
Any entity that has state and behaviour is known as an object. For example: chair, pen, table,
keyboard, bike etc. It can be physical and logical.
Class
[Type here]
[Type here]
Inheritance
When one object acquires all the properties and behaviours of parent object i.e. known as
inheritance. It provides code reusability. It is used to achieve runtime polymorphism.
Polymorphism
When one task is performed by different ways i.e. known as polymorphism. For example: to
convince the customer differently, to draw something e.g. shape or rectangle etc.
Encapsulation
Binding (or wrapping) code and data together into a single unit is known as encapsulation.
For example: capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is the fully encapsulated class because all
the data members are private here.
Benefits of Inheritance
One of the key benefits of inheritance is to minimize the amount of duplicate code in an
application by sharing common code amongst several subclasses. Where equivalent code
exists in two related classes, the hierarchy can usually be refactored to move the common
code up to a mutual superclass. This also tends to result in a better organization of code and
smaller, simpler compilation units.
Inheritance can also make application code more flexible to change because classes that
inherit from a common superclass can be used interchangeably. If the return type of a
method is superclass
Reusability - facility to use public methods of base class without rewriting the same.
Extensibility - extending the base class logic as per business logic of the derived class.
Data hiding - base class can decide to keep some data private so that it cannot be
The history of java starts from Green Team. Java team members (also known as Green Team),
initiated a revolutionary task to develop a language for digital devices such as set-top boxes,
televisions etc.
[Type here]
[Type here]
For the green team members, it was an advance concept at that time. But, it was suited for internet
programming. Later, Java technology as incorporated by Netscape.
Currently, Java is used in internet programming, mobile devices, games, e-business solutions etc.
There are given the major points that describes the history of java.
1) James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project
in June 1991. The small team of sun engineers called Green Team.
2) Originally designed for small, embedded systems in electronic appliances like set-top boxes.
3) Firstly, it was called "Greentalk" by James Gosling and file extension was .gt.
4) After that, it was called Oak and was developed as a part of the Green project.
There are many java versions that has been released. Current stable release of Java is Java SE 8.
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 March, 2014)
Features of Java
There is given many features of java. They are also known as java buzzwords. The Java Features
given below are simple and easy to understand.
1. Simple
2. Object-Oriented
3. Portable
4. Platform independent
5. Secured
6. Robust
7. Architecture neutral
8. Dynamic
9. Interpreted
10. High Performance
11. Multithreaded
12. Distributed
[Type here]
[Type here]
Java Comments
The java comments are statements that are not executed by the compiler and interpreter. The comments
can be used to provide information or explanation about the variable, method, class or any statement.
It can also be used to hide program code for specific time.
Syntax:
Output:
10
Syntax:
/*
This
is
multi line
comment
*/
[Type here]
[Type here]
Example:
Output:
10
The documentation comment is used to create documentation API. To create documentation API,
you needto use javadoc tool.
Syntax:
/**
Thi
s is
documentation
comment
*/
Example:
/** The Calculator class provides methods to get addition and subtraction of given 2 numbers.*/
public class Calculator {
/** The add () method returns addition of given numbers.*/
public static int add(int a, int b){return a+b;}
/** The sub() method returns subtraction of given numbers.*/
public static int sub(int a, int b){return a-b;}
}
[Type here]
[Type here]
javac Calculator.java
javadoc Calculator.java
Now, there will be HTML files created for your Calculator class in the current directory. Open the
HTMLfiles and see the explanation of Calculator class provided through documentation
comment.
Data Types
Data types represent the different values to be stored in the variable. In java, there are two types of data types:
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
Output:20
[Type here]
[Type here]
Variable is a name of memory location. There are three types of variables in java: local, instance
and static.
There are two types of data types in java: primitive and non-primitive.
Types of Variable
o local variable
o instance variable
o static variable
1) Local Variable
2) Instance Variable
A variable which is declared inside the class but outside the method, is called instance variable . It
is not declared as static.
3) Static variable
class A{
int data=50;//instance variable
static int m=100;//static variable
void method(){
int n=90;//local variable
}
Constants in Java
A constant is a variable which cannot have its value changed after declaration. It uses
the 'final'keyword.
Syntax
modifier final dataType variableName = value; //global constant
}//end of class
modifier static final dataType variableName = value; //constant within a c
[Type here]
[Type here]
Instance variables
Instance variables are those that are defined within a class itself and not in any method or constructor of the
class. They are known as instance variables because every instance of the class (object) contains a copy of
these variables. The scope of instance variables is determined by the access specifier that is applied to these
variables. We have already seen about it earlier. The lifetime of these variables is the same as the lifetime of
the object to which it belongs. Object once created do not exist for ever. They are destroyed by the garbage
collector of Java when there are no more reference to that object. We shall see about Java's automatic garbage
collector later on.
Argument variables
These are the variables that are defined in the header oaf constructor or a method. The scope of these
variables is the method or constructor in which they are defined. The lifetime is limited to the time for
which the method keeps executing. Once the method finishes execution, these variables are destroyed.
Local variables
A local variable is the one that is declared within a method or a constructor (not in the header). The scope
and lifetime are limited to the method itself.
One important distinction between these three types of variables is that access specifiers can be applied
to instance variables only and not to argument or local variables.
In addition to the local variables defined in a method, we also have variables that are defined in bocks life
an if block and an else block. The scope and is the same as that of the block itself.
DATA TYPES
[Type here]
[Type here]
There are eight primitive data types supported by Java. Primitive data types are predefined
by the language and named by a keyword. Let us now look into the eight primitive data
types in detail.
byte
short
[Type here]
[Type here]
int
long
float
double
boolean
char
[Type here]
[Type here]
Java Literals
A literal is a source code representation of a fixed value. Literals can be assigned to any
primitive type variable.
byte, int, long, and short can be expressed in decimal(base 10), hexadecimal(base 16) or
octal(base 8) number systems as well.
Prefix 0 is used to indicate octal, and prefix 0x indicates hexadecimal when using these
number systems for literals. For example −
String literals in Java are specified like they are in most other languages by enclosing a
sequence of characters between a pair of double quotes. Examples of string literals are −
Example
"Hello World" "two\nlines" "\"This is in quotes\""
String and char types of literals can contain any Unicode characters. For example −
char a = '\u0001'; String a = "\u0001";
Java language supports few special escape sequences for String and char
\n Newline (0x0a)
\f Formfeed (0x0c)
\b Backspace (0x08)
\s Space (0x20)
\t Tab
[Type here]
[Type here]
\\ Backslash
1. class Sample{
2. public static void main(String[] args){
3. int i=50;
4. int j=60;
5. int k=a+b;
6. System.out.println(k); 7. }
}
1. class Sample{
2. public static void main(String[] args){
3. int j=10;
4. float k=a;
5. System.out.println(i);
6. System.out.println(j); 7. }}
Unicode System
ASCII (American Standard Code for Information Interchange) for the United States.
ISO 8859-1 for Western European Language.
KOI-8 for Russian.
GB18030 and BIG-5 for chinese, and so on.
[Type here]
[Type here]
Java Tokens
Java Tokens are the smallest individual building block or smallest unit of a Java program,
it is used by the Java compiler for constructing expressions and statements. Java program
is collection different types of tokens, comments, and white spaces.
Java Supports Five Types of Tokens:
Variable
Variable is name of reserved area allocated in memory. In other words, it isa name of
memory location. It is a combination of "vary + able" that means its value can be
changed.
local variable
instance variable
static variable
[Type here]
1) Local Variable
2) Instance Variable
3) Static variable
<DEPARTMENT> PLAN-ACT-SUCCEED
16
SELF-ASSESSMENT
1. Fill in the Blanks Questions – minimum ten questions
A. Groovy
B. Java
C. C++
D. Scala
A. Javac -version
B. Javac – Version
C. Javac – ver
D. Javac -v
A. True
B. False
A. 0 to 256
B. -128 to 127
C. 0 to 65535
D. 0 to 32767
<DEPARTMENT> PLAN-ACT-SUCCEED
17
7. JDK stands for ____.
A. Dynamic
B. Architecture Neutral
C. Use of pointers
D. Object-oriented
A. 0
B. Not a Number
C. Infinity
D. Run time exception
<DEPARTMENT> PLAN-ACT-SUCCEED
18
3. Long Answer Questions – minimum two
1. Elaborate the difference between C++ and java
2. Explain in detail: Applications of java
4. Case Study Questions – minimum one, where Possible
Write essay on various types of editors.
5. Problem Solving Questions – minimum one, where Possible
How do you swap two numbers without using a third variable in C
6. Assignment – minimum one
Applications of java in real life
SUMMARY (in bullet points)
Java is a widely-used programming language for coding web applications. It has been a popular choice
among developers for over two decades, with millions of Java applications in use today. Java is a multi-
platform, object-oriented, and network-centric language that can be used as a platform in itself.
KEYWORDS
Object Oriented
Class
Object
TEXT BOOKS
Text Book:
1.E. Balagurusamy, “Programming With Java – A Primer”, TMH publication 4th Edition, 2011.
2.C.Xavier, “Programming With Java 2”, Scitech Publications (INDIA) Pvt. Ltd.2010 (UNIT IV)
Reference Book(s):
1. Patrick Naughton & Hebert Schildt,“The Complete Reference Java 2”, 6thEdition,
TMH Publication, 2012.
2. Herbert Schildt, “Java: A Beginner's Guide”, TMH Publication, 6th Edition, 2014.
3. D.T. Editorial Services , “Java 8 Programming Black Book”, Dream Tech Publication,2015
edition.
4. John R. Hubbard, “Programming with Java”, McGraw Hill Publication, 2nd Edition,2012
<DEPARTMENT> PLAN-ACT-SUCCEED
19
E-RESOURCES (Websites, YouTube, etc)
1. http://tutorialspoints.com/
2. Tutorials List - Javatpoin https://www.javatpoint.com/
<DEPARTMENT> PLAN-ACT-SUCCEED
20