0% found this document useful (0 votes)
2 views24 pages

Unit-1 Introduction To Java by Prof. Meet Sanghavi

The document provides an introduction to Java programming, covering its features, environment components, and basic syntax. It includes details on data types, operators, control statements, and loops, along with practical examples. Additionally, it outlines the steps to run a Java program and includes a practical exercise for calculating and displaying student results.

Uploaded by

rajvekariya555
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)
2 views24 pages

Unit-1 Introduction To Java by Prof. Meet Sanghavi

The document provides an introduction to Java programming, covering its features, environment components, and basic syntax. It includes details on data types, operators, control statements, and loops, along with practical examples. Additionally, it outlines the steps to run a Java program and includes a practical exercise for calculating and displaying student results.

Uploaded by

rajvekariya555
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/ 24

Object Oriented Programming using Java

Unit-I Introduction to Java


INDEX
Unit-I Introduction to Java:
[Weightage=25% approx., Lectures=7, Practicals=14]:
Introduction to Java: Overview of Java programming language,
Features of Java: Platform independence, Object Oriented, etc.,Java
Development Kit (JDK),Java Runtime Environment (JRE),and Java
Virtual Machine(JVM).

Basics of Java: Java syntax and structure, Data types and variables,
Operators: arithmetic, relational, logical, bitwise, Control statements:
Conditional statements (if, switch) and loops (for, while, do-while).

X ---------------- X

Page 1 of 24
❖​Introduction to Java:

Java is a high-level, class-based, object-oriented programming


language developed by Sun Microsystems in 1995. It is known for its
write once, run anywhere philosophy, meaning Java code can run on any
device that has the Java Virtual Machine (JVM).

❖​Overview of Java:

🔹 Key Points:
●​ Developed by James Gosling.​

●​ Syntax is similar to C/C++.​

●​ Designed for web, mobile, desktop, and enterprise


applications.​

●​ Used in Banking, E-commerce, Android, Big Data, etc.

📝 Exam Point:
Java was initially called Oak. Later it was renamed Java, inspired
by Java coffee.

Page 2 of 24
❖​Features of Java:

Feature Description
Platform Java code runs on any device having JVM.
Independent
Object-Oriented Uses concepts like class, object, inheritance,
encapsulation, etc.
Simple Syntax is similar to C++, easy to learn.
Secure Provides bytecode verification and no explicit
pointer use.
Robust Strong memory management and exception
handling.
Multithreaded Supports concurrent execution of two or more
threads.
Portable Bytecode can run on multiple platforms.

📝 Exam Point:
JVM makes the Java platform independent by executing the same
bytecode on different machines.

Page 3 of 24
❖​Java Environment Components:

Component Description
JDK ​ Includes compiler (javac), debugger, tools,
(Java Development Kit) JRE. Used to develop Java programs.
JRE (Java Runtime Provides libraries and JVM required to run
Environment) Java programs.
JVM (Java Virtual Converts bytecode to machine code.
Machine) Responsible for Java program execution.

📝 Exam Point:
JDK = JRE + Development Tools

Page 4 of 24
❖​Basics of Java:

🔹 Basic Java Program:


public class Hello {

public static void main(String[] args) {

System.out.println("Hello, Java!");

Element Meaning

public class Hello Class declaration

main() Entry point of program

System.out.println() Used to print output to console

📝 Exam Point:
Java program execution starts from the main() method.

Page 5 of 24
❖​Data Types and Variables:
🔹 Primitive Data Types:
Data Type Size Example

int 4 bytes int a = 10;

float 4 bytes float f = 3.14f;

char 2 bytes char c = 'A';

boolean 1 bit boolean x = true;

double 8 bytes double d = 9.8;

long 8 bytes long l = 123456L;

short 2 bytes short s = 1000;

byte 1 byte byte b = 100;

🔹 Non-Primitive Data Types:


Type Description Example

String Sequence of characters. String name="Java";

Collection of fixed number of similar int[]marks= {80,90};


Array
data types.
A template or blueprint for creating Student s=new
Class objects. Student();

Interface A reference type used for abstraction. Runnable r=new Task();

Root class of all Java classes. Object obj=new


Object
Object();

Page 6 of 24
🔹 Key Differences:
Primitive Type Non-Primitive Type
Stores simple values Stores reference to object
Fast and memory efficient Slower, more flexible
Not objects Are objects

Examples: int, float Examples: String, Array

🔹 Variables:
int roll = 101;

String name = "Meet Sanghavi";

📝 Exam Point:
Variable names are case-sensitive in Java. Total and total are different.

Page 7 of 24
❖​Operators in Java:

1. Arithmetic Operators

2. Relational (Comparison) Operators

3. Logical Operators

4. Bitwise Operators

🔹1. Arithmetic Operators:


Operator Description Example Result

+ Addition 10 + 5 15

- Subtraction 10 - 5 5

* Multiplication 10 * 5 50

/ Division 10 / 5 2

% Modulus (remainder) 10 % 3 1

🔹 Example:

int a = 10, b = 3;

System.out.println(a + b); // 13

System.out.println(a % b); // 1

📝 Exam Point: % operator is commonly asked to find remainder.


Page 8 of 24
🔹 2. Relational (Comparison) Operators
Operator Description Example Result

== Equal to 5 == 5 true

!= Not equal to 5 != 3 true

> Greater than 10 > 5 true

< Less than 3 < 5 true

>= Greater than or equal 5 >= 5 true

<= Less than or equal 4 <= 5 true

🔹 Example:

int x = 10, y = 20;

System.out.println(x > y); // false

Page 9 of 24
🔹 3. Logical Operators
Operator Description Example Result

&& Logical AND true && false false

` ` Logical OR

! Logical NOT !(true) false

🔹 Example:

int age = 25;

boolean isAdult = (age >= 18) && (age <= 60);

System.out.println(isAdult); // true

🔹 4. Bitwise Operators
Operator Description Example Result (Binary)

& Bitwise AND 5 & 3 1 (0101 & 0011)

` ` Bitwise OR `5

^ Bitwise XOR 5 ^ 3 6

~ Bitwise ~5 -6
Complement

🔹 Example:

System.out.println(5 & 3); // 1

System.out.println(5 | 3); // 7

Page 10 of 24
❖​Control Statements: Conditional Statements (if, switch)

🔹 What are Control Statements?


Control statements in Java control the flow of execution in a program.

They allow the program to:

●​ Make decisions (like if, switch)​

●​ Repeat actions (like for, while, do-while)​

●​ Jump to other parts of the program (break, continue, return)

🔹 Types of Control Statements


Java control statements are mainly divided into three categories:

1.​ Decision-making statements​

2.​ Looping (Iteration) statements​

3.​ Jump statements​

Page 11 of 24
🔹 1. Decision-Making Statements
Used to execute code conditionally, based on boolean expressions.

1) if Statement

Executes a block of code only if the condition is true.

Syntax:

if (condition) {
// code to execute if true

🔹
}
Example:

int age = 20;


if (age >= 18) {
System.out.println("Eligible to vote");
}

2) if-else Statement

Executes one block if true, and another block if false.

Syntax:

if (condition) {
// code if true
} else {
// code if false
}

Page 12 of 24
🔹 Example:

int marks = 35;


if (marks >= 40) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}

3) if-else-if Ladder

Used to check multiple conditions.

Syntax:

if (condition1) {
// code
} else if (condition2) {
// code
} else {
// default code

🔹
}
Example:

int marks = 85;


if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 75) {
System.out.println("Grade B");
} else {
System.out.println("Grade C");
}

Page 13 of 24
4) switch Statement

Checks a variable for equality against multiple values (cases).

Syntax:

switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// default code

🔹
}
Example:

int day = 2;
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Other day");
}

Page 14 of 24
❖​loops (for, while, do-while):
1) for Loop

Used when the number of iterations is known.

Syntax:

for (initialization; condition; update) {


// code to repeat

🔹
}
Example:

for (int i = 1; i <= 5; i++) {


System.out.println("Java");
}

2) while Loop

Used when the number of iterations is unknown.

Syntax:

while (condition) {
// code

🔹
}
Example:

int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}

Page 15 of 24
3) do-while Loop

Executes code at least once, then checks condition.

Syntax:

do {
// code
} while (condition);

🔹 Example:

int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);

📝 Exam Point: do-while loop runs at least once, even if condition is


false.

Page 16 of 24
🔹 3. Jump Statements
Used to alter the normal flow of control.

1) break Statement

●​ Exits the loop or switch block immediately.

Syntax:

for (int i = 1; i <= 10; i++) {


if (i == 5) break;
System.out.println(i); // prints 1 to 4
}

2) continue Statement

●​ Skips current iteration and goes to the next loop cycle.

Syntax:

for (int i = 1; i <= 5; i++) {


if (i == 3) continue;
System.out.println(i); // skips 3
}

3) return Statement

●​ Exits from a method and optionally returns a value.

Syntax:

public int add(int a, int b) {


return a + b;
}

Page 17 of 24
❖​Header files and their usage:
Definition:
●​ In Java, header files (as in C/C++) do not exist. Instead, Java uses
import statements to include built-in classes and packages.
●​ These import statements allow you to use predefined classes and
functions.

Java Packages: (Classe & Usage):

Package Class Used Usage

java.lang String, Math Basic classes like Strings, Math, etc.


Automatically imported.
java.util Scanner, Utility classes like data structures,
ArrayList date/time, input reading
java.io File, For input/output operations (e.g.,
IOException reading/writing files)
java.net Socket, URL Network communication like web,
sockets, etc.
javax.swing JFrame, GUI components for desktop
JButton applications
java.sql Connection, Database connectivity (JDBC)
DriverManager
java.awt Color, Graphics Abstract Window Toolkit for GUI
applications

Page 18 of 24
🔹 Example:

import java.util.Scanner; // Scanner class is used to take input from the user

public class InputExample {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // Scanner object created
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hello, " + name);
}
}

🔹public static void main(String[] args) in Java:


Definition:

This is the starting point of execution for any standalone Java application.
Each keyword has a specific meaning:

Keyword Meaning

public Access modifier – allows JVM to access this method from


anywhere.
static Method belongs to the class, not an instance – JVM can call
it without creating an object.
void Return type – this method does not return any value.
main Name of the method – predefined name used by JVM as
entry point.
String[] args Parameter that can store command-line arguments as strings.

Page 19 of 24
🔹System.out.println("Hello, Java!"); in Java
Definition:
This line prints a message to the console. It is used for output display.

Component Meaning
System A built-in class that belongs to java.lang package
out A static object of PrintStream class connected to the console
println() A method to print a message and move to a new line

🔹 Example:

public class HelloExample {


public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}

🔹Scanner sc = new Scanner (System.in); in Java


Definition:

This line creates a Scanner object for reading user input from the console
(System.in).

Component Meaning
Scanner A class from java.util package used for input
sc Object name (can be anything)
new Creates a Scanner to read from the console
Scanner(System.in) (keyboard)

Page 20 of 24
🔹 Example:

import java.util.Scanner;

public class InputExample {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println("Your age is: " + age);
}
}

📝 Steps to Run Java Program Using Notepad++ & CMD:


1.​ Save the code in Notepad++ with filename: (.java)​

2.​ Open CMD → go to folder location using cd command.​

3.​ Compile: javac (FileName).java​

4.​ Run: java (FileName)

Page 21 of 24
🔹 Example: Cover All concepts from this unit only.:

PRACTICAL TITLE:
"Develop a Java Program to Calculate and Display Student Result using
Control Statements."

// File: StudentResult.java

import java.util.Scanner;
class StudentResult {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Input student data


System.out.print("Enter Student Name: ");
String name = sc.nextLine();

System.out.print("Enter Marks for Subject 1: ");


int m1 = sc.nextInt();

System.out.print("Enter Marks for Subject 2: ");


int m2 = sc.nextInt();

System.out.print("Enter Marks for Subject 3: ");


int m3 = sc.nextInt();

// Calculate total and percentage


int total = m1 + m2 + m3;
float percentage = total / 3.0f;

Page 22 of 24
// Display result
System.out.println("\n--- Student Report ---");
System.out.println("Name : " + name);
System.out.println("Total Marks: " + total);
System.out.println("Percentage : " + percentage + "%");

// if-else condition for grade


if (percentage >= 75) {
System.out.println("Grade: Distinction");
} else if (percentage >= 60) {
System.out.println("Grade: First Class");
} else if (percentage >= 40) {
System.out.println("Grade: Pass");
} else {
System.out.println("Grade: Fail");
}

// switch statement with loops


System.out.println("\nChoose Loop Option to Display Motivation:");
System.out.println("1. for loop");
System.out.println("2. while loop");
System.out.println("3. do-while loop");

int choice = sc.nextInt();


switch (choice) {
case 1:
for (int i = 1; i <= 3; i++) {
System.out.println("Keep learning Java!");
}
break;

Page 23 of 24
case 2:
int i = 1;
while (i <= 3) {
System.out.println("Practice makes perfect!");
i++;
}
break;

case 3:
int j = 1;
do {
System.out.println("Never give up!");
j++;
} while (j <= 3);
break;
default:
System.out.println("Invalid option selected.");
}
sc.close();
}
}

X ---------------- X
Page 24 of 24

You might also like