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

OOP1 Unit1

Uploaded by

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

OOP1 Unit1

Uploaded by

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

Object Oriented Programming 1

OOP1 - 3140705
• Java is an object-oriented programming
language.

• What is Object?
What is Object?
• Everything in Java is associated with classes and
objects, along with its attributes and methods.

• For example: in real life, a car is an object.

• The car has attributes, such as model, brand and


color, and methods, such as speed, drive and brake.
Class ?
• A class is a user defined blueprint or prototype
from which objects are created.

• It represents the set of properties or methods


that are common to all objects of one type.
History of Java
• Java is an object-oriented programming language
developed by James Gosling and colleagues at Sun
Microsystems in the early 1990s.

• The primary motivation was the need for a platform


independent language that could be used to create
software to be embedded in various consumer electronics
devices.

• Java derives much of its characteristics from C and C++.

• syntax of C and Object oriented features of C++ derived in


java.
Execution of Java Programs
• Java Programs are collection of a whitespace,
identifiers, comments, literals, operators and
keywords.

1. Whitespace : Java is a free form language


that means you do not need to follow any
special indentation rule. In java, whitespace
is a space, tab or newline.
2. Identifiers
• Identifiers are used for class name, method name and
variable name.
• It can be a combination of uppercase and lowercase letters,
numbers or the underscore and dollar sign character.
• They must not begin with a number.
• Examples of Valid Identifiers are
sales_tax, _circleArea, box100width, $directory,
ab1234$$
• Examples of Valid Identifiers are
• 2num, num-oranges, num students
3. Literals
• A constant value in java is created by using a literal representation
of it.
• Literals are integer, floating –point, character and sting
• E.g. 100, 18.6, ‘w’ and “ This is String”

4. Comments - Three types


• Single line //
• Multi line /*……………….. */
• Documentation comment /**……………… */

5. Java Keywords
• Java keywords are also known as reserved words.
• These are predefined words by Java so it cannot be used as a
variable or object name.
• e.g. boolean, byte, abstract, break, char, class, continue, default, do,
while
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:

• Primitive data types: The primitive data types include


boolean, char, byte, short, int, long, float and double.

• Non-primitive data types: The non-primitive data types


include Classes, Interfaces, and Arrays.
There are 8 types of primitive data types:
boolean Data Type
• The Boolean data type is used to store only two
possible values: true and false.
• boolean one = false

• byte Data Type


• Its value-range lies between -128 to 127 (inclusive).
• Its minimum value is -128 and maximum value is
127. Its default value is 0.
short Data Type
• Its value-range lies between -32,768 to 32,767 (inclusive). Its
minimum value is -32,768 and maximum value is 32,767. Its
default value is 0.
• Example: short s1 = 10000;

• int Data Type


• Its value-range lies between - 2,147,483,648 (-2^31) to
2,147,483,647 (2^31 -1) (inclusive). Its minimum value is -
2,147,483,648and maximum value is 2,147,483,647. Its
default value is 0.
• Example: int a = 100000;
long Data Type
• Its value-range lies between -9,223,372,036,854,775,808(-2^63)
to 9,223,372,036,854,775,807(2^63 -1)(inclusive). Its default
value is 0.
• Example: long a = 100000L;

float Data Type

• You should use a floating point type whenever you need a


number with a decimal, such as 9.99 or 3.14515.
• The float data type can store fractional numbers from 3.4e−038
to 3.4e+038. Note that you should end the value with an "f":
• float myNum = 5.75f;
double Data Type
• The double data type can store fractional numbers
from 1.7e−308 to 1.7e+308.
• Example: double d1 = 12.3;

Use float or double?


• The precision of a floating point value indicates
how many digits the value can have after the
decimal point.
• The precision of float is only six or seven decimal
digits, while double variables have a precision of
about 15 digits.

• Therefore it is safer to use double for most


calculations.
Variable
• The variable is the basic unit of storage in java
program.
• A variable is defined by the combination of an
identifier, a type and an optional initializer.
• Declaring a variable:
type identifier[=value][,identifier[=value]….];
e.g. int a;
int a,b;
int a=10,b=20;
Dynamic Initialization
If any variable is not assigned with value at compile-
time and assigned at run time is called dynamic
initialization of a variable.
class DynInt
{
public static void main(String args[])
{
double a=3.0, b=4.0;
double c = Math.sqrt(a*a + b*b);
System.out.print(“C = “ + c);
}
}
Operators in Java
Operator in Java is a symbol which is used to perform operations.

• Unary Operator
• Arithmetic Operator
• Relational Operator
• Assignment Operator
• Bitwise Operator
• Logical Operator
• Ternary Operator
• Shift Operator
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


• inverting the value of a boolean
Java Unary Operator Example: ++ and –

class OperatorExample
{
public static void main(String args[])
{
int x=10;
System.out.println(x++); //10 (11)
System.out.println(++x); //12
System.out.println(x--); //12 (11)
System.out.println(--x); //10
}
}
Java Unary Operator Example – inverting boolean value

class OperatorExample
{
public static void main(String args[])
{
boolean c=true;
boolean d=false;
System.out.println(!c);//false (opposite of boolean value)
System.out.println(!d);//true
}
}
Arithmetic Operators
Java arithmetic operators are used to perform addition, subtraction, multiplication,
and division. They act as basic mathematical operations.

class Demo
{
public static void main(String[] args)
{
int a = 12, b = 5;
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b));
System.out.println("a % b = " + (a % b));
}
}
Output
a + b = 17
a-b=7
a * b = 60
a/b=2
a%b=2
Relational Operator
Relational operators are used to check the relationship between two operands.
Note: Relational operators are used in decision making and loops.

class Main
{
public static void main(String[] args)
{
int a = 7, b = 11;

System.out.println("a is " + a + " and b is " + b);


System.out.println(a == b); // false
System.out.println(a != b); // true
System.out.println(a > b); // false
System.out.println(a < b); // true
System.out.println(a >= b); // false
System.out.println(a <= b); // true
}
}
Assignment Operator
Assignment operators are used in Java to assign values to
variables. it assigns the value on its right to the operand on its
left.

The value on the right side must be of the same data-type of the
operand on the left side otherwise the compiler will raise an error.

The general format of assignment operator is,


variable operator value;
For example,
int age;
age = 5;
Bitwise Operators
Java defines several bitwise operators, which can be
applied to the integer types, long, int, short, char, and
byte. Bitwise operator works on bits and performs the bit-
by-bit operation.

• Bitwise AND (&)


• Bitwise OR (|)
• Bitwise XOR (^)
• Bitwise Compliment (~)

Assume if a = 60 and b = 13; now in binary format they


will be as follows −

a = 0011 1100
b = 0000 1101
a = 0011 1100
b = 0000 1101

& (bitwise and) - Binary AND Operator copies a bit to the result if it exists in both
operands.
a&b = 0000 1100

| (bitwise or) - Binary OR Operator copies a bit if it exists in either operand.


a|b = 0011 1101

^ (bitwise XOR) - Binary XOR Operator copies the bit if it is set in one operand but
not both.
a^b = 0011 0001

~ (bitwise compliment) - Binary Ones Complement Operator is unary and has the
effect of 'flipping' bits.
~a = 1100 0011
Logical Operator
&& (Logical AND)
|| (Logical OR)
! (Logical NOT)
class Main
{
public static void main(String[] args)
{

// && operator
System.out.println((5 > 3) && (8 > 5)); // true
System.out.println((5 > 3) && (8 < 5)); // false

// || operator
System.out.println((5 < 3) || (8 > 5)); // true
System.out.println((5 > 3) || (8 < 5)); // true
System.out.println((5 < 3) || (8 < 5)); // false

// ! operator
System.out.println(!(5 == 3)); // true
System.out.println(!(5 > 3)); // false
}
}
Ternary Operator
A ternary operator evaluates the test condition
and executes a block of code based on the result
of the condition.

It's syntax is:

condition ? expression1 : expression2;

Here, condition is evaluated and


if condition is true, expression1 is executed.
And, if condition is false, expression2 is executed.
class Ternary {
public static void main(String[] args)
{

// variable declaration
int n1 = 5, n2 = 10, max;

System.out.println("First num: " + n1);


System.out.println("Second num: " + n2);

// Largest among n1 and n2


max = (n1 > n2) ? n1 : n2;

// Print the largest number


System.out.println("Maximum is = " + max);
}
}
Output:
First num: 5
Second num: 10
Maximum is = 10
Shift Operator
• Bitwise left shift (<<)
• Bitwise right shift (>>)
• Shift Right zero fill (>>>)

a = 0011 1100 b = 0000 1101

<< (left shift) - The left operands value is moved left by the number of bits specified by the
right operand.

A << 2 will give 240 which is 1111 0000

>> (right shift) - The left operands value is moved right by the number of bits specified by
the right operand.
A >> 2 will give 15 which is 1111

>>> (zero fill right shift) - Shift right zero fill operator. The left operands value is moved right
by the number of bits specified by the right operand and shifted values are filled up
with zeros.
A >>>2 will give 15 which is 0000 1111

>>> always shifts a zero into the leftmost position whereas >> shifts based on sign of the
number i.e. 1 for negative number and 0 for positive number.
Java Operator Precedence
Operator precedence determines the order in which the operators in an
expression are evaluated.

Operator precedence determines the grouping of terms in an expression. This


affects how an expression is evaluated.

Certain operators have higher precedence than others; for example, the
multiplication operator has higher precedence than the addition operator −

For example, x = 7 + 3 * 2;

here x is assigned 13, not 20 because operator * has higher precedence than +,
so it first gets multiplied with 3 * 2 and then adds into 7.
Scope of Variables in Java

Scope is that area of the program where the variable is


visible to a program and can be used (accessible). i.e.
the scope of variable determines its accessibility for
other parts of program.

Java allows declaring variables within any block. A block


defines scope that starts with an opening curly brace
and ends with a closing curly brace.
class Scope
{
public static void main(String args[])
{
int x;
x=10;
if(x==10)
{
int y=20;
System.out.println(“X=“+x+”Y=“+y)
x=y*2;
}
y=100; // Error
System.out.println(“X is = “+x);
}
}
Type Conversion
When you assign value of one data type to another, the two types might not be
compatible with each other.

If the data types are compatible, then Java will perform the conversion
automatically known as Automatic Type Conversion and if not then they need
to be casted or converted explicitly.

For example, assigning an int value to a long variable.

Widening or Automatic Type Conversion

Widening conversion takes place when two data types are automatically
converted. This happens when:
– The two data types are compatible.
– When we assign value of a smaller data type to a bigger data type.
For Example, in java the numeric data types are compatible with each other
but no automatic conversion is supported from numeric type to char or
boolean.

Also, char and boolean are not compatible with each other.

Widening Casting (automatically) - converting a smaller type to a larger type


class Main
{
public static void main(String[] args)
{
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double

System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0
}
}
Narrowing Casting
Narrowing casting must be done manually by placing the type in parentheses
in front of the value:
for example, what if you want to assign an int value to a byte variable?
This conversion will not be performed automatically, because a byte is smaller
than an int.

public class Main


{
public static void main(String[] args)
{
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int

System.out.println(myDouble); // Outputs 9.78


System.out.println(myInt); // Outputs 9
}
}

You might also like