Java Unit1 ExamNotes
Java Unit1 ExamNotes
History of Java 1991 Oak → 1995 Java by Sun; WORA principle; JVM magic
Class & Object Class = blueprint; Object = real instance; new keyword creates object
Variables & Scope Local (method), Instance (class), Static (class-level shared)
1991 James Gosling at Sun Microsystems starts 'Green Project'. Language first called Oak.
1995 Oak renamed Java (inspired by Java coffee). Publicly released. Applets debut in browsers.
v [javac compiler]
|
v [JVM on Windows / Linux / Mac]
Program Output
JDK is the outermost container. JRE is inside JDK. JVM is inside JRE.
Memory Trick: D(evelop) > R(un) > V(irtualize) — DRV → Developer Runs Virtually.
Syntax derived from C/C++ but without complex features like pointers, operator
Simple overloading, multiple inheritance.
Platform-Independent WORA — bytecode runs on any JVM. Achieved via intermediate bytecode.
Multithreaded Built-in support for concurrent programming using Thread class / Runnable interface.
High Performance JIT (Just-In-Time) compiler converts bytecode to native code at runtime for speed.
Dynamic Classes loaded at runtime. Supports reflection. Programs can adapt at run time.
■ Memory Trick
Simple Secure Robust Object Platform Multithreaded Architecture High Dynamic = SSROPMAHD → 'Some
Students Run On Platforms Making Amazing High-quality Degrees'
Object-Oriented Programming (OOP) is a programming paradigm that organises software design around
data (objects) rather than functions and logic. It models real-world entities as objects that have state
(attributes/fields) and behaviour (methods). Java is a purely object-oriented language (except for its 8
primitive data types).
Real-world analogy: A capsule tablet — the medicine is inside, protected. You only interact via the outer shell
(methods like setName, getName).
// Encapsulation Example
Pillar 2 — Inheritance
Definition: The mechanism by which one class (child/subclass) acquires the properties and behaviours (fields and
methods) of another class (parent/superclass) using the extends keyword. Promotes code reusability.
Type Description
// Inheritance Example
class Animal {
// Usage:
Pillar 3 — Polymorphism
Definition: The ability of an object to take many forms. A single interface can represent different underlying forms
(data types/classes). Comes in two types: Compile-time (Method Overloading) and Runtime (Method Overriding).
Method Overloading (Compile-time Same method name, different parameter lists in the SAME class.
Polymorphism) Resolved at compile time.
Method Overriding (Runtime Same method name + same parameters in PARENT and CHILD class.
Polymorphism) Child overrides parent. Resolved at runtime via dynamic dispatch.
Pillar 4 — Abstraction
Definition: Hiding the internal implementation details and showing only the essential features to the user.
Achieved in Java using abstract classes and interfaces.
Real-world analogy: You drive a car without knowing how the engine works internally — you only interact with the
steering wheel and pedals (the interface).
■ Memory Trick
A class in Java is a user-defined blueprint or template that defines the structure and behaviour of objects. It
is a logical construct that encapsulates data members (fields/variables) and member functions (methods)
into a single unit. A class does not occupy memory by itself — memory is allocated only when an object is
created.
Syntax of a Class:
class ClassName {
dataType fieldName;
ClassName(parameters) {
// initialization code
// 3. Methods — behaviour
returnType methodName(parameters) {
// method body
An object is a runtime instance of a class. It is a real entity that has: (1) State — represented by its instance
variables; (2) Behaviour — defined by its methods; (3) Identity — a unique reference in memory (handled
by the JVM). Objects are created using the new keyword, which allocates memory on the heap.
class Box {
double height;
double depth;
width = w;
height = h;
depth = d;
// Method — behaviour
double volume() {
class Main {
Volume = 150.0
■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■
When you do Box b3 = b1; both b3 and b1 refer to the same object. This is NOT copying the object — it is
copying the reference (address). So [Link] = 99 will also change b1's width!
4.1 Literals
■ Formal Definition
A literal is a fixed, constant value that appears directly in the source code. Literals represent the raw data
values and are assigned directly to variables. Java supports six types of literals: Integer, Floating-point,
Character, String, Boolean, and Null.
int a = 42;<br/>int
hex = 0xFF;<br/>int
bin = 0b1010;<br/>int Decimal, Hexadecimal (0x), Binary (0b), Octal (0)
Integer Literal oct = 017; — Java supports all 4!
double d =
3.14;<br/>float f =
Floating-point Literal 2.5f; Default is double. Use 'f' suffix for float.
char c =
'A';<br/>char n = Enclosed in single quotes. \n, \t are escape
Character Literal '\n'; sequences.
String Literal String s = "Hello"; Enclosed in double quotes. Stored in String Pool.
4.2 Variables
■ Formal Definition
A variable is a named memory location that stores a value which can change during program execution. In
Java, every variable must be declared with a data type before use. Variables have a name (identifier), a
type, and a value.
4.3 Comments
// 1. Single-line Comment — anything after // is ignored by compiler
int x = 10; // This is a single-line comment
/**
*/
4.4 Separators
Separators are symbols that define the structure of the code:
Separator Usage
Scope refers to the region of a program where a variable is accessible (can be read or written). Lifetime
refers to the duration for which a variable exists in memory. These two concepts are closely linked — a
variable is allocated memory when it comes into scope and is destroyed (garbage collected or popped from
stack) when it goes out of scope.
class ScopeDemo {
static int classVar = 100; // CLASS scope — exists as long as class is loaded
void method() {
int localVar = 10; // METHOD scope — exists only during method execution
[Link](blockVar); // OK
[Link](localVar); // OK
■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■
■ ■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■ ■
■ ■ ■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■ ■ ■
■ ■ ■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■ ■ ■
■ ■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■ ■
■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■ Exam Trap
Local variables in Java do NOT get default values. If you declare int x; inside a method and use it without
initializing, you get a compile-time error: 'variable x might not have been initialized'.
■ EXAM Q: Explain variable scope with a code example showing all three types.
6. Data Types in Java
■ Formal Definition
A data type in Java specifies the type of data that a variable can store, the size of memory it occupies, and
the range of valid values it can hold. Java is a statically typed language — every variable must be declared
with a type before use. Data types are divided into two categories: Primitive (8 types) and
Non-Primitive/Reference (objects, arrays, strings).
8 bits
byte 1 byte -128 to 127 0 byte b = 100;
16 bits
short 2 bytes -32,768 to 32,767 0 short s = 1000;
32 bits
int 4 bytes -2,147,483,648 to 2,147,483,647 0 int i = 42;
64 bits
long 8 bytes -9.2×10^18 to 9.2×10^18 0L long l = 100L;
32 bits
float 4 bytes ~6-7 decimal digits 0.0f float f = 3.14f;
64 bits
double 8 bytes ~15-16 decimal digits 0.0d double d = 3.14;
16 bits
char 2 bytes 0 to 65,535 (Unicode) '\u0000' char c = 'A';
boolean ok =
boolean 1 bit true or false false true;
■ Memory Trick
'B S I L F D C B' → 'Big Students In Lab Find Data Crunching Boring' (byte, short, int, long, float, double, char,
boolean)
[Link]((int)'A'); // Output: 65
■■ Common Mistake
■ EXAM Q: List all 8 primitive data types with their sizes and ranges.
■ EXAM Q: What is the difference between float and double?
7. Type Conversion and Casting
■ Formal Definition
Type Conversion is the process of converting a value from one data type to another. In Java, there are two
kinds: Widening Conversion (automatic/implicit, from smaller to larger type, no data loss) and Narrowing
Conversion (explicit/casting, from larger to smaller type, possible data loss).
byte b = 10;
double d = 9.99;
[Link](i); // Output: 9
[Link](small); // Output: 44
// double to float
double pi = 3.141592653589793;
float pf = (float) pi; // precision lost
44
3.1415927
(int)3.99 gives 3, NOT 4. Casting truncates (chops off) the decimal part. To round, use [Link]().
■ EXAM Q: What is the difference between widening and narrowing conversion? Give examples.
■ EXAM Q: What is the output of: int x = (int)3.99; [Link](x);
8. Operators in Java
■ Formal Definition
An operator is a special symbol or keyword that instructs the compiler to perform a specific mathematical,
logical, bitwise, or relational operation on one or more operands (values/variables) and produce a result.
Java provides a rich set of operators organized into distinct categories.
* Multiplication 5 * 3 = 15 —
Modulus
% (Remainder) 5 % 3 = 2 Useful for even/odd check: if(n%2==0)
int a = 5;
[Link](a); // Output: 6
int b = 5;
[Link](b); // Output: 6
5 | 3 = 7 (101 |
| Bitwise OR At least one bit is 1 → result is 1 011 = 111)
5 ^ 3 = 6 (101 ^
^ Bitwise XOR Bits differ → result is 1 011 = 110)
~5 = -6 (flips all
Bitwise NOT bits + two's
~ (complement) Inverts all bits complement)
5 << 1 = 10
<< Left Shift Shifts bits left, fills 0 on right (equivalent to ×2)
20 >> 2 = 5
>> Right Shift (signed) Shifts bits right, preserves sign bit (equivalent to ÷4)
>>> Unsigned Right Shift Shifts right, fills 0 (ignores sign) -1 >>> 28 = 15
-6
10
^ Boolean XOR true if operands are DIFFERENT true ^ false = true; true ^ true = false
int x = 10;
boolean result = (x > 5) || (++x > 0); // right side NOT evaluated!
boolean r2 = (x < 5) && (++x > 0); // right side NOT evaluated!
10
10
&= Bitwise AND and assign x &= 0xFF Same as x = x & 0xFF
Operator Precedence defines the order in which operators are evaluated in an expression that has multiple
operators. Higher precedence operators are evaluated first. When two operators have the same
precedence, associativity determines the order (left-to-right or right-to-left).
= += -= *= /= %= &= ^= |= <<=
14 (Lowest) Assignment >>= >>>= Right → Left
int result = 2 + 3 * 4 - 1;
// 3 * 4 = 12
// Step 2: left-to-right: 2 + 12 = 14
// Step 3: 14 - 1 = 13
[Link](result); // Output: 13
// Another example:
int x = 10, y = 5, z = 2;
13
true
■ Memory Trick
P-U-M-A-S-R-E-B-X-O-A-O-T-A → 'Postfix Unary Mult Add Shift Relational Equality Bitwise(&) Xor bitOr
And(&&) Or(||) Ternary Assignment'
// ============================================================
// ============================================================
JavaUnit1Demo(int val) {
short s = 32000;
long l = 9_000_000_000L;
float f = 3.14f;
double d = 3.141592653589793;
char c = 'J';
boolean ok = true;
int x = 42;
// Narrowing (explicit)
double pi = 3.99;
int a = 10, b = 3;
// Arithmetic
// Bitwise
// Relational
int counter = 0;
// Ternary operator
// Compound assignment
a += 5;
dataTypesDemo();
castingDemo();
operatorsDemo();
[Link] = 777;
char=J boolean=true
After a += 5: a = 15
[Link] = 500
[Link] = 999
[Link] = 777
11. Common Mistakes & Exam Traps
Trap 1: Using = instead of == if (x = 5) — COMPILE ERROR in Java (unlike C, where it runs silently)
Trap 2: Integer division truncation int x = 7/2; → x = 3, NOT 3.5. Use double d = 7.0/2;
Trap 4: float needs 'f' suffix float f = 3.14; → ERROR. Must be float f = 3.14f;
Trap 5: long needs 'L' suffix long l = 9999999999; → ERROR. Must be 9999999999L
Trap 8: ++ pre vs post int a=5; int b=a++; → b=5, a=6. int c=++a; → a=7, c=7.
Trap 9: Short-circuit &&/|| If left side of && is false, right side is NEVER evaluated.
Trap 10: boolean ≠ integer Java boolean cannot be used as int. if(1){} → COMPILE ERROR.
Trap 11: char is unsigned char range is 0–65535, not negative. It's actually uint16 internally.
Trap 12: Multiple inheritance Java classes cannot extend multiple classes. Use interfaces instead.
12. Top Exam Questions — Unit 1
2-Mark Questions
Q1. Define Java. What does WORA mean?
Q2. What is a JVM? What is its role?
Q3. Differentiate between JDK, JRE, and JVM.
Q4. List any 5 features of Java.
Q5. What is an object? What are its three characteristics?
Q6. What is the difference between a class and an object?
Q7. Define encapsulation with an example.
Q8. What is the default value of int, float, boolean, char?
Q9. What is a literal? Give 3 types with examples.
Q10. What is type casting? Give one example.
Q11. What is the difference between == and = operators?
Q12. What is the difference between & and && operators?
Q13. What is operator precedence? Give an example.
Q14. What is the scope of a local variable?
Q15. Define widening and narrowing conversion.
5-Mark Questions
Q1. Explain the history and evolution of Java with a timeline.
Q2. Explain all 4 pillars of OOP with real-life examples and Java code.
Q3. Write a Java program to demonstrate all 8 primitive data types.
Q4. Explain the difference between method overloading and method overriding with examples.
Q5. Describe all types of variables in Java with scope and lifetime.
Q6. Explain all arithmetic operators with examples and a working Java program.
Q7. Describe all bitwise operators with truth tables and examples.
Q8. Explain type conversion (widening and narrowing) with examples and potential issues.
Q9. Write a note on comments and separators in Java with examples.
Q10. Explain operator precedence with a table and worked example.
DRV = Developer Runs Virtually. JDK for Developing, JRE for Running, JVM for
JDK ⊃ JRE ⊃ JVM Virtualizing.
'B S I L F D C B' → 'Big Students In Lab Find Data Crunching Boring' (byte, short, int,
8 Primitive Types long, float, double, char, boolean)
Pre vs Post ++ 'POST = Print THEN tick. PRE = tick THEN Print.' (tick = increment)
Short-circuit 'Short means STOP early.' && stops on first FALSE. || stops on first TRUE.
Float vs Double F = Fewer digits (6-7). D = Double the digits (15-16). Always double for precision.
■ All the best for your exams! Revise this sheet 24 hours before the exam.
Focus: OOP pillars + Data types table + Operator precedence + Type casting rules + Pre/Post increment.