Unit-1 Introduction To Java by Prof. Meet Sanghavi
Unit-1 Introduction To Java by Prof. Meet Sanghavi
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:
❖Overview of Java:
🔹 Key Points:
● Developed by James Gosling.
📝 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:
System.out.println("Hello, Java!");
Element Meaning
📝 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
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
🔹 Variables:
int roll = 101;
📝 Exam Point:
Variable names are case-sensitive in Java. Total and total are different.
Page 7 of 24
❖Operators in Java:
1. Arithmetic Operators
3. Logical Operators
4. Bitwise Operators
+ 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
== Equal to 5 == 5 true
🔹 Example:
Page 9 of 24
🔹 3. Logical Operators
Operator Description Example Result
` ` Logical OR
🔹 Example:
System.out.println(isAdult); // true
🔹 4. Bitwise Operators
Operator Description Example Result (Binary)
` ` Bitwise OR `5
^ Bitwise XOR 5 ^ 3 6
~ Bitwise ~5 -6
Complement
🔹 Example:
System.out.println(5 | 3); // 7
Page 10 of 24
❖Control Statements: Conditional Statements (if, switch)
Page 11 of 24
🔹 1. Decision-Making Statements
Used to execute code conditionally, based on boolean expressions.
1) if Statement
Syntax:
if (condition) {
// code to execute if true
🔹
}
Example:
2) if-else Statement
Syntax:
if (condition) {
// code if true
} else {
// code if false
}
Page 12 of 24
🔹 Example:
3) if-else-if Ladder
Syntax:
if (condition1) {
// code
} else if (condition2) {
// code
} else {
// default code
🔹
}
Example:
Page 13 of 24
4) switch Statement
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
Syntax:
🔹
}
Example:
2) while Loop
Syntax:
while (condition) {
// code
🔹
}
Example:
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
Page 15 of 24
3) do-while Loop
Syntax:
do {
// code
} while (condition);
🔹 Example:
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
Page 16 of 24
🔹 3. Jump Statements
Used to alter the normal flow of control.
1) break Statement
Syntax:
2) continue Statement
Syntax:
3) return Statement
Syntax:
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.
Page 18 of 24
🔹 Example:
import java.util.Scanner; // Scanner class is used to take input from the user
This is the starting point of execution for any standalone Java application.
Each keyword has a specific meaning:
Keyword Meaning
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:
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;
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);
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 + "%");
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