JAVA NOTES FOR DSSSB TGT COMPUTER
SCIENCE EXAM
PART 1: JAVA FUNDAMENTALS, JVM, DATA TYPES,
OPERATORS AND BASIC PROGRAMS
1. Introduction to Java
Java is a high-level, object-oriented, class-based programming language developed by Sun
Microsystems.
Java was created by:
James Gosling
Year:
1995
Currently maintained by:
Oracle Corporation
2. History of Java
Java was originally called:
Oak
Reason:
It was developed for consumer electronic devices.
Later renamed:
Java
The first public version:
Java 1.0 (1996)
3. Java Design Goals
Java was designed with the following goals:
1. Simple
2. Object-oriented
3. Platform independent
4. Secure
5. Robust
6. Portable
7. Multithreaded
8. Distributed
9. High performance
4. Features of Java (WORA)
1. Platform Independent
Java follows:
Write Once, Run Anywhere (WORA)
A Java program is compiled into:
Bytecode
Bytecode runs on:
JVM (Java Virtual Machine)
Flow:
Java Source Code (.java)
Java Compiler (javac)
Bytecode (.class)
JVM
Machine Code
5. Java Features
1. Object-Oriented
Java is based on objects and classes.
Main OOP concepts:
• Encapsulation
• Inheritance
• Polymorphism
• Abstraction
2. Simple
Java removed complex features of C++:
• Pointer arithmetic
• Multiple inheritance through classes
• Operator overloading
3. Secure
Java provides security through:
• Bytecode verification
• No direct memory access
• Exception handling
4. Robust
Java is robust because of:
• Strong memory management
• Exception handling
• Automatic garbage collection
5. Multithreaded
Java supports execution of multiple tasks simultaneously.
Example:
• Downloading a file
• Playing music
6. Portable
Java programs can run on different platforms because of JVM.
6. Java Platform Components
Java platform consists of:
1. JDK
2. JRE
3. JVM
7. JDK (Java Development Kit)
JDK is used for:
• Developing Java programs
• Compiling programs
• Debugging
Contains:
• JRE
• Compiler
• Debugger
• Documentation tools
Diagram:
JDK
|
|-- JRE
|
|-- JVM
8. JRE (Java Runtime Environment)
JRE provides environment to run Java programs.
Contains:
• JVM
• Java class libraries
Does NOT contain compiler.
9. JVM (Java Virtual Machine)
JVM executes Java bytecode.
It converts:
Bytecode → Machine Code
10. JVM Components
JVM consists of:
1. Class Loader
2. Runtime Memory Area
3. Execution Engine
4. Native Method Interface
11. Class Loader
Class loader loads .class files into memory.
Functions:
• Loading
• Linking
• Initialization
12. Runtime Memory Areas
JVM memory is divided into:
1. Method Area
Stores:
• Class information
• Static variables
• Method code
2. Heap
Stores:
• Objects
• Instance variables
Example:
Student s = new Student();
Object is stored in heap.
3. Stack
Stores:
• Local variables
• Method calls
• References
Example:
int x = 10;
Variable x is stored in stack.
4. Program Counter Register
Stores address of current instruction.
5. Native Method Stack
Stores native method information.
13. Execution Engine
Responsible for executing bytecode.
Components:
Interpreter
Executes bytecode line by line.
Advantage:
• Faster startup
Disadvantage:
• Slower execution
JIT Compiler
Full Form:
Just In Time Compiler
JIT (Just-In-Time) Compiler is a part of the Java Virtual Machine (JVM) that improves the
performance of Java programs by converting bytecode into native machine code at runtime.
Converts frequently executed bytecode into machine code.
Advantage:
• Improves performance
14. Garbage Collector
Garbage collection automatically removes unused objects from memory.
Example:
Student s = new Student();
s = null;
The object becomes eligible for garbage collection.
15. Java Program Structure
Example:
class Hello
{
public static void main(String args[])
{
[Link]("Hello Java");
}
}
16. Explanation of Main Method
public static void main(String args[])
public
Accessible from anywhere.
static
Allows JVM to call main without creating object.
void
Does not return value.
main
Starting point of program execution.
String args[]
Stores command-line arguments.
17. Java Tokens
Smallest individual elements of Java program are called tokens.
Types:
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Separators
18. Keywords
Keywords are reserved words having predefined meanings.
Examples:
class
public
static
void
int
if
else
for
while
return
new
this
super
final
19. Identifiers
Names given to:
• Variables
• Classes
• Methods
Rules:
1. Cannot start with digit.
2. Cannot contain spaces.
3. Cannot be a keyword.
4. Java identifiers are case-sensitive.
Valid:
student
_marks
total1
Invalid:
1student
class
my name
20. Data Types in Java
Java data types are divided into:
1. Primitive
2. Non-primitive
21. Primitive Data Types
Java has 8 primitive data types.
Type Size Range
byte 1 byte -128 to 127
short 2 bytes -32768 to 32767
int 4 bytes -2³¹ to 2³¹-1
long 8 bytes Large values
float 4 bytes Decimal
double 8 bytes Large decimal
char 2 bytes Unicode character
boolean JVM dependent true/false
22. Integer Types
byte
Example:
byte a = 100;
Range:
-128 to 127
short
Example:
short b = 20000;
int
Default integer type.
Example:
int marks = 90;
long
Used for large numbers.
Example:
long population = 8000000000L;
L is compulsory for long literals.
23. Floating Point Types
float
Example:
float x = 5.6f;
f is required.
double
Default decimal type.
Example:
double pi = 3.14159;
24. Character Type
char stores a single Unicode character.
Example:
char grade='A';
Size:
2 bytes
25. Boolean Type
Stores:
true
false
Example:
boolean result=true;
26. Default Values of Data Types
Instance variables get default values.
Data Type Default Value
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
boolean false
Reference null
Local variables do NOT get default values.
27. Type Casting
Conversion of one data type into another.
Types:
1. Widening
2. Narrowing
28. Widening Casting
Smaller type → Larger type
Automatic conversion.
Example:
int a=10;
double b=a;
Output:
10.0
Order:
byte
↓
short
↓
int
↓
long
↓
float
↓
double
29. Narrowing Casting
Larger type → Smaller type
Manual conversion.
Example:
double x=10.5;
int y=(int)x;
[Link](y);
Output:
10
Decimal part removed.
30. Operators in Java
Types:
1. Arithmetic
2. Relational
3. Logical
4. Assignment
5. Unary
6. Bitwise
7. Conditional
31. Arithmetic Operators
Operators:
+
-
*
/
%
Example:
int a=10;
int b=3;
[Link](a/b);
Output:
3
Because integer division removes decimal.
32. Important Output Questions
Example 1
class Test
{
public static void main(String args[])
{
int a=5;
int b=2;
[Link](a/b);
}
}
Output:
2
Example 2
class Test
{
public static void main(String args[])
{
double a=5;
double b=2;
[Link](a/b);
}
}
Output:
2.5
33. Increment Operators
Operators:
++
--
Pre Increment
Value increases first.
Example:
int a=5;
[Link](++a);
Output:
6
Post Increment
Value used first, increased later.
Example:
int a=5;
[Link](a++);
[Link](a);
Output:
5
6
34. Relational Operators
Used for comparison.
Operators:
>
<
>=
<=
==
!=
Return:
boolean value
Example:
[Link](5>3);
Output:
true
35. Logical Operators
Operators:
&&
||
!
Example:
[Link](5>3 && 4>2);
Output:
true
36. Assignment Operators
Examples:
=
+=
-=
*=
/=
Example:
int a=10;
a+=5;
[Link](a);
Output:
15
37. Conditional Operator
Syntax:
condition ? expression1 : expression2
Example:
int a=10,b=20;
int max=(a>b)?a:b;
[Link](max);
Output:
20
DSSSB TGT Quick Revision
1. Java developed by James Gosling.
2. Java released in 1995.
3. Original name was Oak.
4. Java follows WORA principle.
5. Java source file extension = .java
6. Bytecode file extension = .class
7. JVM executes bytecode.
8. JDK contains JRE.
9. JRE contains JVM.
[Link]() method is entry point.
[Link] size = 4 bytes.
[Link] size = 2 bytes.
[Link] is not a primitive data type.
[Link] collector removes unused objects.
[Link] improves execution speed.
End of Java Part 1