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

Java PDF

The document provides an overview of Java, a high-level, object-oriented programming language developed by James Gosling in 1995, highlighting its features, history, and applications. It explains the concepts of programming languages, types of programming languages, and key principles of object-oriented programming such as encapsulation, inheritance, and polymorphism. Additionally, it discusses the Java Development Kit (JDK), Java Runtime Environment (JRE), and Java Virtual Machine (JVM), as well as the process of compiling and executing Java programs.

Uploaded by

neeraj pathak
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 views46 pages

Java PDF

The document provides an overview of Java, a high-level, object-oriented programming language developed by James Gosling in 1995, highlighting its features, history, and applications. It explains the concepts of programming languages, types of programming languages, and key principles of object-oriented programming such as encapsulation, inheritance, and polymorphism. Additionally, it discusses the Java Development Kit (JDK), Java Runtime Environment (JRE), and Java Virtual Machine (JVM), as well as the process of compiling and executing Java programs.

Uploaded by

neeraj pathak
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

JAVA

INTRODUCTION
JAVA Revision Class

What is Programming
Languages?
A programming language is a way for programmers to
communicate with computers.
It is a formal language that specifies a set of instructions for a
computer to perform specific tasks.
A programming language is a set of symbols, grammars and rules
with the help of which one is able to translate algorithms to
programs that will be executed by the computer.

Examples of programming languages include: Python, Java, C++,


JavaScript, C#, Ruby, PHP, Swift, etc.
JAVA Revision Class

Types of Programming
Language
Low Level Language
A "low-level language" is a programming language that is very close to the
computer's machine code, meaning it directly interacts with the hardware
and is harder for humans to understand.

High Level Language


A "high-level language" is closer to human language, making it easier to read
and write, but requires a compiler to translate it into machine code that the
computer can execute.
JAVA Revision Class

Low Level Language

Machine Language
Written in binary (0s and 1s).
Directly executed by the computer without any translation.
Example: Binary code specific to a CPU (e.g., 11001010 00011101).

Assembly Language
Uses symbolic representations (mnemonics) for instructions,
making it easier than machine language.
Requires an assembler to convert it into machine code.
Example: MOV A, B ; Moves data from register B to register A
JAVA Revision Class

High Level Language


Procedural Languages
Focus on a step-by-step procedure or instructions to perform tasks.
Examples: C, Fortran, Pascal

Object-Oriented Languages
Based on objects and classes, focusing on encapsulation,
inheritance, and polymorphism.
Examples: Java, Python, C++, C#, Ruby

Scripting Languages
Used for automation, web development, and managing tasks.
Examples: JavaScript, PHP, Python, Ruby
JAVA Revision Class

Object Oriented Programming


Language
Object-oriented means focusing on real-world entities (objects).
In simple terms, object-oriented programming is a way of writing programs that focus on objects or things, just like real-world entities.
It represents real-life concepts like inheritance (passing down features), polymorphism (doing different tasks), and encapsulation
(keeping information safe).

Object Oriented Programming Concepts

4. Encapsulation
1. Class
Encapsulation is the concept of bundling data (attributes) and the
Class is a group of similar type of object. A class is like a blueprint
or template for creating objects. It defines the attributes (data) functions (methods) that operate on that data into a single unit or class. It
and methods (functions) that the objects created from it will also hides the internal details of how the data is being processed, so that
have. A class itself is not an actual object; it's just a definition. outside code can only interact with the data through well-defined methods.

2. Object 5. Polymorphism:
An object is an instance of a class. It is a real-world entity Polymorphism means using the same function name to perform
created using the class blueprint. When you create an object, different tasks based on the object. For example, a "makeSound"
you are creating an actual thing that has the attributes and function can make different sounds for a dog and a cat.
methods defined in the class.

3. Inhertance 6. Abstraction
Inheritance is a concept in OOP where a new class (called a Abstraction is the concept of hiding the complex details of how
subclass or derived class) is created based on an existing class
something works and showing only the essential features. It
(called a superclass or base class). The subclass inherits the
attributes and methods of the superclass, allowing for code helps simplify the interaction with objects by focusing on what
reuse and the ability to extend or modify the inherited features. they do, not how they do it.
JAVA Revision Class

What is JAVA?
Java is a high-level, object-oriented programming language developed by James Gosling at Sun
Microsystems (now owned by Oracle) and released in 1995. It is easy to read and understand. With it,
developers can “write once, run anywhere” (WORA), meaning that the compiled Java code will run
on all Java-compatible platforms without the need for recompilation.

What is Java programming language used for?

Desktop applications
Mobile applications (specially Android apps)
Web applications
Web servers and application servers
Database connection
Game Development
Cloud computing
And much more
History of Java
Java was created by James Gosling and his team at Sun Microsystems in the early 1990s. It was initially called
Oak and later renamed Java in 1995.
Initially it was designed for small, embedded systems in electronic appliances like set-top boxes.
Firstly, it was called "Greentalk" by James Gosling, and the file extension was .gt.
After that, it was called Oak and was developed as a part of the Green project and later renamed Java in 1995..

Timeline of Java Development:

1991: Sun Microsystems started the "Green Project" to develop software for home appliances.

1995: Java was officially released with JDK 1.0. The key slogan was "Write Once, Run Anywhere" (WORA) due to its
platform independence.

1996: JDK 1.1 was released with improvements like JDBC (Java Database Connectivity).

1997: Java 2 (JDK 1.2) intr and the Java Virtual Machine (JVM) was improved.

2006: Sun Microsystems made Java open source by releasing OpenJDK.

2009: Oracle acquired Sun Microsystems, taking ownership of Java.

2018: Java 11 became the first Long-Term Support (LTS) release under the new schedule.

Java SE 24: This is the latest feature release, released on March 18, 2025.

Java SE 21: This is the current Long-Term Support (LTS) version, released on September 19, 2023.
JAVA Revision Class

Key Features of Java


1. Platform Independent
Java programs are compiled into bytecode, which can run on any system with a Java Virtual
Machine (JVM).
This makes Java highly portable across different operating systems like Windows, Linux, and macOS.

2. Object-Oriented Programming
Java follows the Object-Oriented Programming (OOP) paradigm, which includes:
Classes & Objects Inheritance Abstraction
Polymorphism Encapsulation

3. Simple & Easy to Learn

Java removes complex features like pointers and multiple inheritance


(which exist in C++).
It has an easy-to-understand syntax similar to C and C++.
JAVA Revision Class

4. Robustness

Java ensures application stability through strong memory


management, exception handling, and runtime checks, reducing
crashes and errors.

5. Security

Java runs in a sandboxed environment, preventing malicious


code from affecting the host system.
Features like bytecode verification, security manager, and
automatic memory management enhance security.

6. Dynamic & Extensible

Java supports dynamic class loading, meaning classes are


loaded into memory only when needed.
It can integrate with other technologies like C, C++ (via Java
Native Interface - JNI).
JAVA Revision Class

7. Multithreading

Java allows multiple threads to run simultaneously, enhancing


performance in applications like games, servers, and real-time
systems.

8. Portability

Java’s platform-independent bytecode ensures programs run


smoothly across different systems without requiring
modifications.

9. High Performance

Java achieves high performance through Just-In-Time (JIT)


compilation and runtime optimization, suitable for demanding
applications.
JAVA Revision Class public static void main(String[] args):
First Program in JAVA This is the main method, which is the entry point
for any Java application.
public class HelloWorld: public: Makes the method accessible from
This line declares a public class named HelloWorld. In Java, every outside the class.
program must have at least one class definition. The name of the static: Allows the method to be called without
class should match the name of the file (e.g., [Link]). creating an object of the class.
void: Indicates that the method doesn't return
any value.
main: The name of the method, which is
recognized as the starting point by the Java
runtime environment.
String[] args: An array of strings that can be
used to pass command-line arguments to the
program.

[Link]("Hello, World!");:
This line prints the text "Hello, World!" to the
console.
[Link]: Represents the standard
output stream.
println: A method that prints a line of text
followed by a newline.
JAVA Revision Class

The process of executing Java source code involves the following steps:

Compile the Code: Run the Program:


Use the javac Use the java
command to command to run
Write the Code:
compile the .java the compiled Output:
Create a file
file. bytecode. The program's
with a .java
Example: javac Example: java output is
extension (e.g.,
[Link] HelloWorld displayed on the
[Link])
The compiler The JVM (Java console.
and write the
converts the Virtual Machine)
program in it.
source code into executes the
bytecode (a bytecode line by
.class file). line.

Write → Compile → Run → Output


JAVA Revision Class

Compiler and Interpreter


Both compiler and interpreter are tools used to translate high-level programming languages into
machine code, but they work differently.

1. What is a Compiler?

A compiler is a program that translates the entire source code into machine code at once before
execution. The translated code (called object code) is saved and can be executed multiple times
without recompilation.
Example: C, C++ use compilers.

How Java Uses a Compiler?


Java uses a compiler (javac) to convert Java source code
(.java file) into bytecode (.class file), which is an intermediate
representation.
JAVA Revision Class

2. What is an Interpreter?

An interpreter translates and executes code line by line at runtime. It does not produce an
intermediate file; instead, it directly executes the code.
Example: Python, JavaScript use interpreters.
How Java Uses a Interpreter?
Java uses the Java Virtual Machine (JVM) as an interpreter to
execute bytecode line by line.

Java Uses Both Compiler and Interpreter

Compiler (javac) → Converts Java source code (.java) to bytecode (.class).


Interpreter (java command on JVM) →Executes the bytecode line by line.
JAVA Revision Class

Difference Between Compiler & Interpreter


JAVA Revision Class

JVM (Java Virtual Machine) → The Chef


What it does?

JVM is the engine that runs Java programs.


It takes the compiled Java file (bytecode) and converts it into machine code that your
computer understands.
JVM ensures Java programs can run on any OS (Windows, Mac, Linux) without modification.

Key Role: Runs Java code and manages memory (garbage collection).
Can you install JVM alone? ❌ No, it comes with the JRE.
JAVA Revision Class

JRE (Java Runtime Environment) → The Kitchen


What it does?

JRE is a set of tools that allows you to run Java programs.


It includes the JVM + essential libraries needed for execution.

Key Role: Provides everything required to run Java programs.


Can you write Java programs with JRE? ❌ No, you need JDK for that.
JAVA Revision Class

JDK (Java Development Kit) → The Recipe Book


📖 + Kitchen
What it does?

JDK is a complete package for developing Java applications.

It includes .
JRE (to run Java programs)
Compiler (javac) – Converts Java code into bytecode
Debugger & development tools

Key Role:Allows developers to write, compile, and run Java programs..


Can you run Java programs with only JDK? ✅ Yes, because JDK includes JRE and JVM
JAVA Revision Class

Comparison Table

JVM (Java Virtual JRE (Java Runtime JDK (Java


Feature
Machine) Environment) Development Kit)

Purpose Runs Java programs Provides an environment Develops and runs Java
to run Java programs programs

Includes Just the JVM JVM + Java Libraries JRE + Compiler + Debugger

Used by End-users running End-users running Java Developers writing Java


Java apps apps code

Installation No, comes with JRE Yes, for running Java Yes, for developing Java
Needed? apps apps
JAVA Revision Class

Which One Do You Need?


If you only want to run Java programs→ Install JRE.
If you want to develop Java programs → Install JDK (which includes JRE & JVM).

Example to Understand JVM, JRE, JDK in Action



If you only want to run Java programs
Install [Link] 1: You write Java code ([Link]).Step 1: You write Java code
Step 1: You write Java code ([Link]).([Link]).
Step 2: JDK's compiler (javac) converts it into bytecode ([Link]).
Step 3: JVM executes the bytecode using JRE.
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, Java!");
}
}
JAVA Revision Class

Conclusion
JVM → Runs Java programs.
JRE → Provides an environment to run Java programs.
JDK → Used for developing Java programs (includes JRE & JVM).

If you only want to run Java programs → Install [Link] 1: You write Java code ([Link]).Step 1: You write Java code
([Link]).
JAVA Revision Class

IDE
An IDE (Integrated Development Environment) is a software application that provides all
the necessary tools for writing, compiling, and debugging code in one place. It makes
coding easier, faster, and more efficient for developers.

Byte code
Bytecode is an intermediate code generated by the Java
compiler (javac) after compiling a Java program. It is not
machine-specific and can run on any platform that has a
Java Virtual Machine (JVM).
JAVA Revision Class

What is token in Java?


In Java, Tokens are the smallest elements of a program that is meaningful to the compiler. They are
also known as the fundamental building blocks of the program.

1. Keywords

2. Identifiers

Types 3. Literals

4. Operators

5. Separators
JAVA Revision Class Keywords are pre-defined or reserved words in a programming
language.

Keywords Each keyword is meant to perform a specific function in a program.


Since keywords are referred names for a compiler, they can’t be used
as variable names because by doing so, we are trying to assign a new
meaning to the keyword which is not allowed.
Java language supports the following keywords:
JAVA Revision Class

Keywords
JAVA Revision Class

Identifiers
In Java, an identifier is the name given to variables, methods, classes, packages, or any other user-defined
item. There are specific rules for naming identifiers in Java, which help ensure that the code is clear, readable,
and doesn't conflict with Java's reserved keywords.

Rules for naming identifiers in Java

1. Identifiers must start with a letter (a-z, A-Z), an underscore (_), or a


dollar sign ($).
2. They cannot begin with a number.
3. After the first character, an identifier can contain letters, digits (0-9),
underscores (_), or dollar signs ($).
4. Java is case-sensitive, so myVariable and myvariable would be
considered different identifiers. In Java, whitespace (like spaces, tabs, or
newlines) cannot be used in an identifier.
JAVA Revision Class
VALID InValid
myVariable 123abc (starts with a number)

_tempValue !myVar (starts with an invalid character)

$value my-var (hyphen is not allowed)

Valid And
var1 total# (hash symbol is not allowed)

_temp123 1stName (starts with a number)

InVaild MyVariable public (reserved keyword)

Identifiers
count1 my-variable (contains a hyphen)

sum_total

MAX_VALUE

num$

first_name
JAVA Revision Class

Constants/ Literals
Constants are also like normal variables. But the only difference is, their values cannot be modified by
the program once they are defined. Constants refer to fixed values. They are also called as literals.
Constants may belong to any of the data type.

literals Type
23 int

9.86 Floating Point

false, true boolean

'K', '7', '-' char

"javatpoint" String

null any reference type


JAVA Revision Class

Separators
Separators are used to separate different parts of the codes. It tells the compiler about completion of a
statement in the program. The most commonly and frequently used separator in java is semicolon (;).

It is used to define array elements. A pair of square brackets


Square Brackets [] represents the single-dimensional array, two pairs of square
brackets represent the two-dimensional array.

Parentheses () It is used to call the functions and parsing the parameters.

The curly braces denote the starting and ending of a code


Curly Braces {}
block.

Comma (,) It is used to separate two values, statements, and parameters.

Assignment Operator (=) It is used to assign a variable and constant.

It is the symbol that can be found at end of the statements. It


Semicolon (;)
separates the two statements.

It separates the package name form the sub-packages and class.


Period (.)
It also separates a variable or method from a reference variable.
JAVA Revision Class
JAVA Revision Class

Data Types in Java


Data types in Java specify the type of value a variable can store. Java is a statically typed language, so every
variable must be declared with a data type before use. Java provides various data types in different sizes to
ensure efficient memory usage and accurate data representation for all kinds of programming needs.

Types of Data Types in JAVA


1. Primitive data types

The primitive data types include boolean, char,


byte, short, int, long, float and double.

2. Non-Primitive data types

The non-primitive data types include Classes,


Interfaces, and Arrays.
JAVA Revision Class

JAVA Primitive Data Types


In Java language, primitive data types are the building blocks of data manipulation.
These are the most basic data types available in Java language.
In Java, there are mainly eight primitive data types.

boolean int

byte long
Primitive
Data
Types
char float

short double
Data Type Size Default Description Example
Value

Stores whole numbers from -128 to 127.


byte 1 byte 0 byte b = 100;
Useful for saving memory in large arrays.

Stores whole numbers from -32,768 to


short 2 bytes 0 short s = 30000;
32,767.

Stores whole numbers from -2,147,483,648


int 4 bytes 0 to 2,147,483,647. Default data type for int i = 123456;
integers.

Stores whole numbers from


long 8 bytes 0L -9,223,372,036,854,775,808 to long l = 123456789L;
9,223,372,036,854,775,807.

Stores fractional numbers. Precision: 6-7


float 4 bytes 0.0f float f = 3.14f;
decimal digits.

Stores fractional numbers. Precision: 15


double d =
double 8 bytes 0.0d decimal digits. Default for floating-point
3.14159265359;
literals.

char 2 bytes '\u0000' Stores a single 16-bit Unicode character. char c = 'A';

Stores only two values: true or false. Used


boolean 1 bytes false boolean flag = true;
for conditional logic.
JAVA Revision Class

JAVA Non-Primitive Data Types


The Non-Primitive (Reference) Data Types will contain a memory address of variable
values because the reference types won’t store the variable value directly in memory. They
are strings, objects, arrays, etc.

string

Non-
Primitive array
Data
Types

Classes,
Interfaces, Enums
JAVA Revision Class

Category Description Example

Represents a sequence of characters (text). Strings are


String String str = "Hello, Java!";
immutable in Java.

Used to store multiple values of the same type in a


Array int[] arr = {1, 2, 3};
single variable.

User-defined blueprints for creating objects,


Classes
encapsulating data and methods.

Contracts that define a set of methods that classes can


Interfaces
implement.

Special data types that allow a variable to be a set of


Enums
predefined constants.
JAVA Revision Class

Operators
Java provides many types of operators which can be used according to the need. They are
classified based on the functionality they provide.

Operator Symbols
Arithmetic +,-,/,*,%

Unary ++ , - - , !

Assignment = , += , -= , *= , /= , %= , ^=

Relational ==, != , < , >, <= , >=

Logical && , ||

Ternary (Condition) ? (Statement1) : (Statement2);

Bitwise &,|,^,~

Shift << , >> , >>>


JAVA Revision Class

Operators in JAVA
Java operators are special symbols that perform operations on variables or values. They can be
classified into several categories based on their functionality. These operators play a crucial role in
performing arithmetic, logical, relational, and bitwise operations etc.

Arithmetic Logical

Unary Ternary
Operators
in
Assignment JAVA Bitwise

Relational Shift
JAVA Revision Class

1. Arithmetic Operators

Arithmetic Operators are used to perform simple arithmetic operations on primitive


and non-primitive data types.

Operator Description Example

+ Addition a+b

- Subtraction a-b

* Multiplication a*b

/ Division a/b

% Modulus (remainder) %
JAVA Revision Class

2. Unary Operators

Unary Operators need only one operand. They are used to increment, decrement, or negate a
value.

Operator Description Example

+ Unary plus (positive) +a

- Unary minus (negative) -a

++ Increment (pre/post) ++a or a++

-- Decrement (pre/post) --a or a--

! Logical complement (NOT) !a


JAVA Revision Class

3. Logical Operators

Logical Operators are used to perform “logical AND” and “logical OR” operations, similar to
AND gate and OR gate in digital electronics. They have a short-circuiting effect, meaning the
second condition is not evaluated if the first is false.

Operator Description Meaning

true only if both expression1 and


&& (Logical AND) expression1 && expression2
expression2 are true

true if either expression1 or expression2


|| (Logical OR) expression1 || expression2
is true

! (Logical NOT) true if expression is false and vice


!expression
versa
JAVA Revision Class

4. Relational Operators

Relational Operators are used to check for relations like equality, greater than, and less than.
They return boolean results after the comparison and are extensively used in looping
statements as well as conditional if-else statements.

Operator Description Meaning

== Equal to a == b

!= Not equal to a != b

> Greater than a>b

< Less than a<b

>= Greater than or equal to a >= b

<= Less than or equal to a <= b


JAVA Revision Class

5. Assignment Operators

‘=’ Assignment operator is used to assign a value to any variable. It has right-to-left
associativity, i.e. value given on the right-hand side of the operator is assigned to the variable
on the left, and therefore right-hand side value must be declared before using it or should be
a constant.

Operator Description Meaning

= Assign a=b

+= Add and assign a += b

-= Subtract and assign a -= b

*= Multiply and assign a *= b

/= Divide and assign a /= b

%= Modulus and assign a %= b


JAVA Revision Class

6. Ternary Operators

The Ternary Operator is a shorthand version of the if-else statement. It has three operands
and hence the name Ternary.

Operator Description Meaning

?: Conditional operator a = (b > c) ? b : c;

7. Shift Operators

Shift Operators are used to shift the bits of a number left or right, thereby multiplying or
dividing the number by two, respectively. They can be used when we have to multiply or
divide a number by two.
JAVA Revision Class

Comments in Java

In Java, comments are used to explain the code.


They are ignored by the compiler and are written only for humans (programmers).

Comments help in:


Making code easy to understand 1⃣ Single-Line Comment
Debugging 👉Used for writing a short note in one line.
Documentation 👉Starts with //
Explaining logic

2⃣ Multi-Line Comment
👉Used for writing explanations in multiple lines.
👉Starts with /* and ends with */

Types of Comments
in Java 3⃣ Documentation Comment (Javadoc Comment)
👉 Used to generate documentation.
👉 Starts with /** and ends with */
This type of comment is used with Javadoc tool in Java.
JAVA Class

Single-Line Comment
Documentation Comment (Javadoc Comment)
// This is a single-line comment
[Link]("Hello World"); // Prints Hello World
/**
* This program prints Hello World
* Author: Neeraj Sir
*/
public class Test {
public static void main(String[] args) {
Multi-Line Comment
[Link]("Hello World");
/* This is a multi-line comment }
It can be written in }
multiple lines */
[Link]("Java Programming");

You might also like