0% found this document useful (0 votes)
10 views61 pages

Introduction To Java Programming

The document outlines a course on Object Oriented Programming (OOP) using Java, emphasizing its importance in software design and real-world applications. It details course outcomes, Java's features, and its applications in various industries, highlighting its platform independence and strong community support. The document also includes a syllabus overview, teaching scheme, and examples of Java's use in practical scenarios.

Uploaded by

crazyabc801
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)
10 views61 pages

Introduction To Java Programming

The document outlines a course on Object Oriented Programming (OOP) using Java, emphasizing its importance in software design and real-world applications. It details course outcomes, Java's features, and its applications in various industries, highlighting its platform independence and strong community support. The document also includes a syllabus overview, teaching scheme, and examples of Java's use in practical scenarios.

Uploaded by

crazyabc801
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

Object Oriented Programming

(BE04000231)

Prof. H. S. Sanghavi
Computer Engineering Department
Government Engineering College, Bhavnagar
About the subject
• Category of the Course : Professional Core Course
(forms the backbone of your engineering skillset. We will be using Java, a language famous for its platform
independence and rich libraries. If you want to build Android apps, enterprise web systems, or big data tools
later in your career, this is where it starts)

• Why Object Oriented ?? – to move from Procedures to Objects


(It helps us design software that mimics the real world. We stop thinking about just 'functions' and start
thinking about 'Objects' that interact with each other. This makes our code modular, reusable, and easier to
maintain.")
Course Outcomes
Why we are learning Java Programming Language ?
 Platform Independence
 write once, run anywhere (WORA) Foundation for Learning Other Languages
OOP Java is the easiest, scoring and my favorite subject

 ideal for cross-platform applications. Teaches you strong programming fundamentals


(OOP, data structures, multithreading, etc.)
 Wide Industry Use
 Strong Object-Oriented Programming (OOP) Foundation
 Android App Development
 one of the primary languages for Android app development
 Rich API and Libraries
 vast standard library (Java API) and strong ecosystem support (e.g., Spring, Hibernate, Maven)
 Strong Community Support
 High Demand in Job Market
 Excellent Performance and Scalability
 offers good performance through Just-In-Time (JIT) compilation and is suitable for scalable, high-
performance applications
 Security Features
4
Real Time Applications of Java
Sr.
Application Area Description / Use Cases
No.
OOP Java is the easiest, scoring and my favorite subject

1 Android Mobile Applications Primary language for Android apps; used in WhatsApp, Spotify, Signal

Backend systems using Spring, Hibernate, Java EE; used in banking, e-


2 Enterprise Web Applications
commerce, govt portals

3 Financial Services Used in high-frequency trading, online banking, credit systems

4 Big Data Technologies Supports tools like Apache Hadoop, Spark, Kafka

Used in simulations and calculations; libraries include Apache Commons Math,


5 Scientific Applications
Java 3D

6 Cloud-Based Applications Cloud-native apps using Spring Boot; runs on AWS, Azure, Google Cloud

7 Embedded Systems / IoT Java ME used in smartcards, set-top boxes, IoT sensors

8 Games and Multimedia Applications 2D/3D games development; example: Minecraft

9 Desktop GUI Applications Java Swing/JavaFX for apps like NetBeans, Eclipse, Jitsi

Used for live monitoring and trading platforms due to concurrency and low
10 Real-Time Trading & Analytics Dashboards
latency
5
Real-World Examples (Why this matters)
Here we connect the syllabus topics to actual problems we will solve in the labs.
OOP Java is the easiest, scoring and my favorite subject

Example A: The Banking System


• Concept: Encapsulation & Exception Handling "Imagine we are building software for a bank.
• The Object: A BankAccount.
• The Data: It holds private data like your balance.
• The Rules: We can't let just anyone change the balance directly. They must use a method like withdraw().
• The Safety: What if you try to withdraw ₹5,000 but only have ₹1,000? In this course, you will learn
Exception Handling to create custom errors like NotSufficientFundException. You will actually code this in
Practical Set 6!"
• The Benefit: You don't rewrite code for wheels; you inherit it. This is the power of Inheritance, which we
cover in Unit 4."

6
Real-World Examples (Why this matters)
Example B: The Shopping Mall

OOP Java is the easiest, scoring and my favorite subject
Concept: Polymorphism "Think of a billing counter at a mall.
• The Scenario: You have Regular customers and Privileged customers.
• The Code: You don't want to write completely different code for every customer type. You want one
method, generateBill(), that behaves differently depending on who the customer is.
• The Tech: This is called Polymorphism and Method Overloading. We will build a billing system that
automatically applies flat discounts for privileged members and percentage discounts for festive offers.“

Example C: Tesla vs. Maruti


• Concept: Inheritance "All cars share common features: wheels, speed, steering.
• The Hierarchy: We can create a base class called Vehicle.
• The Specialist: Then we create a subclass called ElectricCar. It inherits everything from Vehicle but adds
specific features like batteryCapacity and chargingTime.
• The Benefit: You don't rewrite code for wheels; you inherit it. This is the power of Inheritance, which we
cover in Unit 4."

7
Java vs Other Programming Languages

OOP Java is the easiest, scoring and my favorite subject


Feature Java C/C++ Python

Platform Cross-platform Platform-dependent Cross-platform

Typing Statically typed Statically typed Dynamically typed

Memory Automatic GC Manual Automatic

Performance Good High Moderate

one where variable types You don’t declare the


are known at compile type—it’s figured out at
time runtime

8
Teaching and Examination Scheme

OOP Java is the easiest, scoring and my favorite subject

Teaching Scheme Credits Examination Marks


Total
Theory Marks Practical Marks
L T P C Marks
ESE (E) PA (M) ESE (V) PBL(I) PA (I)
3 0 2 4 70 30 50 30 20 200

9
OOP Java is the easiest, scoring and my favorite subject

Syllabus
Course Roadmap (Syllabus Walkthrough)

OOP Java is the easiest, scoring and my favorite subject

11
In-detail Syllabus

OOP Java is the easiest, scoring and my favorite subject

12
In-detail Syllabus

OOP Java is the easiest, scoring and my favorite subject

13
Reference Books

OOP Java is the easiest, scoring and my favorite subject

14
OOP Java is the easiest, scoring and my favorite subject

Introduction
Welcome to the world of Programming
What is Language?
 A language is a system of communication used by a particular country or community.
OOP Java is the easiest, scoring and my favorite subject

 It is a structured system of communication used by humans, based on speech and gesture


(spoken language), sign or often writing.
 Both person should understand each other’s language.

Hello! કેમ છો!


你好 મજામ ાં

16
Programming Language
 A programming language is a computer language that is used by programmers (developers)
to communicate with computers. OOP Java is the easiest, scoring and my favorite subject

 It is a set of instructions written in a specific language to perform a specific task.


 It allow us to give instructions to a computer in a language the computer understands.
 5000+ programming languages are there, notably used are approximate 250.
 E.g. C, C++, C#, Java, Python, PHP, Pascal, etc.

17
Types of Programming Languages
1. Low-level programming language
 It is a machine-dependent (hardware specific) programming language.
OOP Java is the easiest, scoring and my favorite subject

 It consists of a set of instructions that are either in the binary form (0 or 1) or in a symbolic
and human-understandable mnemonics (ADD, MOV, SUB).
 E.g. Machine level language, Assembly language, etc.
2. High-level programming language
 It is closer to human languages than machine-level languages.
 It is easy to read, write, and maintain as it is written in English like words.
 It allows to write the programs which are independent of a particular type of machine
(hardware).
 A compiler is required to translate a high-level language into a low-level language.
 E.g. Python, Java, JavaScript, PHP, C#, LISP, FORTRAN, etc.
3. Middle-level programming language
 Middle-level programming language lies between the low-level and high-level
programming language.
 E.g. C, C++, etc.

18
OOP Java is the easiest, scoring and my favorite subject

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
OOP Java is the easiest, scoring and my favorite subject

dependencies as possible.
 Java was originally developed by James Gosling at Sun Microsystems and released in 1995.
 Java was initially named as Oak language and renamed to JAVA in 1995.

Setup size 149 MB (Linux), 152 MB (Windows x64)


Download Link [Link]
Official Website [Link]
Integrated Development 1. Eclipse NetBeans
Environment 2. IntelliJ IDEA Community Edition
(IDE) 3. BlueJ

20
Features of JAVA
Simple: Java inherits C/C++ syntax and many object-oriented features of C++.
OOP Java is the easiest, scoring and my favorite subject

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.

21
Features of JAVA (Cont.)
Architectural Neutral: Java is platform independent which means that any application
OOP Java is the easiest, scoring and my favorite subject

written on one platform can be easily ported to another platform.

Interpreted: Java is compiled to bytecodes, which are interpreted by a Java run-time


environment.

High Performance: Java achieves high performance through the use of bytecode which
can be easily translated into native machine code. With the use of JIT (Just-In-Time)
compilers, Java enables high performance.

22
Features of JAVA (Cont.)

OOP Java is the easiest, scoring and my favorite subject


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.

23
Components of Java
 Java Virtual Machine (JVM)
JDK  Java Runtime Environment (JRE)
OOP Java is the easiest, scoring and my favorite subject

 Java Development Kit (JDK)

Compiler Java Packages


([Link]) (math, util, awt JVM
Java Application etc…)

Launcher
([Link]), Runtime
AppletViewer, Libraries
etc..)

Development tools JRE

24
Java Development Kit (JDK)
 JDK contains tools needed ,
 To develop the Java programs and
OOP Java is the easiest, scoring and my favorite subject

 JRE to run the programs.


 The tools include
 compiler ([Link]),
 Java application launcher ([Link]),
 Appletviewer, etc…
 Java application launcher ([Link]) opens a
JRE, loads the class, and invokes its main
method.

25
Java Runtime Environment (JRE)
 The JRE is required to run java applications.
OOP Java is the easiest, scoring and my favorite subject

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

26
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
OOP Java is the easiest, scoring and my favorite subject

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.
 JVM interprets the byte code into the machine code.
27
How Java become Platform Independent?
Source code .java file
OOP Java is the easiest, scoring and my favorite subject
(Program)

Compiler

Bytecode .class file

JVM JVM JVM


(Windows) (Linux) (Mac)

Machine Code Machine Code Machine Code

28
Java Interview Question
1. Difference between JRE and JVM?
OOP Java is the easiest, scoring and my favorite subject

2. Difference between interpreter and JIT compiler?


3. Why Java is platform independent?
4. What are Java bytecodes?
5. JVM vs. JRE vs. JDK

29
Hello World Java Program File must be saved as [Link]

public class HelloClass


Main method from where execution will start
OOP Java is the easiest, scoring and my favorite subject

{
public static void main(String[] args)
{ String must start with capital letter
[Link](“Welcome to Semester-5");
}
System must start with capital letter
}

 We have to save this in [Link] file as it has public class named HelloWorld.
 String and System are inbuilt Java Classes.
 Classes in java are always written in Camel case.

30
How to execute Java Program?
1. Save the program with the same name as the public class with .java extension.
OOP Java is the easiest, scoring and my favorite subject

31
How to execute Java Program?
2. Open command prompt (cmd) / terminal & navigate to desired directory / folder.
OOP Java is the easiest, scoring and my favorite subject

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

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

32
OOP Java is the easiest, scoring and my favorite subject

Tokens
Tokens
 The smallest individual unit of a language / program is known as a token.
OOP Java is the easiest, scoring and my favorite subject

 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.
34
Classification of Tokens
Sr. Token Description OOP Java is the easiest, scoring and my favorite subject
Examples

1 Keywords Predefined reserved words void, int, float, for, if

User-defined combination of alphanumeric characters.


2 Identifiers a, i, sum, 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!”

5 Special Symbols Symbols that have special meaning #, $, @, %, =, :, ;

6 Operators A symbol that performs operation on a value or a variable +, -, *, /

35
OOP Java is the easiest, scoring and my favorite subject

Identifiers
Identifiers
 They are used for class names, method names and variable names.
OOP Java is the easiest, scoring and my favorite subject

 An identifier may be any descriptive sequence of


 uppercase(A…Z) and lowercase(a..z) letters
 Numbers(0..9) Java is Case-Sensitive i.e.
 Underscore(_) and dollar-sign($) characters VALUE is different than value
 Examples for valid Identifiers,
 AvgTemp
 count
 a4
 $test
 this_is_ok
 Examples for invalid Identifiers,
 2count (Identifiers can not start with digit)
 High-temp (Identifiers can not contain dash)
 Ok/NotOK (Identifiers can not contains slash)
37
Identifier Name Valid or Invalid?

for Rajkot _Name If Roll Number


OOP Java is the easiest, scoring and my favorite subject
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;

38
OOP Java is the easiest, scoring and my favorite subject

Literals
Literals
 A constant value in Java is created by using a literal representation of it
OOP Java is the easiest, scoring and my favorite subject

Integer Floating point number Character String

 A literal can be used anywhere a value of its type is allowed.

40
Comments
 Single Line Comments : //
OOP Java is the easiest, scoring and my favorite subject

 Multi Line Comments : starts with /* and ends with */


 Documentation Comments
 used to produce an HTML file that documents your program
 begins with a /** and ends with a */

Separators
 most commonly used separator in Java is the semicolon

41
OOP Java is the easiest, scoring and my favorite subject

Java Keywords
Keywords
 Keywords
 combined with the syntax of the operators and separators, form the foundation of the
OOP Java is the easiest, scoring and my favorite subject

Java language.
 cannot be used as names for a variable, class, or method
 In addition to the keywords, Java reserves the following: true, false, and null. These are
values defined by Java.

43
Data Types

OOP Java is the easiest, scoring and my favorite subject

Java Datatypes

Primitive Non-primitive

Floating-point
Integers Characters Boolean Class
numbers

44
Primitive Data Types
Data Type Size Range Example
OOP Java is the easiest, scoring and my favorite subject

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 2,147,483,647 int a = 50000;
long 8 Bytes -9,223,372,036,854,775,808 to long a = 20;
9,223,372,036,854,775,807
float 4 Bytes 1.4e-045 to 3.4e+038 float a = 10.2f;

double 8 Bytes 4.9e-324 to 1.8e+308 double a = 10.2;

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


character)
boolean Not defined true or false boolean a = true;

45
Escape Sequences
 Escape sequences in general are used to signal an alternative interpretation of a series of
characters. OOP Java is the easiest, scoring and my favorite subject

 For example, if you want to put quotes within quotes you must use the escape sequence, \",
on the interior quotes.
[Link]("Good Morning \"World\"");

Escape Sequence Description


\’ Single quote
\” Double quote
\\ Backslash
\r Carriage return
\n New Line
\t Tab

46
Type Casting
 Assigning a value of one type to a variable of another type is known as Type Casting.
OOP Java is the easiest, scoring and my favorite subject

 In Java, type casting is classified into two types,


 Widening/Automatic Type Casting (Implicit)

 Narrowing Type Casting(Explicitly done)

47
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:
OOP Java is the easiest, scoring and my favorite subject

 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;
[Link](“a=“+a);
[Link](“b=“+b);
}

48
Casting Incompatible Types
 To create a conversion between two incompatible types, you must use a cast
OOP Java is the easiest, scoring and my favorite subject

 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;
[Link](“a=“+a);
[Link](“b=“+b);
// √ this is correct
byte b = (byte)a ;
[Link](“a=“+a);
[Link](“b=“+b);
}
49
OOP Java is the easiest, scoring and my favorite subject

Operator
Perform definite operation
Operators
1. Arithmetic Operators
OOP Java is the easiest, scoring and my favorite subject

2. Relational Operators
3. Bitwise Operators
4. Logical Operators
5. Assignment Operators
6. Conditional / Ternary Operator
7. Instance of Operator

51
Operators
 An operator is a symbol to perform specific mathematical or logical functions.
OOP Java is the easiest, scoring and my favorite subject

 We use operators in maths to perform certain operations, e.g. +, -, *, /, etc.


 Unary operators (++, --) take one operand, Binary operators (+, -, *, /) take two operands.
 Programming languages are rich in operators which can be divided in following categories,
Sr. Operator Examples
1 Arithmetic Operators +, -, *, /, %
2 Relational Operators <, <=, >, >=, ==, !=
3 Logical Operators &&, ||, !
4 Assignment Operators =, +=, -=, *=, /=
5 Increment and Decrement ++, --
Operators
6 Conditional Operator ?:
7 Bitwise Operators &, |, ^, <<, >>

52
Arithmetic Operators
 An arithmetic operator performs basic mathematical calculations such as addition,
subtraction, multiplication, division etc on numerical values (constants and variables).
OOP Java is the easiest, scoring and my favorite subject

Operator Meaning Example Description


+ 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- remainder a%b Modulo of a by b

53
Relational Operators
 A relational operators are used to compare two values.
OOP Java is the easiest, scoring and my favorite subject

 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 to a >= b a is greater than or equal to b
== is equal to a == b a is equal to b
!= is not equal to a != b a is not equal to b

54
Bitwise Operators Note : A = 60 & B = 13

OOP Java is the easiest, scoring and my favorite subject

Operator Description Example

& Binary AND Operator A & B = 12 which is 0000 1100

| Binary OR Operator A | B = 61 which is 0011 1101

^ Binary XOR Operator A ^ B = 49 which is 0011 0001


~A = -61 which is 1100 0011 in
~ Binary Ones Complement Operator 2's complement form due to a
signed binary number.
<< Binary Left Shift Operator A << 2 = 240 which is 1111 0000

>> Binary Right Shift Operator. A >> 2 = 15 which is 1111

>>> Shift right zero fill operator. A >>>2 = 15 which is 0000 1111

55
Logical Operators
 Logical operators are decision making operators.
OOP Java is the easiest, scoring and my favorite subject

 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.
Operato
Meaning Example (Let’s assume c=5 and d=2)
r
expression ((c==5) && (d>5)) equals to
&& Logical AND. True only if all operands are true
0.
Logical OR. True only if either one operand is
|| expression ((c==5) || (d>5)) equals to 1.
true
a b a && b a || b
! Logical NOT. True only if the operand is false expression !(c==5) equals to 0.
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1
56
Logical Operators Note : A = true & B = false

Operator Description Example


OOP Java is the easiest, scoring and my favorite subject

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

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

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

57
Assignment Operators
 Assignment operators are used to assign a new value to the variable.
OOP Java is the easiest, scoring and my favorite subject

 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, value of RHS is assigned to the LHS
Operator Meaning
= 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
58
Increment / Decrement Operators
 Increment and decrement operators are unary operators that add or subtract one, to or
from their operand. OOP Java is the easiest, scoring and my favorite subject

 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.
 If ++ operator is used as a postfix (a++) then the value of a is returned first then it increments
value of a by 1.
Expression
Expression Evaluation (Let’s say a=10, c=15)
b = a++
b = --a + c++ b = ??
b = ++a
b = a++ + ++c b = ??
b = a--
b = ++a - ++c b = ??
b = --a

59
Conditional Operator (Ternary)
 Conditional Operator ( ? : )
 Syntax:
OOP Java is the easiest, scoring and my favorite subject

variable x = (expression) ? value if true : value if false


 Example:
b = (a == 1) ? 20 : 30;

60
OOP Java is the easiest, scoring and my favorite subject

Thank You

You might also like