0% found this document useful (0 votes)
6 views47 pages

Lect-1C Intro To Java

Uploaded by

f20230634
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)
6 views47 pages

Lect-1C Intro To Java

Uploaded by

f20230634
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

Lecture 1C

ECOM F213
Object Oriented Programming
01/08/25

BITS, Pilani Goa campus


Prof. Anita Agrawal
2

Encapsulation
A mechanism of wrapping the data (variables) and code
acting on the data (methods) together as a single unit.

8/21/2025 8:00 AM Anita Agrawal ECOM F213


3

Encapsulation and classes


▪ Class contains: Data
Member/Instance
variables

Code Member methods

o Class encapsulates complexity

8/21/2025 8:00 AM Anita Agrawal ECOM F213


4

8/21/2025 8:00 AM Anita Agrawal ECOM F213


5

Inheritance
o A mechanism in which one class acquires the property of
another class.
o Example, a child inherits the traits of his/her
parents.

o With inheritance, the fields and methods of the existing


class can be reused.

o An inherited class is called a subclass of its parent class


or super class.

o It can be of different types- Single, Multiple, Multilevel,


Hierarchical and Hybrid

8/21/2025 8:00 AM Anita Agrawal ECOM F213


6

Real World example of


inheritance

8/21/2025 8:00 AM Anita Agrawal ECOM F213


Another example of Inheritance:
One class acquires properties of another class

Supports hierarchical classification

7 8/21/2025 8:00 AM Anita Agrawal ECOM F213


8

Real World Example in a


banking system

8/21/2025 8:00 AM Anita Agrawal ECOM F213


9

Polymorphism
▪ It is a OOP concept where one name can have many
forms.

▪ In a real world example, a person at a same time can


have different characteristic.

▪ Example- Calculate the Area-


• For Circle: Radius r
• For Rectangle: Length L and Width W
• For Sector: Radius r, Angle O.

8/21/2025 8:00 AM Anita Agrawal ECOM F213


Introduction
to Java
Topics:
▪ Anatomy of a Java Program
▪ History of Java
▪ Salient Features of Java
▪ Stages of Execution of Java Program
▪ JVM vs. JRE vs. JDK

10 8/21/2025 8:00 AM Anita Agrawal ECOM F213


11

Java Program

▪ A Java program is defined as a collection of objects


that communicate with each other to perform specific
tasks.

8/21/2025 8:00 AM Anita Agrawal ECOM F213


12

Anatomy of a Java Program


A. Reserved words / Keywords
B. Classes
C. Package
D. Modifiers
E. Statements
F. Blocks
G. Methods
H. The main method
I. Comments

8/21/2025 8:00 AM Anita Agrawal ECOM F213


13

A simple java program

//This comment contains only one line.

8/21/2025 8:00 AM Anita Agrawal ECOM F213


14

Breaking down the program


▪ “class" keyword is used to declare a new class.
▪ “Myfirstjavaprogram" is the identifier
▪ public static void main
▪ public:
» It is an example of an access specifier.
» Allows programmer to control the visibility of class
members
» Members may be accessed from outside the class in which
they are declared.

8/21/2025 8:00 AM Anita Agrawal ECOM F213


15

Breaking down the program


(Contd.)

 main() be declared as public since it will be called


by outside it’s class

8/21/2025 8:00 AM Anita Agrawal ECOM F213


Breaking down the program (Contd.)
▪ static:
» Allows main() to be called without creating an object of the
class.
» It is necessary since main() is called by JVM before any
objects are made.
▪ void:
» Defines the return type, in this case it is void.

16 8/21/2025 8:00 AM Anita Agrawal ECOM F213


main

» The system locates and runs the main method for a class
when you run a program.

» Other methods get executed when called by the main


method.

17 8/21/2025 8:00 AM Anita Agrawal ECOM F213


18

▪ String args[ ]

– Declares a variable (object) named args

– It is an array of String class instances

– args[ ] receives any command line arguments during


program execution

8/21/2025 8:00 AM Anita Agrawal ECOM F213


19

[Link]
▪ println: It is a method

▪ It is used by invoking a statement with a string argument.

▪ The string argument is enclosed within parentheses. In this


case, the argument is "This is my first java program!"

▪ You can call the same println method with a different


argument to print a different message.

8/21/2025 8:00 AM Anita Agrawal ECOM F213


20

Blocks
▪ A pair of braces in a program form a block that groups
components of a program.

8/21/2025 8:00 AM Anita Agrawal ECOM F213


21

Comments
//my first java program
class myfirstjavaprogram
{
public static void main(String args[])
{
[Link]("this is my first java
program");

}
}

// This is a single line comment


/* These are multiple
line comments */
8/21/2025 8:00 AM Anita Agrawal ECOM F213
22

Lexical constraints
▪ Whitespace – Java is a free-form language (indentation is not
necessary but desirable).
» As long as there is one whitespace character between each token –
Space, tab, and newline.

▪ Identifiers – Used for class, method, and variable names.

▪ Literals – A constant value in Java is called literal

▪ Comments – /* */ and //

8/21/2025 8:00 AM Anita Agrawal ECOM F213


Identifiers
▪ Combination of uppercase and lowercase letters,
and numbers
▪ Underscore_ and $ sign are allowed.
▪ Must begin with an alphabet, underscore or $
▪ Case-sensitive
▪ 3-15 characters
▪ Reserved keywords cannot be used

23 8/21/2025 8:00 AM Anita Agrawal ECOM F213


24

List of Separators

8/21/2025 8:00 AM Anita Agrawal ECOM F213


25

Reserved words/ Keywords in Java

8/21/2025 8:00 AM Anita Agrawal ECOM F213


26

Important points
▪ The name of the source file is very important.
▪ In previous example, the name of the file will be
[Link]
▪ The name of the class should match the name of file that holds
the program.
▪ A source file is officially called compilation unit.

8/21/2025 8:00 AM Anita Agrawal ECOM F213


27

▪ It is a text file that contains one or more class definitions.

▪ For Java compiler, source file must have .java extension.

▪ In Java, all code must reside inside a class

▪ Java is a case-sensitive programming language.

8/21/2025 8:00 AM Anita Agrawal ECOM F213


Example 2: If control statement
//if control statement
class if_eg
{
public static void main(String args[])
{
int n=10;
if(n%2== 0)
[Link]("The number is even");
else
[Link]("The number is odd");
}
}
28 8/21/2025 8:00 AM Anita Agrawal ECOM F213
29

Example 3: for control statement


//for statement
class for_eg
{
public static void main(String args[]
{
int x=10;
int i;
int sum=0;
for(i=1; i<=x; i++)
sum = sum+i;
[Link]("The sum is: " +sum);
}
}

8/21/2025 8:00 AM Anita Agrawal ECOM F213


30

History of Java

❑ Initially called ‘Oak’, but after a while (1995), came to be


known as ‘Java’, over coffee.

❑ Name of Coffee seed

8/21/2025 8:00 AM Anita Agrawal ECOM F213


31

Salient Features of Java


❑ Object-Oriented Programming language

❑ Structured in terms of classes, which group data with


operations on that data

❑ Can construct new classes by extending existing ones

8/21/2025 8:00 AM Anita Agrawal ECOM F213


32

Salient features of Java contd….

▪ Platform Independent:
Unlike many other programming languages including C
and C++, when compiled, it is not compiled into a code
which is platform specific, rather into “platform-
independent byte code”.

▪ It runs on a variety of platforms such as Windows, Mac OS,


and the various versions of Unix.

8/21/2025 8:00 AM Anita Agrawal ECOM F213


33

Salient Features of Java contd…

▪ Simple:
Easy to learn

▪ Secure:
Enables to develop virus-free, tamper-free systems.

8/21/2025 8:00 AM Anita Agrawal ECOM F213


34
Salient Features of Java
contd…
▪ Architecture-neutral:
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 it portable

8/21/2025 8:00 AM Anita Agrawal ECOM F213


35

Salient Features of Java


▪ Robust:
Eliminates error-prone situations by emphasizing mainly on
compile time error checking and runtime checking.

▪ Multithreaded:
Programs can be written that can perform many tasks
simultaneously.

▪ Interpreted:
Java code is translated on the fly to native machine instructions.
8/21/2025 8:00 AM Anita Agrawal ECOM F213
36

Salient Features of Java contd..


▪ High Performance:
With the use of Just-In-Time compilers, Java enables high
performance

▪ Dynamic: More dynamic than C or C++


Can carry extensive amount of run-time information that can be
used to verify and resolve accesses to objects on run-time.

8/21/2025 8:00 AM Anita Agrawal ECOM F213


▪ Follows 2-step process:
▪ Compilation
▪ Execution

37 8/21/2025 8:00 AM Anita Agrawal ECOM F213


38

ByteCode

▪ Output of Java Compiler is not executable code but is a


Bytecode.

▪ Bytecode is a highly optimized set of instructions to be executed


by Java Runtime System (Java Virtual Machine- JVM).

▪ JVM is the interpreter for Bytecode -> Converts into machine code
for execution.

8/21/2025 8:00 AM Anita Agrawal ECOM F213


39 8/21/2025 8:00 AM Anita Agrawal ECOM F213
Java program: Stages of execution

40 8/21/2025 8:00 AM Anita Agrawal ECOM F213


41

Bytecode- Contd.
▪ Advantages:
▫ It makes it easier to execute a program
▫ Only the JVM needs to be implemented

▪ Disadvantages:
▫ Slower performance

8/21/2025 8:00 AM Anita Agrawal ECOM F213


42

Requirements
▪ A text editor
▪ The Java Development Kit (JDK)
▫ Development Tools to develop your java programs

▪ The Java Runtime Environment (JRE) to execute your


java Programs
» Includes Java Virtual Machine (JVM)
» Libraries
8/21/2025 8:00 AM Anita Agrawal ECOM F213
43

JVM vs. JRE vs. JDK


▪ JVM – Java Virtual machine(JVM) – JVM is responsible for executing the
java bytecode line by line hence it is also known as interpreter – JVM allows
Java to be a "portable language" (write once, run anywhere)

▪ JRE – Java Runtime Environment – An installation package which provides


environment to only run (not develop) the java program onto your machine. JRE
includes JVM and libraries – JRE is used by them who only wants to run the
Java Programs i.e. end users of your system.

▪ JDK – Java Development Kit – The JDK is a superset of the JRE, and contains
everything that is in the JRE, plus tools such as the compilers and debuggers
necessary for developing programs and applications.
8/21/2025 8:00 AM Anita Agrawal ECOM F213
44 8/21/2025 8:00 AM Anita Agrawal ECOM F213
45

JDK editions
A. Java Standard Edition (J2SE)
▫ J2SE can be used to develop client-side standalone applications
or applets.

B. Java Enterprise Edition (J2EE)


▫ J2EE can be used to develop server-side applications such as
Java servlets and Java ServerPages.

C. Java Micro Edition (J2ME)


▫ J2ME can be used to develop applications for mobile devices
such as cell phones.

8/21/2025 8:00 AM Anita Agrawal ECOM F213


Next …………Data Types and Variables

46 8/21/2025 8:00 AM Anita Agrawal ECOM F213


THANKS!
Any questions?

47 8/21/2025 8:00 AM Anita Agrawal ECOM F213

You might also like