Introduction to java
24 May 2025 09:03
Developed By : James gosling
When: 1995
purpose:
26 temp
Originally made for devices like TV, remotes etc
2023----------------> stack over flow-----> doubts
2025---------------->chatgpt/AI model ----------------> change in system
In 2000s ------> bank, ecomm, andriod app etc
1. High security
2. Stability
New Section 1 Page 1
2. Stability
3. Performance
Why we learn java in 2025 ?
----------------------------------------
1. 90% 500 fortune companies used in for there BE dev
2. Amazon, Uber, Netflix -----> Java
3.
Java still is best choice for
1. Android
2. Banking and Fintech
3. Big data - Hadoop (java based)
4. Minecract ----> java
5. Cloud, devops
Why we learn java in 2025 ?
1. Secure and scalable(agar main abhi 10 logo k liy use kr raha hu to main ise 100 logo k liy bhi kr sakta)
2. Most in demand --> high salary
3. BE systems ---> 90-95%
4. Morden tech --> kakfa, docker , kubernetes, AWS etc
5. Easy to learn
Domain Java role
Banking HDFC, ICICI core system
E-comm Amazon, Flipkart
Android 70% app
Payments Rozorpay, PhonePe backend in java
OTT platform Neflix
Gamming Minecraft
Space Tech NASA uses java for simulations
New Section 1 Page 2
Ok let's install java:
1. For Windows
Step 1: Download Java
1. Visit the java download page. :
https://www.oracle.com/in/java/technologies/downloads/
2. Select the latest Java Development Kit (JDK) [jdk 21] for Windows.
3. Download the installer (.exe file).
Step 2: Install Java
1. Run the downloaded .exe file.
2. Follow the installation wizard steps:
o Accept the license agreement.
o Choose an installation directory (default is usually fine).
3. Click Finish when the installation is complete.
Step 3: Set the PATH Environment Variable
1. Open the Start menu and search for Environment Variables.
2. Click Edit the system environment variables.
3. In the System Properties window, click the Environment Variables
button.
4. Under System Variables, find and select the Path variable, then
click Edit. If not available create one
5. Add the following to the list of paths:
o C:\Program Files\Java\jdk-<version>\bin (Replace <version>
with the installed JDK version number).
6. Click OK to save changes
Java -version
Step 1: Download Java
1. Visit the Java Download page.
2. Select the latest JDK for macOS.
3. Download the .dmg file.
New Section 1 Page 3
Step 2: Install Java
1. Open the downloaded .dmg file.
2. Follow the installation prompts:
o Drag the Java installer to the Applications folder.
3. Once the installation is complete, Java will be installed in
/Library/Java/JavaVirtualMachines/.
Step 3: Set the PATH Environment Variable
1. Open the Terminal application.
2. Edit the ~/.zshrc or ~/.bash_profile file (depending on your shell):
nano ~/.zshrc
3. Add the following line at the end of the file:
export PATH=$PATH:/Library/Java/JavaVirtualMachines/jdk-
<version>/Contents/Home/bin
(Replace <version> with the installed JDK version).
4. Save the file and reload it:
source ~/.zshrc
Step 4: Verify Installation
1. Open the Terminal.
2. Run the command:
java -version
If Java is installed correctly, it will display the insta
Do not use any IDE ---->
EditPlus
sublime -- for mac and linux
Internal working
New Section 1 Page 4
How to run java code ?
Compilation Java className output
Javac fileName.java bytecode
Java Code .class JVM
(.java) intermediate code
1. Create java file with extension .java
2. Name class and file also as per class name
3. Compile this using command Javac filename.java
4. Run this file java class name
5. output
class FirstProg
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
New Section 1 Page 5
Compile time
C/C++
Memory allocation
Difference between java and other lang
--------------------------------------------------------
1. C And C++ -----> static programming language And java Is dynamic programming lang
Exp: if any prog lang allow memory allocation for primitive data types at compile
time [static time] then the lang is called static prog lang
If any prog lang allow memory allocation for primitive data types at runtime, not
at compile time then this lang is called dynamic prog lang
Pre-processor is required in c and c++ not in java
-----------------------------------------------------------------
1. #include<stdio.h>
2. #include<math.h>
If we want to use predefined lin in C/C++ we have to include headers files
If we include header file then Pre-processor will load that specified header file
into the memory. This type of loading happened at compilation time and it's
called static loading
Lib : collection of something
Books
---------
lib : collections books
Programming
------------------
Lib: collection code
New Section 1 Page 6
#include<stdio.h>
Compile time
c/c++
Pre processing
.c
Compile
compilation
Assembler
Loader/ linker
If we want to use predefined lib in java, then we have to include package in java
Eg:
1. Import java.io*
2. Import java.util.*
3. Import java.sql.*
While executing java prog, JVM encounter any class or any interface from the specified package JVM will
load required class and interface in to the memory at RUNTIME, loading predefined lib at runtime is
called as Dynamic loading
New Section 1 Page 7
1. Ane se pahle sara saman le ayo --------------------> C/c++ --------------> pre-processing [loading at compile
time]--------> static prog lang
2. Bad me saman le jaye ------------------------------------> Java -------[loading at runtime]---------> Dynamic prog lang
Java
Pre processing
.c
Compile
compilation
Assembler
Loader/ linker
3. C and C++ platform dependent prog lang, but java is platform independent
C engine
execution
compilation
windows
output
windows
.exe
New Section 1 Page 8
.exe file contains directly executable code, it is generated as per OS
windows
Compilation
mac
Java code
.class
linux
Linux ---> tar
Windows ---> exe
mac -dmg
this java software is platform dependent
New Section 1 Page 9
.exe .class
1. Byte code, it is not executable code
1. Executable code 2. Platform independent
2. Platform dependent 3. More secure
3. Less secure
01
10
01
Byte code JVM
JVM
Javac
Eng
New Section 1 Page 10
JVM
JVM
Javac
Eng
JVM
Special char
ByteCode Machine Code
Not readable by human and not Generated by Bytecode by the JVM(using JIT compiler)
understand by machine specific to OS
it is understandable by JVM only Written in 1s and 0s
What is java ?
Java is compilation or interpreted lang ?
1. java is both compiled and interpreted lang ?
bytecode
.class
Java code(.java)
1. Bytecode is interpreted / executed by JVM at runtime
2. JIT[Just in time compiler] convert bytecode to machine code during
execution for better performance
Internal of code execution
-----------------------------------
Case 1: Without JIT
Use interpreter and each instruction converted line by line
Case 2: With JIT
Code is compiled once and resued
New Section 1 Page 11
interpreter
Threshold value = 5
JIT
Bahut time jayega
Reusable code
What is JVM ?
1. Load .class file
2. Verify bytecode
3. Heap stack memory
4. JIT
What is JRE(Java runtime environment)
New Section 1 Page 12
4. JIT
What is JRE(Java runtime environment)
JRE = JVM + runtime lib
1. It provide necessary lib to run java app
2. Does not provide compilation (javac)
JDK: java development kit
1. JDK = JRE + Development tools (javac, javadoc, jar)
2. Needed to develop & run java prog
3. Contains everything in JRE + compiler , debugger
What is JIT ?
part of jvm
Convert frequently used bytecode into machine code
New Section 1 Page 13
Note:
JVM read line by line (interpreter) but If you repeat a task again n again, JIT says , Bhai Main kr deta hu
and main ise yaad rakh lunga( cache ) then if you call the same code (hotcode) again then JIT will if you
mahine code from cache
Questions :
1. Is java is compiled lang or interpreted ?
2. What is JVM and what it does ?
3. Difference between JDK , JRE and JVM ?
4. How JIT improve performance ?
5. Can we run Java program with only JRE ? Yes if I have .class file
Java Program Structure :
class CAR
{
public static void main(String[] args)
{
System.out.println("Hello World!");
} Method
}
What is class ?
A class is blueprint/template (CAR) from which individual object are created
New Section 1 Page 14
•
• House
class
Object
Note : Why is everything in java inside class ?
Java is pure object oriented programming lang that's why all thing wrap inside object
What is object ?
real world instance of class
running of instance of class
Class -> blue for CAR
Object -> Actual CAR built using the blueprint -> Harrier ,Brezza
Method:
1. Block of code that perform specific task
2. Reusable code
Variables :
--------------
New Section 1 Page 15
Variable are used to hold/share the used program execution
We need to specify that which type of variable we need
To specify type of variable we will use "Data type"
Data Types
Primitive Non Primitive
String
Array
List
Non-Numeric Numeric
Stack
boolean
Integral
Character
true false char
Integer Floating-Point
long
byte short int float double
Bytes and bites
--------------------------
1 Byte => 8 bites
New Section 1 Page 16
Syntax :
<Data type> <Name> = value;
Int salary = 10000;
Terminal
System.out.println("Hello World!");
H.W
1. Storage for byte > start to end
New Section 1 Page 17
Data types default values
----------------------------------
byte age=25;
Identifiers
--------------
Are the names given to class, variable, method and interfaces
Rules for naming identifiers
-------------------------------------
1. Valid char : lower case, Upper case , digit, _ , $
2. Start with : letter , _ , $ [it cannot start with digit]
3. Case sensitive : car, CAR, CaR
4. Reserved keyword : (int, for, if else)
What is type casting ?
New Section 1 Page 18
Can we run .java file using only JRE ?
New Section 1 Page 19
Can we run .java file using only JRE ?
No
Which of the following contains java compiler
1. Jre
2. Jdk --
3. Jvm
Which is the output of javac
1. Machine code
2. Bytecode --
3. Native code
What is the default value of boolean ?
1. True
2. False --
Identify the valid java variable name
1. 1age
2. total_marks --
3. @marks
4. string
What will be the output ?
Double d = 9.8;
Int i=(int) d;
SOP(i) //----> 9
Which type of casting it is ?
long l = 134;
Int x=(int) l; //explicit
Which one of the following give compile time error
int x=12.4;
Int y = (int) 19.2; //
double d=11;
Float f = 17.10f;
Chapter 2 : Operators
------------------------------
New Section 1 Page 20
Operators:
1. Is a symbol which tells compiler to perform some operation/task.
Types
1. Arithmetic operator = +, -, *, / , %
2. Relational operator =
3. Logical Operator
4. Assignment Operator
5. Unary Operators
6. Ternary Operators
Increment & decrement(unary)
-----------------------------------------
++ ==>
-- ==>
int c= 6;
//mera 6 + 8 + 9 + 9 + 11 = 43
New Section 1 Page 21
//mera 6 + 8 + 9 + 9 + 11 = 43
int res = c++ + ++c + ++c + c++ + ++c;
System.out.println(res); //14
Operations : + -
Post : first operation kro then increment kro
pre : first increment kro then operation
Relational operator
--------------------------
Used to test comparison between operands or values
A+ b
Operands = A,B
Logical Operators
--------------------------
&& ---> AND -----> a && b --- if both true then only res will true
|| ----> OR ------> a || b --> if anyone is true then res will true
! ------> true --> false
Assignment Operator
-----------------------------
Int a =10 =
1. =
2. +=
3. -=
4. *=
5. /=
6. %=
New Section 1 Page 22
Int a=10
Int a=a+5; ------> int a+=5//
Ternary Operator
-------------------------
Int marks =32;
String res = (marks > 33) ? "Pass" : "Fail"
new
-------
new is keyword or operator which is used to create the object
ClassName ref = new ClassName();
Control Statement
-------------------------
1. these are used to control program flow
2. These are used to execute group of statement based on condition
New Section 1 Page 23
Type of control :
1. Decision Making : if , if-else, if-else-if, switch
2. Loop : for , while, do-while
3. Jump : break, continue, return
New Section 1 Page 24
How to take input from user
--------------------------------------
Scanner
Import in java
import
SwitchDemo
Scanner
Java.util.Scanner
Import java.util.Scanner
Import java.util.*
New Section 1 Page 25
Homework :
1. Take string value
2. Ask user which subject we want to choose eg: choice 2
Case 1: math
Case 2 : eng
SOP(english)
3. Home work
a. 1 --- one
b. 2 --- two
c. 9 ---
Loop:
Why we need loops ?
New Section 1 Page 26
Do while
------------
Jab ek bar loop chalana ho
For loop -----> condition check
While ----> condition check
Syntax
---------
Do {
SOP()
}
While(condition);
Kya 4-5 month complete ho payega ?
New Section 1 Page 27
1. break
2. continue
3. return
New Section 1 Page 28
Continue
------------
New Section 1 Page 29
Type of variables (based on scoop)
Java has 3 type of scope
Instance local static
Ese varaibles jo class k under or
Main method k bahar
New Section 1 Page 30
Lahore
Kamla nagar
Islamabad
delhi
india
Pakistan
New Section 1 Page 31
Instance : class k under or method k bahar
Local : method k ander can't use outside of moethod
Static : in static areas only static variable will be used, static variable can be used in instance or local area
Note: If we want to use instance variable inside static block/area we have to use object
Note : if we want to access static instance varaible in static area then no need to create object also we
have call using classname
New Section 1 Page 32
Student s = new Student();
name="Nazia";
age =24;
course = "Java";
#98022i083
s
Int a =10
instance local static
Ashwani
name="Nazia";
age =24; s Ashwani
course = "Java"; name="Nazia";
age =24; s
course = "Java";
Himadri
name="Nazia";
age =24; S1 Himadri
course = "Java"; name="Nazia";
age =24; S1
course = "Java";
Student.course
Har object k liy humne instance variable ki alag copy milti ha
For static instance variable only single will shared across the
object, if we change in one place then change will reflect in all
object because of single copy
Methods in java
----------------------
Obj.methodName
ClassName.methodName
New Section 1 Page 33
Types of method in java
--------------------------------
Type:
1. No Parameter, No Return
New Section 1 Page 34
Why ?
Int a=10
Int b=20
Int c=19
Array In java
------------------
New Section 1 Page 35
New Section 1 Page 36