0% found this document useful (0 votes)
2 views

Unit-wisNotes_java1

This document provides an overview of Object Oriented Programming (OOP) principles and the Java programming language, including its history, features, and data types. It covers essential OOP concepts such as classes, objects, inheritance, polymorphism, abstraction, and encapsulation, as well as Java's evolution and its applications. Additionally, it outlines learning outcomes and includes examples of Java comments, variables, and constants.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit-wisNotes_java1

This document provides an overview of Object Oriented Programming (OOP) principles and the Java programming language, including its history, features, and data types. It covers essential OOP concepts such as classes, objects, inheritance, polymorphism, abstraction, and encapsulation, as well as Java's evolution and its applications. Additionally, it outlines learning outcomes and includes examples of Java comments, variables, and constants.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

[Type here]

UNIT-1 NOTES
< Object Oriented Programming with Java - 23BDA2C20>

UNIT 1 (Copy-Paste from the Syllabus)

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.

TOPICS (Should match with the Teaching Plan in CAMU)


NO. TOPICS
1 Fundamentals of Object Oriented Programming
2 Introduction- Object Oriented Paradigm
3 Basic Concepts of Object Oriented Programming
4 Benefits of OOP
5 Applications of OOP
6 Java Evolution: Java History
7 Java Features
8 How java differs from C and C++.
9 Overview of Java Language
10 Constants, Variables
11 Data types.
12 interview questions on 1st unit

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.

Platform: Any software or hardware environment in which a program runsis known


as a platform. Java has its own runtime environment (JRE) and API so java is
also called as platform.

Java fallows the concept of Write Once, Run Anywhere.


Application of java
1. Desktop Applications
2. Web Applications
3. Mobile
4. Enterprise Applications
5. Smart Card
6. Embedded System
7. Games
8. Robotics et

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:

 Object Oriented – Java implements basic concepts of Object oriented


programming System (OOPS) ie Object, Class, Inheritance, Polymorphism,
Abstraction, Encapsulation. In Java, everything is an Object. Java can be
easily extended since it is based on the Object model.
 Platform Independent − Unlike many other programming languages including
C and C++, when Java is compiled, it is not compiled into platform specific
machine, rather into platform independent byte code. This byte code is
distributed over the web and interpreted by the Virtual Machine (JVM) on
whichever platform it is being run on.
 Simple – Java fallows the basic Syntax of C,C++. If you understand the
basic concept of OOPS then it is easy to master in java.
 Secure − With Java's secure feature it enables to develop virus-free, tamper-
free systems. Authentication techniques are based on public- key encryption.
 Architecture-neutral − Java compiler generates an architecture- neutral object
file format, which makes the compiled code executable on many processors,
with the presence of Java runtime system.
 Portable − Being architecture-neutral and having no implementation
dependent aspects of the specification makes Java portable. Compiler in Java
[Type here]
[Type here]

is written in ANSI C with a clean portability boundary, which is a POSIX


subset.
 Robust − Java makes an effort to eliminate error prone situations by
emphasizing mainly on compile time error checking and runtime checking.
 Multithreaded − With Java's multithreaded feature In java we can write
programs that can perform many tasks simultaneously. This design feature
allows the developers to construct interactive applications that can run
smoothly.
 Interpreted − Java byte code is translated on the fly to native machine
instructions and is not stored anywhere. The development process is more
rapid and analytical since the linking is an incremental and light-weight
process.
 High Performance − With the use of Just-In-Time compilers, Java enables
high performance.
 Distributed − Java is designed for the distributed environment of the internet.
 Dynamic − Java is considered to be more dynamic than C or C++ since it
is designed to adapt to an evolving environment.

OOP Concepts

Object Oriented Programming is a paradigm that provides many concepts such as


inheritance, data binding, polymorphism etc.

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.

Smalltalk is considered as the first truly object-oriented programming language.

OOPs (Object Oriented Programming System)

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

Collection of objects is called class. It is a logical entity.

[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.

In java, we use method overloading and method overriding to achieve polymorphism.


Another example can be to speak something e.g. cat speaks meaw, dog barks woof etc
Abstraction
Hiding internal details and showing functionality is known as abstraction.
For example: phonecall, we don't know the internal processing.
In java, we use abstract class and interface to achieve abstraction.

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

altered by the derived class

Procedural and object oriented programming paradigms


Java Programming- History of Java

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.

Java Version History

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.

Types of Java Comments

There are 3 types of comments in java.

1. Single Line Comment


2. Multi Line Comment
3. Documentation Comment

Java Single Line Comment

The single line comment is used to comment only one line.

Syntax:

1. //This is single line comment


Example:

public class CommentExample1 {


public static void main(String[] args) {
int i=10;//Here, i is a variable
System.out.println(i);
}
}

Output:

10

Java Multi Line Comment

The multi line comment is used to comment multiple lines of code.

Syntax:

/*
This
is
multi line
comment
*/

[Type here]
[Type here]

Example:

public class CommentExample2 {


public static void main(String[] args) {
/* Let's declare and
print variable in java. */
int i=10;
System.out.println(i);
}}

Output:

10

java Documentation Comment

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;}
}

Compile it by javac tool:

[Type here]
[Type here]

javac Calculator.java

Create Documentation API by javadoc tool:

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:

o Primitive data types


o Non-primitive data types

Data Type Default Value Default size

boolean False 1 bit

char '\u0000' 2 byte

byte 0 1 byte

short 0 2 byte

int 0 4 byte

long 0L 8 byte

float 0.0f 4 byte

double 0.0d 8 byte

Java Variable Example: Add Two Numbers


class Simple{
public static void main(String[] args){
int a=10;
int b=10;
int c=a+b;
System.out.println(c);
}}

Output:20

[Type here]
[Type here]

Variables and Data Types in Java

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

There are three types of variables in java:

o local variable
o instance variable
o static variable

1) Local Variable

A variable which is declared inside the method is called 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

A variable that is declared as static is called static variable. It cannot be local.


We will have detailed learning of these variables in next chapters.
Example to understand the types of variables in java

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]

Scope and Life Time of Variables


The scope of a variable defines the section of the code in which the variable is visible. As a general rule,
variables that are defined within a block are not accessible outside that block. The lifetime of a variable
refers to how long the variable exists before it is destroyed. Destroying variables refers to deallocating the
memory that was allotted to the variables when declaring it. We have written a few classes till now. You
might have observed that not all variables are the same. The ones declared in the body of a method were
different from those that were declared in the class itself. There are three types of variables: instance
variables, formal parameters or local variables and local variables.

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

There are two data types available in Java −

 Primitive Data Types


 Non Primitive Types

[Type here]
[Type here]

Primitive Data Types

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

 Byte data type is an 8-bit signed two's complement integer


 Minimum value is -128 (-2^7)
 Maximum value is 127 (inclusive)(2^7 -1)
 Default value is 0
 Byte data type is used to save space in large arrays, mainly in place of integers,
since a byte is four times smaller than an integer.
 Example: byte a = 100, byte b = -50

short

 Short data type is a 16-bit signed two's complement integer


 Minimum value is -32,768 (-2^15)
 Maximum value is 32,767 (inclusive) (2^15 -1)
 Short data type can also be used to save memory as byte data type. A short is 2
times smaller than an integer
 Default value is 0.
 Example: short s = 10000, short r = -2000

[Type here]
[Type here]

int

 Int data type is a 32-bit signed two's complement integer.


 Minimum value is - 2,147,483,648 (-2^31)
 Maximum value is 2,147,483,647(inclusive) (2^31 -1)
 Integer is generally used as the default data type for integral values unless there is a
concern about memory.
 The default value is 0
 Example: int a = 100000, int b = -200000

long

 Long data type is a 64-bit signed two's complement integer


 Minimum value is -9,223,372,036,854,775,808(-2^63)
 Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
 This type is used when a wider range than int is needed
 Default value is 0L
 Example: long a = 100000L, long b = -200000L

float

 Float data type is a single-precision 32-bit IEEE 754 floating point


 Float is mainly used to save memory in large arrays of floating point numbers
 Default value is 0.0f
 Float data type is never used for precise values such as currency
 Example: float f1 = 234.5f

double

 double data type is a double-precision 64-bit IEEE 754 floating point


 This data type is generally used as the default data type for decimal values, generally
the default choice
 Double data type should never be used for precise values such as currency
 Default value is 0.0d
 Example: double d1 = 123.4

boolean

 boolean data type represents one bit of information


 There are only two possible values: true and false
 This data type is used for simple flags that track true/false conditions
 Default value is false
 Example: boolean one = true

char

 char data type is a single 16-bit Unicode character

[Type here]
[Type here]

 Minimum value is '\u0000' (or 0)


 Maximum value is '\uffff' (or 65,535 inclusive)
 Char data type is used to store any character
 Example: char letterA = 'A'

Java Literals

A literal is a source code representation of a fixed value. Literals can be assigned to any
primitive type variable.

byte a = 68; char a = 'A'

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 −

int decimal = 100; int octal = 0144; int hexa = 0x64;

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

Notation Character represented

\n Newline (0x0a)

\r Carriage return (0x0d)

\f Formfeed (0x0c)

\b Backspace (0x08)

\s Space (0x20)

\t Tab

\" Double quote

[Type here]
[Type here]

\' Single quote

\\ Backslash

\ddd Octal character (ddd)

\uxxxx Hexadecimal UNICODE character (xxxx)

Java Variable Example: Add Two Numbers

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. }
}

Java Variable Example: Widening

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

Unicode is a universal international standard character encoding that iscapable of


representing most of the world's written languages.

Before Unicode, there were many language standards:

 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:

 Reserved Keywords Identifiers Literals


 Operators Separators

Java Keywords can not be used as a variable name.

Abstract Assert boolean break


Byte Case catch char
Class Const continue default
Do Double else enum
extends Final finally float
For Goto if implements
Import Instanceof int interface
Long Native new package
private Protected public return
Short Static strictfp super
Switch synchronized this throw
throws Transient try void
volatile While true false
Null

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.

There are three types of variables in java:

 local variable
 instance variable
 static variable

[Type here]
1) Local Variable

A variable which is declared inside the method is called local variable.

2) Instance Variable

A variable which is declared inside the class but outside the


method, iscalled instance variable. It is not declared as static.

3) Static variable

A variable that is declared as static is called static variable. It


cannot belocal.

<DEPARTMENT> PLAN-ACT-SUCCEED

16
SELF-ASSESSMENT
1. Fill in the Blanks Questions – minimum ten questions

1. On which platforms Java runs?


A. Windows
B. Mac OS
C. UNIX
D. All of these

2. Who is known as father of Java Programming Language?


A. James Gosling
B. M. P Java
C. Charel Babbage
D. Blais Pascal

3._______ Language is not supported by Java platform.

A. Groovy
B. Java
C. C++
D. Scala

4. How to find the version of Java compiler?

A. Javac -version
B. Javac – Version
C. Javac – ver
D. Javac -v

5. Java is case sensitive language

A. True
B. False

6. What is the numerical range of a char data type in Java?

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. Java development kit


B. Java deployment kit
C. JavaScript deployment kit
D. None of these

8. JRE stands for ___.

A. Java run ecosystem


B. JDK runtime Environment
C. Java Runtime Environment
D. None of these

9. Which of the following is not a Java features?

A. Dynamic
B. Architecture Neutral
C. Use of pointers
D. Object-oriented

10. What does the expression float a = 35 / 0 return?

A. 0
B. Not a Number
C. Infinity
D. Run time exception

1.Match the Following – minimum five options


1. Keyword –java developer-3
2. Double- 1 bit-5
3. James Gosling- array-4
4. Not primitive datatype- double size of data of float data type-2
5. size of boolean datatype- Reserved word-1

2.Short Answer Questions – minimum five


1. Define Datatype
2. Describe features of java
3. Define variables
4. Define Constants
5. Write Datatype types

<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.

(UNIT I, II, III, IV, V).

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

You might also like