0% found this document useful (0 votes)
11 views50 pages

OOP-Introduction To Java

OOP Java

Uploaded by

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

OOP-Introduction To Java

OOP Java

Uploaded by

indu jaiswar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Object-Oriented Programming

Object-Oriented Programming
(01CT0105)

Object Oriented
Programming

Prof. Indu Jaiswal


Department of ICT
Marwadi University
Introduction to
JAVA
 Java is a general-purpose computer-programming language that is open
source, platform independent, object-oriented and specifically designed to
have as few implementation dependencies as possible.
 Java was originally developed by James Gosling at Sun Microsystems and
released in 1995.
 Java was
Current initially named
Version Javaas
SE Oak
19.0. language
2 (as of Feb,and
10th renamed
2023) to JAVA in 1995.
Version we will use Java SE 17.0.6 (LTS)
Setup size 149 MB (Linux), 153 MB (Windows x64)
Download Link https://www.oracle.com/in/java/technologies/downloads/
Official Website https://java.com
Integrated Development 1. Eclipse NetBeans
Environment 2. IntelliJ IDEA Community Edition
(IDE) 3. BlueJ
Features of JAVA
Simple: Java inherits C/C++ syntax and many object-oriented features
of C++.

Object Oriented: “Everything is an object” paradigm, which possess


some state, behavior and all the operations are performed using
these objects.

Robust: Java has a strong memory management system. It helps in


eliminating error as it checks the code during compile and runtime.

Multithreaded: Java supports multiple threads of execution, including


a set of synchronization primitives. This makes programming with
threads much easier.
Features of JAVA (Cont.)
Architectural Neutral: Java is platform independent which means that
any application written on one platform can be easily ported to
another platform.

Interpreted: Java is compiled to byte codes, which are interpreted by


a Java run-time environment.

High Performance: Java achieves high performance through the use


of byte code which can be easily translated into native machine code.
With the use of JIT (Just-In-Time) compilers, Java enables high
performance.

SOR MAHI
Features of JAVA (Cont.)

Distributed: Java provides a feature which helps to create distributed


applications. Using Remote Method Invocation (RMI), a program can
invoke a method of another program across a network and get the
output. You can access files by calling the methods from any
machine on the internet.

Dynamic: Java has ability to adapt to an evolving environment which


supports dynamic memory allocation due to which memory wastage is
reduced and performance of the application is increased.
Components of Java
 Java Virtual Machine (JVM)
JDK  Java Runtime
Environment (JRE)
Compiler
 Java Development Kit
Java
(javac.exe) Packages (JDK)
Java (math, util, JVM
Application awt etc…)

Launcher
(java.exe), Runtime
AppletViewer Libraries
, etc..)
Development JRE
tools
Java Development Kit (JDK)
 JDK contains tools needed ,
 To develop the Java programs
and
 JRE to run the programs.
 The tools include
 compiler (javac.exe),
 Java application launcher
(java.exe),
 Appletviewer, etc…
 Java application launcher
(java.exe) opens a JRE, loads the
class, and invokes its main
method.
Java Development Kit (JDK)

OOP Java is the easiest, scoring and my favorite subject

9
Java Development Kit (JDK)
JDK and JRE: The JDK enables programmers to create core
OOP Java is the easiest, scoring and my favorite subject

Java programs that can be run by the JRE, which included JVM
and class libraries.
Class Libraries: It is a group of dynamically loadable
libraries that Java program can call at run time.
Compilers: It is a Java program that accepts text file of
developers and compiles into Java class file. It is the common
form of output given by compiler, which contains Java byte
code. In Java, the primary compiler is Javac.
Debuggers: Debugger is a Java program that lets developers
test and debug Java programs.
JavaDoc: JavaDoc is documentation made by Sun
Microsystems for the Java. JavaDoc can be used generating
API documentation in HTML file from the source program. 10
Java Runtime Environment (JRE)
 The JRE is required to run java applications.
 It combines the Java Virtual Machine (JVM),
platform core classes and supporting libraries.
 JRE is part of the Java Development Kit (JDK), but
can be downloaded separately.
 It does not contain any development tools such as
compiler, debugger, etc.
Java Runtime Environment (JRE)

Class loaders: The class loader loads various


OOP Java is the easiest, scoring and my favorite subject

classes that are necessary for running a Java


program. JVM uses three class loaders called the
bootstrap class loader, extensions class
loader, and system class loader.
Byte code verifier: Byte code verifier verifies the
byte code so that the code doesn’t disturb the
interpreter.
Interpreter: Once the classes get loaded, and the
code is verified, the interpreter reads the code line
by line.
Run-time: Run-time is a system used mainly in
programming to describe time period during which
a particular program is running.
Hardware: Once you compile Java native code,12 it
Java Virtual Machine (JVM)
 JVM is a virtual machine that enables a computer to
run Java programs as well as programs written in
other languages and compiled to Java Bytecode.
 Bytecode is a highly optimized set of instructions
designed to be executed by the Java Virtual
Machine(JVM).
 Byte code is intermediate representation of java
source code.
 Java compiler provides byte code by compiling Java
Source Code.
 Extension for java class file or byte code is
‘.class’, which is platform independent.
 JVM is virtual because , It provides a machine
interface that does not depend on the operating
system and machine hardware architecture.
How Java become Platform Independent?
Source
code .java file
(Program)

Compil
er

Bytecode .class file

JVM
JVM JVM
(Window
(Linux) (Mac)
s)

Machine Machine Machine


Code Code Code
Java Interview Question
1. Difference between JRE and JVM?
2. Difference between interpreter and JIT compiler?
3. Why Java is platform independent?
4. What are Java bytecodes?
5. JVM vs. JRE vs. JDK
OOP Java is the easiest, scoring and my favorite subject

16
Hello World Java ProgramFile must be saved as
HelloWorld.java
public class HelloWorld
Main method from where execution
{ will start
public static void main(String[] args)
{ String must start with capital
letter
System.out.println("Hello World");
} System must start with
capital letter
}

 We have to save this in HelloWorld.java file as it has public class named


HelloWorld.
 String and System are inbuilt Java Classes.
 Classes in java are always written in Camel case.
How to execute Java Program?
1. Save the program with the same name as the public class with .java
extension.

2. Open command prompt (cmd) / terminal & navigate to desired directory /


folder.

3. Compile the “.java” file with javac command.

4. Execute the “.class” file with java command without extension.


Tokens
Tokens
 The smallest individual unit of a language / program is known as a token.
 Tokens are basic building blocks of any language.
“Jane bakes tasty cookies.”
 Jane is noun
 bakes is verb
 tasty is adjective
 cookies is noun
 ‘.’ is special character to end the sentence.
 Each and every word and punctuation is a token.
 We divide sentence into tokens to understand the meaning of a sentence.
 Similarly, the compilers of programming language breaks a program into
the tokens and proceeds to the various stages of the compilation.
 However, collection of tokens in appropriate sequence makes a
meaningful sentence.
Classification of Tokens
Sr. Token Description Examples
void, int, float,
1 Keywords Predefined reserved words
for, if
User-defined combination of alphanumeric
a, i, sum,
2 Identifiers characters.
number, pi
Name of a variable, function, class, etc.
3 Constants Fixed values that do not change 17, -25.50, 82, 0

4 Strings A sequence of characters “Darshan”, “Hi!”


Special
5 Symbols that have special meaning #, $, @, %, =, :, ;
Symbols
A symbol that performs operation on a value or
6 Operators +, -, *, /
a variable
Identifiers
Identifiers
 They are used for class names, method names and variable names.
 An identifier may be any descriptive sequence of
 uppercase(A…Z) and lowercase(a..z) letters
 Numbers(0..9)
 Underscore(_) and dollar-sign($) characters
 Examples for valid Identifiers,
 AvgTemp
 count
 a4
 $test
 this_is_ok
 Examples for invalid Identifiers,
(Identifiers can not start with digit)
 2count
(Identifiers can not contain dash)
 High-temp
(Identifiers can not contains slash)
 Ok/NotOK
Identifier Name Valid or Invalid?

for Rajkot _Name If Roll Number Rs.

_________a int student _7 Identifier Valid

C C++ Java C# Compiler ___8__a__8

7Student Who? v.a.l.u. A_B_C_D_E i if

Int A2020 2021 sum a,b,c i;


Data Types

Java Datatypes

Primitiv Non-
e primitive

Floating-
Integer Charact Boolea
point Class
s ers n
numbers
Primitive Data Types
Data Type Size Range Example
byte 1 Byte -128 to 127 byte a = 10;
short 2 Bytes -32,768 to 32,767 short a = 200;
int 4 Bytes -2,147,483,648 to int a = 50000;
2,147,483,647
long 8 Bytes - long a = 20;
9,223,372,036,854,775,80
float 4 Bytes 1.4e-045
8 to to 3.4e+038 float a = 10.2f;
9,223,372,036,854,775,80
7
double 8 Bytes 4.9e-324 to 1.8e+308 double a = 10.2;

char 2 Bytes 0 to 65536 (Stores ASCII char a = ‘a’;


of character)
boolean Not true or false boolean a = true;
defined
Escape Sequences
 Escape sequences in general are used to signal an alternative
interpretation of a series of characters.
 For example, if you want to put quotes within quotes you must use the
escape sequence, \", on the interior quotes.
System.out.println("Good Morning \"World\"");

Escape Sequence Description


\’ Single quote
\” Double quote
\\ Backslash
\r Carriage return
\n New Line
\t Tab
Type Casting
 Assigning a value of one type to a variable of another type is known as
Type Casting.
 In Java, type casting is classified into two types,
 Widening/Automatic Type Casting (Implicit)

 Narrowing Type Casting(Explicitly done)


Automatic Type Casting
 When one type of data is assigned to other type of variable , an automatic
type conversion will take place if the following two conditions are satisfied:
 The two types are compatible
 The destination type is larger than the source type
 Such type of casting is called “widening conversion”.
 Example:
int can always hold values of byte and short
public static void main(String[] args) {
byte b = 5;
// √ this is correct
int a = b;
}
Casting Incompatible Types
 To create a conversion between two incompatible types, you must use a
cast
 A cast is an explicit type conversion.
 Such type is called “narrowing conversion”.
 Syntax:
(target-type) value
 Example:
public static void main(String[] args) {
int a = 5;
// × this is not correct
byte b = a;
// √ this is correct
byte b = (byte)a ;
}
Operator
Perform definite operation
Operators
1. Arithmetic Operators
2. Relational Operators
3. Bitwise Operators
4. Logical Operators
5. Assignment Operators
6. Conditional / Ternary Operator
7. Instance of Operator
Operators
 An operator is a symbol to perform specific mathematical or logical
functions.
 We use operators in maths to perform certain operations, e.g. +, -, *, /,
etc.
 Unary operators (++, --) take one operand, Binary operators (+, -, *, /)
Sr.
take two operands. Operator Examples
1 Arithmetic Operators +, -, *, /, %
 Programming languages are rich in operators which can be divided in
2 Relational Operators <, <=, >, >=, ==, !
following categories, =
3 Logical Operators &&, ||, !
4 Assignment Operators =, +=, -=, *=, /=
5 Increment and Decrement ++, --
Operators
6 Conditional Operator ?:
7 Bitwise Operators &, |, ^, <<, >>
Arithmetic Operators
 An arithmetic operator performs basic mathematical calculations such as
addition, subtraction, multiplication, division etc on numerical values
(constants and variables).

Operat Examp
Meaning Description
or le
+ Addition a+b Addition of a and b
- Subtraction a–b Subtraction of b from a
* Multiplication a*b Multiplication of a and b
/ Division a/b Division of a by b
Modulo division-
% a%b Modulo of a by b
remainder
Relational Operators
 A relational operators are used to compare two values.
 They check the relationship between two operands, if the relation is true,
it returns 1; if the relation is false, it returns value 0.
 Relational expressions are used in decision statements such as if, for,
while, etc…
Operator Meaning Example Description
< is less than a<b a is less than b
<= is less than or equal to a <= b a is less than or equal to b
> is greater than a>b a is greater than b
>= is greater than or equal a >= b a is greater than or equal to b
to
== is equal to a == b a is equal to b
!= is not equal to a != b a is not equal to b
Relational Operators Note : A = 10 &
B = 20

Operato
Description Example
r
== Equals (A == B) is not
true.
!= Not Equals (A != B) is true.

> Greater than (A > B) is not


true.
< Less than (A < B) is true.

>= Greater than equals (A >= B) is not


true.
<= Less than equals (A <= B) is true.
Bitwise Operators Note : A = 60 &
B = 13
Operat
Description Example
or
A & B = 12 which is 0000
& Binary AND Operator
1100
A | B = 61 which is 0011
| Binary OR Operator
1101
A ^ B = 49 which is 0011
^ Binary XOR Operator
0001
~A = -61 which is 1100
Binary Ones Complement 0011 in 2's complement
~
Operator form due to a signed
binary number.
A << 2 = 240 which is
<< Binary Left Shift Operator
1111 0000
A >> 2 = 15 which is
>> Binary Right Shift Operator.
1111
A >>>2 = 15 which is
>>> Shift right zero fill operator.
0000 1111
Logical Operators
 Logical operators are decision making operators.
 They are used to combine two expressions and make decisions.
 An expression containing logical operator returns either 0 or 1 depending
upon whether expression results false or true.
Opera Example (Let’s assume c=5
Meaning
tor and d=2)
Logical AND. True only if all operands expression ((c==5) && (d>5))
&&
are true equals to 0.
Logical OR. True only if either one expression ((c==5) || (d>5))
||
operand is true equals to 1.
a if the
Logical NOT. True only b operand
a && bis a || b
! expression !(c==5) equals to 0.
false 0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1
Logical Operators Note : A = true & B =
false

Operator Description Example

&& Logical AND operator (A && B) is false.

|| Called Logical OR Operator (A || B) is true.

! Called Logical NOT Operator !(A && B) is true.


Assignment Operators
 Assignment operators are used to assign a new value to the variable.
 The left side operand of the assignment operator is a variable and right
side operand of the assignment operator is a value or a result of an
expression.
 Meaning of = in Maths and Programming is different.
 Value of LHS & RHS is always same in Math.
 In programming,
Opera Meaning value of RHS is assigned to the LHS
tor
= Assigns value of right side to left side. Suppose a=5 and b=10. a=b
means now value of a is 10.
+= a += 1 is same as a = a + 1
-= a -= 5 is same as a = a – 5
*= a *= b is same as a = a * b Shorthand Assignment Operators
/= a /= c is same as a = a / c
%= a %= 10 is same as a = a % 10
Assignment Operators
Operat
Description Example
or
C = A + B will assign
= Simple assignment operator
value of A + B into C
C += A is equivalent to C
+= Add AND assignment operator
=C+A
Subtract AND assignment C -= A is equivalent to C =
-=
operator C-A
C *= A is equivalent to C
*= Multiply AND assignment operator
=C*A
C /= A is equivalent to C =
/= Divide AND assignment operator
C/A
C %= A is equivalent to C
%= Modulus AND assignment operator
=C%A
Left shift AND assignment C <<= 2 is same as C = C
<<=
operator << 2
Right shift AND assignment C >>= 2 is same as C = C
>>=
operator >> 2
C &= 2 is same as C = C &
&= Bitwise AND assignment operator
Increment / Decrement Operators
 Increment and decrement operators are unary operators that add or
subtract one, to or from their operand.
 the increment operator ++ increases the value of a variable by 1, e.g. a+
+ means a=a+1
 the decrement operator -- decreases the value of a variable by 1. e.g. a––
means a=a–1
 If ++ operator is used as a prefix (++a) then the value of a is
incremented by 1 first then it returns the value.
 IfExpressi
++ operator is used as a postfix (a++) Expression
then the value of a is(Let’s
Evaluation returned
say
Evaluation (Let’s say a=10, c=15)
firstonthen it increments value of a by 1. a=10, c=15)
b = --a + c++
Value of b would be 10 and value of a b = 24
b = a++
would be 11. b = a++ + +
+c b = 26
b = ++a Value of b & a would be 11.
b = ++a - + b = –5
Value of b would be 10 and value of a +c
b = a--
would be 9.
b = --a Value of b & a would be 9.
Conditional Operator (Ternary)
 Conditional Operator ( ? : )
 Syntax:
variable x = (expression) ? value if true : value if false
 Example:
b = (a == 1) ? 20 : 30;
Operator Precedence & Associativity
 How does java evaluate 1 + 10 * 9 ?
 (1 + 10 ) * 9 = 99 OR 1 + (10 * 9) = 91
 To get the correct answer for the given problem Java came up with
Operator precedence. ( multiplication have higher precedence than
addition so correct answer will be 91 in this case)
 For Operator, associativity means that when the same operator appears in
a row, then to which direction the expression will be evaluated.
 How does java evaluate 1 * 2 + 3 * 4 / 5 ???
2 + 12 / 5
2 + 2.4
4.4
Precedence of Java Operators
Category Operator Associativity
Postfix () [] . (dot operator) Left to right
Unary ++ - - ! ~ Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift >> >>> << Left to right
Relational > >= < <= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %= >>= <<= &= Right to left
^= |=
Comma , Left to right
Operators
Precedence &
Associativity
Priority matters!
Operators Precedence & Associativity
 Operators Precedence and Associativity are two
characteristics of operators that determine the
evaluation order of sub-expressions in absence of
brackets.
 Operator precedence determines which operation is
performed first in an expression with more than one
operators with different precedence.
 a=10 + 20 * 30 is calculated as 10 + (20 * 30) and
not as (10 + 20) * 30 so answer is 610.
 Operators Associativity is used when two operators of
same precedence appear in an expression. Associativity
can be either Left to Right (L to R) or Right to Left (R to
L).
 a=100 / 10 * 10
 If Left to Right means (100 / 10) * 10 then answer
is 100
Operators Precedence and Associativity
1) Associativity is only used when there are two or more operators of
same precedence.
2) All operators
Priori Opera
ty tor
Description with the same precedence have
Associati
vity
Priori
ty
same Description
Operator associativity Associati
vity
() left-to- is equal to/is not equal left-to-
Parentheses (function call) 7 == != right
[] right to
Brackets (array subscript)
. left-to-
1 Member selection via object name 8 & Bitwise AND
-> right
Member selection via pointer
a++
Postfix increment/decrement left-to-
a— 9 ^ Bitwise exclusive OR
right
Prefix increment/decrement right-to-
++a left left-to-
Unary plus/minus 10 | Bitwise OR
—a right
Logical negation/bitwise complement
+–
Cast (convert value to temporary left-to-
!~ 11 && Logical AND
2 value of type) right
(type)
De-reference
* left-to-
Address (of operand) 12 || Logical OR
& right
Determine size in bytes on this
sizeof right-to-
implementation 13 ?: Ternary conditional
left
left-to-
3 * / % Multiplication/division/modulus = Assignment right-to-
right
+= -= *= /= Shorthand Assignments left
left-to-
4 + – Addition/subtraction %= &= Bitwise
right
14 ^= |= exclusive/inclusive
Exercise
Sr. Exercise Answer
1. int i=1; 4
i=2+2*i++;
2. int a=2,b=7,c=10; 0
c=a==b;
3. int a=2,b=7,c=10; 1
c=a!=b;
4. int a=100 + 200 / 10 - 3 * 10 90
5. int a = 2, b = 6, c = 12, d; 14
d = a * b + c / b;
printf("The value of d = %d ", d);
Thank You

You might also like