0% found this document useful (0 votes)
7 views28 pages

Teaching Plan Java

The document outlines a lesson plan for Java programming, focusing on Object-Oriented Programming (OOP) concepts such as classes, objects, inheritance, polymorphism, and abstraction. It details Java's data types, including primitive and non-primitive types, as well as operators used in Java programming. The content aims to provide foundational knowledge for developing complex software using OOP principles.

Uploaded by

hardikjangid1112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views28 pages

Teaching Plan Java

The document outlines a lesson plan for Java programming, focusing on Object-Oriented Programming (OOP) concepts such as classes, objects, inheritance, polymorphism, and abstraction. It details Java's data types, including primitive and non-primitive types, as well as operators used in Java programming. The content aims to provide foundational knowledge for developing complex software using OOP principles.

Uploaded by

hardikjangid1112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Maharishi Arvind Institute of Science and Management

AmbaBari Circle, Amba Bari, Jaipur


Subject: Java Programming
Lesson Plan UNIT-I
Introduction to Object Oriented Programming Languages:
This unit have following components:
-Procedural Approach
-Data Approach
-Object Oriented Programming characteristics
-Functional Programming
-Data Types in Java
-Operators in Java for building expression

The OOPS approach is Data Approach. The data approach is used to develop complex software’s which was not possible
using procedural programming approach.
The data approach uses objects to develop the software’s.
What is an Object?
An Object is a real world entity. Object have attributes and behaviour’s.
Attribute means data and here behaviour means methods.
Objects can be represented in Java using class. Here class consists Data members and methods. Means that the data and
methods are wrap together in a unit called a class. Binding or wrapping of data and methods at one place is called Data
Encapsulation.
Data Encapsulation provides information hiding. Information hiding means protecting data to be accessed from outside
methods.
The main Characteristics of an Object Oriented Programming languages are:
i. Object
ii. Class
iii. Abstraction
iv. Inheritance
v. Polymorphism
vi. Message Passing
A class is a blue print of an object
Using a class we can create Objects for example:
Class Demo {}
Demo obj1, obj2,….;
What is abstraction?
Abstraction in Java is a fundamental Object-Oriented Programming (OOP) concept that focuses on hiding the
complex implementation details and showing only the essential features of an object.
Java Abstraction
Abstraction in Java is a fundamental Object-Oriented Programming (OOP) concept that focuses on hiding the
complex implementation details and showing only the essential features of an object. It helps in reducing
programming complexity and effort by providing a clear separation between the abstract properties and the
implementation details.

Purpose of Abstraction

 Simplification: Abstraction allows developers to work with complex systems by simplifying the interactions with
them.
 Code Reusability: By defining common interfaces, abstraction promotes code reusability across different
implementations.
 Enhanced Security: It hides the internal implementation details, thus protecting the code from unauthorized access
and modifications.

Implementing Abstraction in Java


Java provides abstraction through abstract classes and interfaces.

Abstract Classes

An abstract class in Java is a class that cannot be instantiated on its own and may contain abstract methods
(methods without a body) that must be implemented by its subclasses.
Interfaces
An interface in Java is a reference type that can contain abstract methods and static constants. Classes that implement an interface
must provide implementations for all its methods.

What is functional programming?


Functional programming is a programming paradigm where programs are built by composing functions, emphasizing
immutability and avoiding shared state to create cleaner, more predictable, and easier-to-test code.
What is functional programming vs. OOP?
Use OOP when you need to model complex systems with multiple entities and interactions, and when you need to encapsulate
data and behavior into reusable components. Use FP when you need to perform pure calculations with simple inputs and outputs,
and when you need to avoid side effects or state changes.

What is inheritance?
In Java, inheritance is a mechanism where one class, the subclass or child class, acquires the properties and
behaviors of another class, the superclass or parent class. This enables code reusability and promotes a hierarchical
structure among classes.
What is polymorphism?

In Java, polymorphism is a core object-oriented programming concept that allows objects of different classes to be
treated as objects of a common parent class. It enables a single action to be performed in different ways, achieving
flexibility and code reusability. This is achieved through method overriding (runtime polymorphism) and method
overloading (compile-time polymorphism).
What is overloading and Overriding in java?
In Java, method overloading allows multiple methods to have the same name but different parameters, while method
overriding allows subclasses to modify inherited methods. Both are important concepts in object-oriented
programming.
What is message passing in OOPs?
In object-oriented programming, communication between objects is a vital aspect of building complex systems. One of
the key mechanisms for achieving this communication is message passing. In Java, message passing allows objects to
interact with each other by invoking methods and passing data between them.

Data types in Java

Data Types in Java


In programming languages, data types specify the different sizes and values that can be stored in the variable or
constants. Each data type is predefined, which makes Java a statically and strongly typed language. There are the
following two types of data types in Java.

1. Primitive Data Types: The primitive data types include boolean, char, byte, short, int, long, float and double.
2. Non-Primitive Data Types: The non-primitive data types include Classes, Interfaces, String, and Arrays.

Java Primitive Data Types


In Java, primitive data types are the building blocks of data manipulation. These are the basic data types.

In Java, there are mainly eight primitive data types which are as follows.

1. boolean data type


2. char data type
3. byte data type
4. short data type
5. int data type
6. long data type
7. float data type
8. double data type
Java is a statically typed programming language. It means all variables must be declared before their use. That is
why we need to declare the variable's type and name. Let's discuss each data type one by one.

1. Boolean Data Type


In Java, the boolean data type represents a single bit of information with two possible states: true or false. The
size of the Boolean data type is 1 byte (8 bits).

It is used to store the result of logical expressions or conditions. Unlike other primitive data types like int or double,
boolean does not have a specific size or range. It is typically implemented as a single bit, although the exact
implementation may vary across platforms.

Syntax:

1. boolean flag;

Example
1. Boolean a = false;
2. Boolean b = true;

2. Byte Data Type


The byte data type in Java is a primitive data type that represents an 8-bits signed two's complement integer. It
has a range of values from -128 to 127. Its default value is 0.

The byte data type is commonly used when working with raw binary data or when memory conservation is a
concern, as it occupies less memory than larger integer types like int or long.

Syntax:

1. byte size;
Example
1. byte a = 10;
2. byte b = -20;

3. Short Data Type


The short data type in Java is a primitive data type that represents a 16-bits signed two-complement integer. Its
range of values is -32,768 to 32,767.

Similar to the byte data type, short is used when memory conservation is a concern, but more precision than byte
is required. Its default value is 0.

Syntax:

1. short var;

Example
1. short a = 10000;
2. short b = -5000;

4. int Data Type


The int data type in Java is a primitive data type that represents a 32-bits signed two's complement integer. It has
a range of values from -2,147,483,648 to 2,147,483,647.

The int data type is one of the most commonly used data types. It is typically used to store whole numbers without
decimal points. Its default value is 0.

Syntax:
1. int myInt = 54;
In Java, int variables are declared using the int keyword. For example, int myInt = 54 ; declares an int variable
named myInt and initializes it with the value 54. int variables can be used in mathematical expressions, assigned
to other int variables, and used in conditional statements.

Remember: In Java SE 8 and later versions, we can use the int data type to represent an unsigned 32-bit integer.
It has a value in the range [0, 232-1]. Use the Integer class to use the int data type as an unsigned integer.

Example
1. int a = 100000;
2. int b = -200000;

5. long Data Type


The long data type in Java is a primitive data type that represents a 64-bits signed two's complement integer. It
has a wider range of values than int, ranging from 9,223,372,036,854,775,808 to
9,223,372,036,854,775,807. Its default value is 0.0L or 0.0l.

The long data type is used when the int data type is not large enough to hold the desired value or when a larger
range of integer values is needed.

Syntax:

1. long num = 15000000000L;


2. long num = 9,223,372,036,854,775l
The long data type is commonly used in applications where large integer values are required, such as in scientific
computations, financial applications, and systems programming. It provides greater precision and a larger range
than int, making it suitable for scenarios where int is insufficient.
Example
1. long a = 5000000L;
2. long b = -6000000L;

Note: In Java SE 8 and later versions, we can use the long data type to represent an unsigned
64-bit long number. It has a minimum value of 0 and a maximum value of 2 64-1.

6. float Data Type


The float data type in Java is a primitive data type that represents single-precision 32-bits IEEE 754 floating-point
numbers. It can represent a wide range of decimal values, but it is not suitable for precise values such as currency.
Its default value is 0.0f or 0.0F.

The float data type is useful for applications where a higher range of values is needed and precision is not critical.

Syntax:

1. float num = 67;


One of the key characteristics of the float data type is its ability to represent a wide range of values, both positive
and negative, including very small and very large values. However, due to its limited precision (approximately 6-7
significant decimal digits), it is not suitable for applications where exact decimal values are required.

Example
1. float f = 234.5f;
7. double Data Type
The double data type in Java is a primitive data type that represents double-precision 64-bits IEEE 754 floating-
point numbers. Its default value is 0.0. It provides a wider range of values and greater precision compared to the
float data type, which makes it suitable for applications where accurate representation of decimal values is
required.

Syntax:

1. double num = 75.658d;


One of the key advantages of the double data type is its ability to represent a wider range of values with greater
precision compared to float. It can accurately represent values with up to approximately 15-16 significant decimal
digits, making it suitable for applications that require high precision, such as financial calculations, scientific
computations, and graphics programming.

Example
1. double d = 12.3;
Note: If accuracy is the most important concern, it is suggested not to use float and double
data types; use the BigDecimal class instead.

8. char Data Type


The char data type in Java is a primitive data type that represents a single 16-bits Unicode character. It can store
any character from the Unicode character set, which allows Java to support the internationalisation and
representation of characters from various languages and writing systems.

Syntax:

1. char alphabets = 'J';


2. char a = 60, b = 61, c = 62;
The char data type is commonly used to represent characters, such as letters, digits, and symbols. It can also be
used to perform arithmetic operations, as the Unicode values of characters can be treated as integers.

Example
1. char c = 'A';

Non-Primitive Data Types in Java


In Java, non-primitive data types are also known as reference data types. It is used to store complex objects rather
than simple values. Reference data types store references or memory addresses that point to the location of the
object in memory. This distinction is important because it affects how these data types are stored, passed, and
manipulated in Java programs.

1. Class
One common non-primitive data type in Java is the class. Classes are used to create objects, which are instances of
the class. A class defines the properties and behaviours of objects, including variables (fields) and methods.

2. Interface
Interfaces are another important non-primitive data type in Java. An interface defines a contract for what a class
implementing the interface must provide without specifying how it should be implemented. Interfaces are used to
achieve abstraction and multiple inheritance in Java, allowing classes to be more flexible and reusable.

3. Arrays
Arrays are a fundamental non-primitive data type in Java that allows you to store multiple values of the same type
in a single variable. Arrays have a fixed size, which is specified when the array is created and can be accessed
using an index. Arrays are commonly used to store lists of values or to represent matrices and other multi-
dimensional data structures.

4. String
In Java, a string is a sequence of characters. In simple words, we can define a string as an array of characters. The
difference between a character array and a string in Java is that the string is designed to hold a sequence of
characters in a single variable, whereas a character array is a collection of separate char-type entities. Note that,
unlike C/C++, Java strings are not terminated with a null character.

5. enum
Java also includes other non-primitive data types, such as enums and collections. Enums are used to define a set of
named constants, providing a way to represent a fixed set of values. Collections are a framework of classes and
interfaces that provide dynamic data structures such as lists, sets, and maps, which can grow or shrink in size as
needed.

What is Unicode System?


o Unicode system is an international character encoding technique that can represent most of the languages around the
world.
o Unicode System is established by Unicode Consortium.
o Hexadecimal values are used to represent Unicode characters.
o There are multiple Unicode Transformation Formats:

o UTF-8: It represents 8-bits (1 byte) long character encoding.


o UTF-16: It represents 16-bits (2 bytes) long character encoding
o UTF-32: It represents 32-bits (4 bytes) long character encoding.
o To access a Unicode character the format starts with an escape sequence \u followed by 4 digits hexadecimal value.
o A Unicode character has a range of possible values starting from \u0000 to \uFFFF.
o Some of the Unicode characters are
\u00A9 represent the copyright symbol - ©
\u0394 represent the capital Greek letter delta - Δ
\u0022 represent a double quote - "

Operators in Java
Operators are an essential part of any programming language. In Java, operator is a symbol that is used to perform
operations. For example: +, -, *, / etc. These are essential for performing different types of operations on variables and
values. In this section, we will discuss different types of operators used in Java programming.

There are mainly eight types of operators in Java:

1. Unary Operator
2. Arithmetic Operator
3. Relational Operator
4. Ternary Operator
5. Assignment Operator
6. Bitwise Operator
7. Logical Operator
8. Shift Operator

Java Unary Operator


The Java unary operators require only one operand. Unary operators are used to perform various operations i.e.
incrementing/decrementing a value by one, negating an expression and inverting the value of a Boolean. Observe
the following table.

Operator Name Description

+ Unary Plus Unary plus operator; indicates


positive value (numbers are
positive without this, however)

- Unary Minus The Unary Minus operator can be


used to make a positive value
negative.

! Logical Complement Operator The Not operator is used to


convert a boolean value. In other
words, it is used to reverse the
logical state of an operand.

++ Increment It is used to increase the value of


an operand by one. It can be done
in two possible ways. One is post-
increment (operand++) and other
is pre-increment (++operand).
In post-increment, the value of the
operand is used first, then its
value is increased by one.

In pre-increment, the value of


the operand is incremented first
by one, then the incremented
value of the operand is used.

-- Decrement It is used to decrease the value of


an operand by one. It can be done
in two possible ways. One is post-
decrement (operand--) and other
is pre-decrement (--operand).
In post-decrement, the value of
the operand is used first, then its
value is decreased by one.
In pre-increment, the value of
the operand is decrement first
by one, then the decremented
value of the operand is used.

Java Arithmetic Operators


Java arithmetic operators are used to perform addition, subtraction, multiplication, division, and modulo operation.
They act as basic mathematical operations.

Operator Name Description

+ Addition Adds two numbers.

- Subtraction Subtracts one number from


another number.

* Multiplication Multiplies two numbers.

/ Division Divides one number by another


number.

% Modulus Divides and returns the


remainder of two numbers.

Java Shift Operators


Java shift operator works on the bits of the data. It shifts the bits of the number from left to right or right to left.
The following table shows the types of shift operator in Java.

Operator Operator Name Description

<< Left Shift Operator (Signed) All of the bits shift to left by a
given number using this
operator.

>> Right Shift Operator (Signed) All of the bits shift to right by a
given number using this
operator.

>>> Unsigned Right Shift Operator It is similar to the right shift


operator (signed). However, the
vacant leftmost position is
occupied with 0 in place of the
signed bit
Java Relational Operators
Java relational or conditional operators are used to check relationship between two operands such as less than,
less than or equal to, greater than, greater than or equal to, equal to and not equal to. It is also known as equality
operator. These operators return Boolean values either true or false.

Operator Name Description

< Less Than If the value of the first operand


is lesser than the value of the
second operand, the Less Than
operator returns true, otherwise
false.

> Greater Than If the value of the first operand


is greater than the value of the
second operand, the Greater
Than operator returns true,
otherwise false.

<= Less Than or Equal to When the value of the first


operand is lesser than or equal
to the value of the second
operand, the Less Than or
Equal to operator returns true,
otherwise false.
>= Greater Than or Equal to When the value of the first
operand is greater than or
equal to the value of the
second operand, the Greater
Than or Equal to operator
returns true, otherwise false.

== Equal to Equal to operator checks


whether the given operands are
equal or not. If they are equal,
they return true otherwise
false.

!= Not Equal to Not Equal to operator works


just opposite to Equal to
operator. It returns false if the
operands are equal in value,
otherwise true.

Java Bitwise Operators


Java bitwise operators are used to perform the manipulation of individual bits of a number and with any of the
integer types. They are used when performing update and query operations of the Binary indexed trees.

Operator Operator Name Description


| Bitwise OR It is a binary operator which
gives OR of the input values bit
by bit.

& Bitwise AND It is a binary operator which


gives AND of the input values
bit by bit.

^ Bitwise XOR It is a binary operator which


gives XOR of the input values
bit by bit.

~ Bitwise Complement The Bitwise Complement


operator is also a unary
operator. It makes every bit 0
to 1, and 1 to 0.

Java Logical Operators


Logical operators are used extensively in various programming languages to perform Logical NOT, OR and AND
operations whose functionality is similar to OR gate and AND gate in the world of digital electronics.

Operator Name Description


&& Conditional AND Operator The logical && operator does
not check the second condition
if the first condition is false. It
checks the second condition
only if the first one is true.

|| Conditional OR Operator The logical || operator does not


check the second condition if
the first condition is true. It
checks the second condition
only if the first one is false.

! Bitwise Complement The NOT operator is used to


reverse the value of a Boolean
expression.

Java Ternary Operator


Java Ternary operator is used as one line replacement for if-then-else statement and used a lot in Java
programming. It is the only conditional operator which takes three operands.
Java Assignment Operator
Java assignment operator is one of the most common operators. It is used to assign the value on its right to the
operand on its left. The left operand has to be a variable and right operand contains value. Note that the data type
of both the operands has to be of the same type. If not, an error will be raised by the compiler. The associativity of
the assignment operators is form right to left, which means values on the right side of the operator is assigned to
the left side.

Types of Assignment Operator


There are two types of assignment operators. They are:

o Simple Assignment operator: The simple assignment operator where only (=) is used.
o Compound Assignment operator: The compound assignment operator is used where -,+,/,* are used along with (=)
operator.
The following table illustrates the working of the different assignment operators.
Types Operator Description

Single Assignment = It is the simplest assignment operator


Operator that assigns the value of the right side
to the variable of the left side.

Compound Assignment += It is a compound assignment operator


Operator which consists of + and =. It first adds
the current value of the variable
present on the left side to the value on
the right side and then assign the
result to the variable on the left side.

*= It is a compound assignment operator


which consists of * and =. It first
multiplies the current value of the
variable present on the left side to the
value on the right side and then assign
the result to the variable on the left
side.

/= It is a compound assignment operator


which consists of / and =. It first
divides the current value of the
variable present on the left side to the
value on the right side and then assign
the quotient to the variable on the left
side.

%= It is a compound assignment operator


which consists of % and =. It first
divides the current value of the
variable present on the left side to the
value on the right side and then assign
the remainder to the variable on the
left side.

Java Operator Precedence

Operator Type Precedence

Unary expr++ expr--

prefix ++expr --expr +expr -expr ~ !

Arithmetic */%

additive +-
Shift << >> >>>

Relational < > <= >= instanceof

equality == !=

Bitwise AND &

Bitwise exclusive OR ^

Bitwise inclusive OR I

Logical AND &&

Logical OR ||

Ternary ?:
Assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

You might also like