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

Day 3

Uploaded by

Shanmukh Varma
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)
6 views22 pages

Day 3

Uploaded by

Shanmukh Varma
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
You are on page 1/ 22

Why to learn Java

Dr Meera Alphy
Objectives
Able to understand..
• Object-Oriented Thinking and Java Basics
• Need for Object-Oriented Paradigm
• Summary of OOP Concepts (4 Pillars)
• Copying with Complexity
• Abstraction Mechanism
• A Way of Viewing the World: Agents, Responsibility, Messages
Object-Oriented Thinking and Java Basics
Object-Oriented Thinking is a mindset where the world is viewed in
terms of objects, rather than procedures. These objects represent real-
world entities, and you solve problems by defining how these objects
interact.
In Java, object-oriented thinking is reflected in:
• Everything being wrapped inside classes
• Using objects (instances of classes) to interact with data
• Using methods to define behavior
Need for Object-Oriented Paradigm
Why not Procedural?
• Procedural programming (like C) is fine for small tasks but struggles with:
• Complexity as systems grow
• Code reuse and maintenance
• Encapsulating real-world problems
OOP to the Rescue:
• Models real-world systems better
• Breaks large systems into manageable parts (objects)
• Promotes reusability, scalability, and maintainability
Summary of OOP Concepts (4 Pillars)

Concept Description
Hiding internal state; only exposing what’s
Encapsulation
necessary using access modifiers
Showing only relevant details; hiding complexity
Abstraction
(via interfaces, abstract classes)
Acquiring properties from parent class
Inheritance
(reusability)
One name, many forms (method
Polymorphism
overloading/overriding)
Coping with Complexity
• Object-Oriented Programming reduces system complexity through:
• Modular design: Each class handles one task
• Code reusability: Inheritance & composition
• Encapsulation: Changes in one class don’t affect others
• Clear interaction: Objects send messages (method calls) to each other
Abstraction Mechanism
Abstraction lets us ignore irrelevant details and focus on what’s
important.
Java provides:
• Abstract classes: Define structure, let subclasses complete
• Interfaces: Specify behavior without implementation
• This allows designing at a high level and implementing later,
promoting flexibility and decoupling.
A Way of Viewing the World: Agents,
Responsibility, Messages

Element Description
Objects that represent entities with state &
Agents
behavior
Each object has a role — what it knows and what
Responsibility
it can do
Objects communicate by sending messages
Messages
(method calls) to one another
Architecture
JVM
• The Java Virtual Machine (JVM) is a virtual engine that executes Java bytecode. It acts
as an interpreter and runtime environment that allows Java programs to run on any
device or operating system without modification.
How JVM Works:Java source code (.java) is written by the programmer.It is compiled by
the Java compiler (javac) into bytecode (.class).The JVM loads, verifies, and executes this
bytecode at runtime.
JVM converts bytecode → machine-specific code using either an interpreter
or JIT compiler.
Why JVM is Important:
• Platform independence: Same .class file runs on Windows, Linux, or Mac.
• Security: Runs code in a controlled environment.
• Portability: Helps Java follow the principle of "Write Once, Run Anywhere".
JRE – Java Runtime Environment

• JRE is the environment required to run Java programs. It includes the


JVM, core class libraries, and supporting files — but does not include
development tools like a compiler.
JRE Contains:
• JVM (Java Virtual Machine)
• Java Libraries (e.g., java.lang, java.util)
• Supporting configuration files
Use:
• For running Java applications (not for developing).
JDK – Java Development Kit
JDK is a software package used to develop Java applications. It contains
the JRE and additional development tools like compiler (javac), debugger,
and documentation tools.
JDK Contains:
• JRE which contains JVM + LibrariesCompiler (javac)
• Debugger (jdb)
• Documentation tools (javadoc)
• Other developer utilities
• Use:Used by developers to write, compile, debug, and run Java
programs.
JIT – Just-In-Time Compiler
• JIT is a part of the JVM that improves performance by converting
bytecode into native machine code at runtime — instead of
interpreting it line by line every time.
How It Works:
• JVM uses JIT to compile frequently used (hot) code into machine code.
• This compiled code is stored in memory and reused → faster execution.
Benefit:
• Speeds up program performance
• Reduces repeated interpretation of code
Term Full Form Purpose Used By Contains
Java Development To develop + run JRE + Compiler +
JDK Developers
Kit Java programs Tools
Java Runtime To run Java
JRE End users / JVM JVM + Libraries
Environment programs
Just-In-Time To optimize Converts bytecode
JIT Part of JVM
Compiler execution to machine code
Scanner Class
• The Scanner class is part of java.util package and is used to read input from various sources — most commonly
from keyboard input (System.in).
import java.util.Scanner;

public class UserInputExample {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // Create Scanner object
System.out.print("Enter your name: ");
String name = sc.nextLine(); // Read full line
System.out.print("Enter your age: ");
int age = sc.nextInt(); // Read integer
System.out.println("Hello " + name + ", you are " + age + " years old.");
}
}
Method Description Example
Reads a full line (including
nextLine() String name = sc.nextLine();
spaces)
next() Reads a single word (no spaces) String word = sc.next();
nextInt() Reads an integer int age = sc.nextInt();
Reads a double (decimal
nextDouble() double salary = sc.nextDouble();
number)
boolean value =
nextBoolean() Reads a boolean (true or false)
sc.nextBoolean();
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner myinput= new Scanner(System.in);
System.out.println("Enter the byte datatype value");
byte a=myinput.nextByte();
System.out.println("Enter the b value");
short b= myinput.nextShort();
System.out.println("Enter the c value");
int c=myinput.nextInt();
System.out.println("Enter d value");
long d=myinput.nextLong();
System.out.println("Enter the e value");
float e=myinput.nextFloat();
System.out.println("Enter the f value");
double f=myinput.nextDouble();
System.out.println("Enter the g value");
char g=myinput.next().charAt(0);
System.out.println("Enter the byte datatype value");
boolean h= myinput.nextBoolean();
String name="Meera",Regno="1481";
System.out.println("Hello!"+name+ " your regno is "+Regno);
System.out.println("a" +a+ " \nb is " +b+ " \tc is" +c+ " \td is" +d+"\n e is"+e+ "\nd is" +f+ "\ng is" +g+ "\nh is"+h);
Extra topics
Compiler (javac)
• What it does:
Converts .java source code into .class bytecode.
Example:Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!"); }}
Compile using:javac Hello.java
This creates Hello.class (bytecode), which you can run using:java Hello
Documentation Tool (javadoc)
DocuWhat it does:
• Generates HTML documentation from special comments in your Java code.

Example:
/**
* The Calculator class performs basic math operations.
*/
public class Calculator {
/**
* Adds two integers.
* @param a First number
* @param b Second number
* @return Sum of a and b
*/
public int add(int a, int b) {
return a + b;
}
}
Debugger (jdb)
What it does:
Helps debug Java code by setting breakpoints, inspecting variables, and tracking program flow.
Example:
// File: DebugDemo.java
public class DebugDemo {
public static void main(String[] args) {
int x = 10; int y = 0; int result = x / y;
// This will cause an exception
System.out.println("Result is: " + result); }}
Compile:javac DebugDemo.java
Run in debugger: jdb DebugDemo the jdb prompt, you can type:
stop at DebugDemo:5
run
This stops the program before the exception line.
HomeWork- (for writing )
1. Identify any real-world object (e.g., Bank Account, Student, Vehicle)
and write:
Its attributes (state)
Its responsibilities (methods/behavior)
Possible messages (method calls) it might receive
2. Imagine a Library Management System.
• Identify at least 3 objects (agents)
• Mention their responsibilities
• Draw a simple class diagram (optional)
HomeWork- (using scanner class)
3. Write a Java program to input two integers and print the greater one.
4. Calculate Area and Perimeter of Rectangle
Formula:
Area = length × breadth
Perimeter = 2 × (length + breadth)
5. Calculate BMI (Body Mass Index)
Formula:
BMI = weight / (height × height)
6. Swap Two Numbers using third variable
7. Calculate and display the speed.
Formula:
Speed = Distance / Time

You might also like