0% found this document useful (0 votes)
9 views5 pages

Java Unit1 University Notes

These university notes cover essential Java concepts including Object Oriented Programming principles, Java essentials, JVM, program structure, data types, variables, operators, selection statements, and iteration statements. Each section includes definitions, syntax, examples, and key points to aid in exam preparation. The notes are formatted for various answer lengths suitable for university exams.

Uploaded by

sakshiii0710
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)
9 views5 pages

Java Unit1 University Notes

These university notes cover essential Java concepts including Object Oriented Programming principles, Java essentials, JVM, program structure, data types, variables, operators, selection statements, and iteration statements. Each section includes definitions, syntax, examples, and key points to aid in exam preparation. The notes are formatted for various answer lengths suitable for university exams.

Uploaded by

sakshiii0710
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 Unit–1 Detailed University Notes

Prepared from the syllabus topics shown in the image. These notes are written in university-answer style
suitable for 5-mark, 7-mark and 10-mark answers, with definitions, syntax, examples and key points.

1. Object Oriented Programming (OOP) Principles


Definition: Object Oriented Programming is a programming methodology in which programs are
organized around objects rather than only functions and logic. An object represents a real-world entity
having state (data) and behavior (methods). Java is a pure object-oriented, class-based and
platform-independent language. Main Principles of OOP:
(i) Class: A class is a blueprint or template used to create objects.
(ii) Object: An object is an instance of a class.
(iii) Encapsulation: Wrapping data and methods into one unit. It is achieved using classes and access
modifiers.
(iv) Abstraction: Hiding internal implementation and showing only essential features.
(v) Inheritance: Acquiring properties of one class into another class.
(vi) Polymorphism: One name, many forms. Example: method overloading and method overriding.
Advantages of OOP: 1. Code reusability 2. Better security 3. Easy maintenance 4. Modularity 5.
Real-world modeling Example: A Student class can have data like name and rollNo and behavior like
display().

2. Java Essentials
Java Essentials are the basic features and building blocks required to write and run Java programs.
Important features of Java: 1. Simple: Java syntax is easier than many older languages. 2. Object
Oriented: Java supports classes and objects. 3. Platform Independent: “Write Once, Run Anywhere”. 4.
Secure: Java provides bytecode verification and memory safety. 5. Robust: Strong memory management
and exception handling. 6. Multithreaded: Java can execute multiple tasks simultaneously. 7.
Distributed: Supports networking and internet programming. 8. Portable: Programs can run on different
systems. 9. Dynamic: Classes can be loaded during execution. Java Development Tools: - JDK (Java
Development Kit): Used for developing Java applications. - JRE (Java Runtime Environment): Used to run
Java programs. - JVM (Java Virtual Machine): Executes Java bytecode.

3. Java Virtual Machine (JVM)


Definition: JVM is an abstract machine that provides the runtime environment in which Java bytecode can
be executed. Working of JVM: 1. Java source file (.java) is written. 2. It is compiled by javac compiler
into bytecode (.class). 3. JVM loads the bytecode and executes it. Functions of JVM: - Loads class files -
Verifies bytecode - Executes bytecode - Manages memory - Performs garbage collection Memory areas
in JVM: 1. Method Area 2. Heap Area 3. Stack Area 4. PC Register 5. Native Method Stack Importance:
JVM makes Java platform independent because the same bytecode can run on any system having JVM.

4. Program Structure in Java


A Java program is written inside a class. Execution starts from the main() method. General Structure: 1.
Documentation / comments 2. Package statement (optional) 3. Import statements (optional) 4. Class
definition 5. Variables and methods 6. main() method Syntax of main method:
public static void main(String[] args) Meaning: - public: accessible from anywhere - static: can be
called without creating object - void: returns no value - String[] args: stores command line arguments
5. Java Class Libraries
Definition: Java class library is a collection of pre-defined classes and packages provided by Java to
perform common tasks. Important packages: 1. [Link] – String, Math, System, Object 2. [Link] –
Scanner, ArrayList, Date 3. [Link] – File handling, streams 4. [Link] – Networking 5. [Link] and
[Link] – GUI 6. [Link] – Database connectivity Benefits: - Saves coding time - Provides tested
code - Improves productivity

6. Data Types in Java


Definition: Data type specifies the type of value that a variable can store. Two categories of data types:
(A) Primitive Data Types 1. byte – 1 byte 2. short – 2 bytes 3. int – 4 bytes 4. long – 8 bytes 5. float – 4
bytes 6. double – 8 bytes 7. char – 2 bytes 8. boolean – true/false (B) Non-Primitive Data Types
Examples: String, Array, Class, Interface, Object Example: - int age = 20; - double salary = 25000.50; -
char grade = 'A'; - boolean result = true;

7. Variables and Arrays


Variable: A variable is a named memory location used to store data. Types of Variables: 1. Local
variable – declared inside method 2. Instance variable – declared inside class but outside method 3.
Static variable – declared with static keyword Rules for variable names: - Must begin with letter, _ or $ -
Cannot start with digit - Cannot use reserved keywords Array: An array is a collection of same type of
elements stored in contiguous memory locations. Syntax: datatype[] arrayName; or datatype
arrayName[]; Example: int[] marks = {80, 75, 90, 88}; Advantages of arrays: - Easy storage of multiple
values - Fast access using index

8. Data Type Casting


Definition: Type casting means converting one data type into another. Types of Casting: (i) Widening
Casting (Implicit)
Small type → Large type
Example: int → long → float → double (ii) Narrowing Casting (Explicit)
Large type → Small type
Needs cast operator Syntax: smallType var = (smallType) largeValue; Example: double d = 10.75; int x =
(int)d; // x becomes 10 Importance: Used when handling different types in expressions and assignments.

9. Automatic Type Promotion in Expressions


When arithmetic expressions involve different data types, Java automatically converts smaller types into
larger compatible types. This is called automatic type promotion. Rules: 1. byte, short and char are
promoted to int during arithmetic operations. 2. If one operand is long, result becomes long. 3. If one
operand is float, result becomes float. 4. If one operand is double, result becomes double. Example: byte
a = 10, b = 20; int c = a + b; // result is int, not byte Use: Prevents loss of precision in expressions.

10. Operators in Java


Definition: Operators are symbols used to perform operations on variables and values. Main categories:
1. Arithmetic Operators 2. Bitwise Operators 3. Relational Operators 4. Logical Operators 5. Assignment
Operators 6. Unary Operators 7. Conditional (?:) Operator

11. Arithmetic Operators


Used for mathematical calculations. Operators: + Addition - Subtraction * Multiplication / Division %
Modulus Example: If a = 10 and b = 3: - a + b = 13 - a - b = 7 - a * b = 30 - a / b = 3 - a % b = 1
Increment/Decrement: ++ and -- Example: int x = 5; x++; // x becomes 6

12. Bitwise Operators


Bitwise operators work on binary bits. Operators: & Bitwise AND | Bitwise OR ^ Bitwise XOR ~ Bitwise
NOT << Left Shift >> Right Shift >>> Unsigned Right Shift Example: a = 5 (0101), b = 3 (0011) - a & b = 1
- a | b = 7 - a ^ b = 6 Use: Used in low-level programming, flags and performance-oriented logic.

13. Relational Operators


Relational operators are used to compare two values and return boolean result. Operators: == Equal to !=
Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to Example: int
a = 10, b = 20; a < b // true a == b // false Use: Mainly used in decision making statements like if and while.

14. Boolean Logical Operators


These operators work on boolean values. Operators: && Logical AND || Logical OR ! Logical NOT
Example: boolean x = true, y = false; x && y // false x || y // true !x // false Short-circuit operators: &&
and || are short-circuit operators in Java.

15. Conditional / Ternary Operator


The conditional operator ?: is a shorthand form of if-else. Syntax: condition ? expression1 : expression2;
Example: int a = 10, b = 20; int max = (a > b) ? a : b; If condition is true, first expression is executed;
otherwise second expression is executed. Advantage: Reduces code length for simple decisions.

16. Operator Precedence


When multiple operators are used in one expression, Java follows a priority order called operator
precedence. Higher to lower precedence (simplified): 1. Unary: ++, --, !, ~ 2. Multiplicative: *, /, % 3.
Additive: +, - 4. Shift: <<, >>, >>> 5. Relational: <, >, <=, >= 6. Equality: ==, != 7. Bitwise: &, ^, | 8. Logical:
&&, || 9. Conditional: ?: 10. Assignment: =, +=, -= etc. Example: int x = 10 + 5 * 2; // result = 20, not 30
Tip: Use brackets to avoid confusion.

17. Java Selection Statements


Selection statements are used to choose one block of code from multiple alternatives. Types: 1. if
statement 2. if-else statement 3. nested if 4. else-if ladder 5. switch statement

18. if, if-else, nested if, else-if ladder


(i) if Statement
Used when code should execute only if condition is true. Syntax: if(condition) { statements; } (ii) if-else
Statement
Used when one block executes if condition is true and another if false. Syntax: if(condition) { statements; }
else { statements; } (iii) Nested if
An if statement inside another if. (iv) else-if ladder
Used for multiple conditions. Example use: grading system, eligibility checking, menu options.

19. switch Statement


The switch statement selects one block from many alternatives based on a value. Syntax:
switch(expression) { case value1: statements; break; case value2: statements; break; default: statements;
} Important points: - expression can be int, char, String, enum etc. - break prevents fall-through. -
default executes if no case matches. Use: Better than else-if ladder when many fixed options are present.

20. Iteration Statements (Loops)


Iteration statements are used to repeat a block of code. Types of loops: 1. while loop 2. do-while loop 3.
for loop 4. enhanced for loop Advantages: - Reduces code repetition - Saves time - Useful for arrays and
repeated tasks

21. while, do-while, for and enhanced for


(i) while loop
Checks condition first, then executes. Syntax: while(condition) { statements; } (ii) do-while loop
Executes at least once, then checks condition. Syntax: do { statements; } while(condition); (iii) for loop
Best when number of iterations is known. Syntax: for(initialization; condition; update) { statements; } (iv)
Enhanced for loop
Used mainly for arrays and collections. Syntax: for(datatype var : array) { statements; }

22. Jump Statements


Jump statements transfer control from one part of the program to another. Types: 1. break 2. continue 3.
return (i) break: Terminates loop or switch. (ii) continue: Skips current iteration and moves to next. (iii)
return: Exits from method and optionally returns value. Use: Improves control over loops and methods.

23. Important University Answer Tips


For a 5-mark answer, write: - Definition - Syntax - One example - 3 to 4 key points For a 10-mark
answer, write: - Definition - Explanation of each part - Syntax - Program/example - Output or result -
Advantages / limitations / applications - Conclusion Exam tip: Always underline keywords like class,
object, JVM, array, operator, loop and selection statement.

Important Syntax and Examples


// 1. Simple Java Program
class Hello {
public static void main(String[] args) {
[Link]("Hello Java");
}
}

// 2. Variables and Data Types


int age = 20;
double salary = 25000.50;
char grade = 'A';
boolean pass = true;

// 3. Array
int[] marks = {80, 75, 90, 88};

// 4. Type Casting
double d = 10.75;
int x = (int)d;

// 5. if-else
if(age >= 18) {
[Link]("Eligible");
} else {
[Link]("Not Eligible");
}
// 6. switch
int day = 2;
switch(day) {
case 1: [Link]("Monday"); break;
case 2: [Link]("Tuesday"); break;
default: [Link]("Invalid");
}

// 7. for loop
for(int i=1; i<=5; i++) {
[Link](i);
}

// 8. while loop
int i = 1;
while(i <= 3) {
[Link](i);
i++;
}

// 9. Ternary Operator
int a = 10, b = 20;
int max = (a > b) ? a : b;

End of Unit–1 notes. This PDF includes every topic visible in your syllabus image and is formatted for
exam preparation.

You might also like