Unit 1 0 at Summary
Unit 1 0 at Summary
Unit 1 and 2
-
Java
• What is Java? → A high-level, object-
oriented programming language.
• Features of Java → Platform
independence, security,
multithreading, memory
management.
• Java vs. C++ → Java has automatic
memory management, no pointers,
supports garbage collection.
• Java Architecture → JDK, JVM, JRE
and how they work together.
Java Basics
Your code tell the JVM to do"
• Java Tokens → Keywords, Identifiers, Literals,
Operators, Separators.
• Data Types → Primitive (int, double, boolean)
& Non-primitive (String, Arrays).
• Variables → Scope & Lifetime (Local,
Instance, Static).
• Operators → Arithmetic, Relational, Logical,
Bitwise, Assignment, Increment/Decrement.
• Control Structures →
• Decision Making: if, if-else, switch-
case.
• Loops: for, while, do-while.
• Jump Statements: break, continue,
return.
Object-Oriented
Programming
(OOP) in Java
• Classes & Objects → Blueprint vs. Real-world
entity.
• Constructors → Default, Parameterized, Copy
Constructor.
• Access Modifiers → Public, Private, Protected,
Default.
• Static Keyword → Static variables, methods,
and blocks.
• Final Keyword → Final variables, final
methods, and final classes.
• Garbage Collection → Automatic memory
management in Java.
• this Keyword → Refers to the current instance
of a class.
Inheritance in Java
• Types of Inheritance:
• Single → Class B extends A
• Multilevel → Class C extends B extends A
• Hierarchical → Class B extends A, Class C extends A
• Multiple (via interfaces) → Class C implements A, B
• Hybrid (via interfaces) → Combination of types
• super Keyword → Calls parent class constructor/method.
• Method Overriding → Achieves runtime
polymorphism.
• Covariant Return Type → Allows overriding methods to
return subclass types.
• Abstract Class vs. Interface → Differences and use
cases.
Interfaces &
Packages
• What is an Interface? →
A blueprint of a class
(100% abstraction).
• How to Implement an
Interface → Class X
implements Interface Y.
• Packages in Java → Built-
in (java.util, java.io) &
User-defined.
• How to Create & Import
Packages → import
mypackage.*;.
Exception
Handling in Java
• What is an Exception? → An
unexpected event that disrupts
program flow.
• Types of Exceptions →
Checked & Unchecked
exceptions.
• Exception Handling
Keywords → try, catch, finally,
throw, throws.
• User-Defined Exception →
Custom exceptions for specific
error handling.
Arrays in Java
Two-Dimensional
One-Dimensional
Arrays → int[][]
Arrays → int[] arr =
matrix = new int[3]
new int[5];.
[3];.
Array Operations →
Jagged Arrays →
Sorting, Searching
Arrays with different
(Linear & Binary),
column sizes.
Reversing.
Multithreading in Java
What is Multithreading? → Running multiple tasks
simultaneously.
Thread Lifecycle → New → Runnable → Running → Blocked →
Terminated.
Creating Threads →
Definition:
• Identifiers are used to name variables, methods, classes, interfaces,
packages, etc.
Rules:
• Formed using letters (A-Z, a-z), digits (0-9), underscore (_), and dollar
sign ($).
• Must begin with a letter, underscore, or dollar sign.
• No length limit.
• No special symbols other than underscore(_) and dollar sign($).
• No white space characters.
Examples:
• Valid: demo, Demo, DEMO, Demo2, version$2, version_2,
_5_, _$2,
$_2
• Invalid: 2ndDemo, version#2, version-2, 5_$
Keywords
Definition:
• Reserved words in the Java language.
Examples:
• int, char, float, double, if, else, while,
for, do, private, protected, public,
static, final, void, assert, enum, class,
interface, package.
Notes:
• Keywords must be in lowercase.
• strictfp (JDK 1.2), assert (JDK 1.4),
enum (JDK 1.5).
• const and goto are not in use.
• Keywords cannot be used as
identifiers.
Literals
Definition:
• Fixed value representations in source code.
Categories:
• Integer Literals: 5, 9, 13, 467, 0, -2, -98, -987
• Floating Point Literals: 2.46, 0.08, -2.46, -999.3566
• Character Literals: 'a', 'x', 'A', 'Z', 'c'
• String Literals: "a", "hi", "hello", "welcome"
• Boolean Literals: true, false
6. Object Literal: null
Notes:
• true, false, and null are not keywords and
cannot be used as identifiers.
Data Types
Definition:
• Determines the value a variable can
hold and the operations on variables.
Categories:
• Primitive Data Types: Predefined types
named by keywords.
• Integers: byte, short, int, long
• Floating Points: float, double
• Characters: char
• Boolean: boolean
• Reference Data Types: Arrays, Strings,
Classes, Interfaces, etc.
Variables
Definition:
• Containers that hold data.
Declaration:
• Syntax: DataType Variable;
• Example: int x;
Assignment:
• Syntax: Variable = Literal;
• Example: x = 10;
Initialization:
• Syntax: DataType Variable = Literal;
• Example: int x = 10;
Boolean Type:
• Cannot be converted to or from other types.
Implicit Conversions (Widening Conversions):
• System performs these automatically.
· Total: 19
• byte: to short, int, long, float, double (5)
• short: to int, long, float, double (4)
• int: to long, float, double (3)
Type Conversions
• long: to float, double (2)
• float: to double (1)
• char: to int, long, float, double (4)
in Java •
Explicit Conversions (Narrowing Conversions):
Must be done by the programmer.
· Total: 23
• byte: to char (1)
• short: to byte, char (2)
• int: to byte, short, char (3)
• long: to byte, short, int, char (4)
• float: to byte, short, int, long, char (5)
• double: to byte, short, int, long, float, char (6)
char: to byte, short (2)
• Only one public class per source file.
• Any number of non-public classes
Source File allowed.
Structure: }
}
String and System classes are part of the
java.lang package, imported by default.
· Compilation:
C:\>javac Demo.java
• Generates .class file for each
class in the source file.
Compilation and
Execution: · Execution:
C:\>java Demo
• Only the class with the main()
method can be used to execute
the program.
Operators: Special symbols that operate on
data, categorized by their operations.
1) Arithmetic Operators
• Addition (+): Adds values.
• Subtraction (-): Subtracts values.
• Multiplication (*): Multiplies values.
• Division (/): Divides values, returns
quotient.
• Modulo (%): Divides values, returns
Java Operators remainder.
2) Relational Operators
• <: Less than
• >: Greater than
• <=: Less than or equal to
• >=: Greater than or equal to
· ==: Equals to
• !=: Not equals to Returns boolean
values.
3) Logical Operators
• &&: Logical AND
• ||: Logical OR
• !: Logical NOT
Types of Operators
• Unary Operator: Operates on one
operand (e.g., ++a, a--, !a).
• Binary Operator: Operates on two
operands (e.g., a + b, a > b).
• Ternary Operator: Operates on three
operands (e.g., condition ? expr1 :
expr2).
6) Assignment Operators
· = : Normal assignment
· += : Add and assign (a += b è a = a + b)
• -= : Subtract and assign (a -= b è a = a - b)
• *= : Multiply and assign (a *= b è a = a * b)
• /= : Divide and assign (a /= b è a = a / b)
• %= : Modulo and assign (a %= b è a = a %
b)
&=, |=, ^=, <<=, >>= : Bitwise operations and
assign
7) Conditional Operator (?:)
Used to express conditions.
• 9) Other Operators
• [] : Array access operator
• () : Type cast operator
• instanceof : Checks if an
object is an instance of a class
• . : Member selection operator
• () : Method call operator
Java statements are categorized into
three main types:
1) Selection Statements
• if Statement: Executes a
block of code if a condition is
true.
• if-else Statement: Executes
one block of code if a condition
Java Statements is true and another if it is false.
• if-else if-else Statement:
Checks multiple conditions,
executing corresponding blocks
of code.
• Nested if Statement: An if
statement inside another if
statement.
• switch Statement: Selects one of many
code blocks to be executed.
• while loop: Repeats a block of
code while a condition is true.
• do-while loop: Executes a
block of code once, and then
2) Iteration repeats it while a condition is
true.
Statements • for loop: Repeats a block of
code a specified number of
(Loops) times.
• Enhanced for loop (for-
each): Iterates over elements
of an array or a collection.
• Nested loops: Loops within
loops.
• break Statement: Exits a loop
or switch statement.
• break LABEL Statement:
Exits an outer loop.
• continue Statement: Skips
the current iteration of a loop
3) Jump and proceeds with the next
iteration.
Statements • continue LABEL Statement:
Skips the current iteration of an
outer loop.
• return Statement: Exits from
the current method and
optionally returns a value.