25th OCT JAVA NOTES
📜 1. History of Java
Java was developed by James Gosling and his team at Sun Microsystems in 1991.
It was originally called Oak, later renamed to Java in 1995.
The goal was to create a language that could run on multiple devices without modification.
In 2010, Oracle Corporation acquired Sun Microsystems and now maintains Java.
⚙️2. Java uses Compiler vs Interpreter
Java uses both compiler and interpreter:
o Compiler (javac): Converts Java source code (.java) into bytecode (.class file).
o Interpreter (JVM): Executes the bytecode line by line using the Java Virtual
Machine.
💾 3. Bytecode
Bytecode is an intermediate code generated after compilation.
It is platform-independent and can be executed on any machine having a JVM.
Example:
Source file → [Link]
After compilation → [Link] (contains bytecode)
📝 4. Run Java Code using Notepad
1. Write your Java program in Notepad and save it as [Link].
2. Open Command Prompt, go to the folder where file is saved:
3. cd path_of_your_file
4. Compile the program:
5. javac [Link]
6. Run the program:
7. java Hello
🌍 5. Why Java is Platform Independent
Java code is compiled into bytecode.
This bytecode runs on any system with a JVM, regardless of hardware or OS.
Hence, Java is called “Write Once, Run Anywhere (WORA)”.
✅ 6. Advantages of Java
1. Platform Independent
2. Object-Oriented
3. Robust and Secure
4. Simple and Easy to Learn
5. Multithreaded
6. High Performance (JIT Compiler)
7. Automatic Memory Management (Garbage Collection)
🚫 7. Why Java is Not a Pure Object-Oriented Language
Java supports primitive data types (int, char, boolean, etc.) which are not objects.
Hence, it’s not 100% object-oriented.
💬 8. Sending Command Line Arguments in Java
class Test {
public static void main(String[] args) {
[Link]("First argument: " + args[0]);
Run using:
java Test Hello
Output:
First argument: Hello
🔢 9. Primitive Data Types in Java
Type Size (Bytes) Range
byte 1 -128 to 127
short 2 -32,768 to 32,767
int 4 -2,147,483,648 to 2,147,483,647
Type Size (Bytes) Range
long 8 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 ~3.4e−038 to 3.4e+038
double 8 ~1.7e−308 to 1.7e+308
char 2 0 to 65,535 (Unicode)
boolean 1 bit true / false
🔄 10. Type Casting
a) Implicit (Widening)
Automatically done by compiler:
int a = 10;
double b = a; // int to double
b) Explicit (Narrowing)
Manually converting:
double x = 9.78;
int y = (int)x; // double to int
🔠 11. ASCII Code
ASCII (American Standard Code for Information Interchange) represents characters as
numbers.
Example:
o 'A' = 65
o 'a' = 97
o '0' = 48
char ch = 'A';
[Link]((int)ch); // prints 65
✨ 12. Escape Sequences in Java
Sequence Meaning
\n New line
Sequence Meaning
\t Tab space
\" Double quote
\\ Backslash
\' Single quote
Example:
[Link]("Hello\nWorld");
🔁 13. Working with if and else
int age = 20;
if(age >= 18)
[Link]("Eligible to vote");
else
[Link]("Not eligible");
🔄 14. Else If Ladder
int marks = 75;
if(marks >= 90)
[Link]("Grade A");
else if(marks >= 75)
[Link]("Grade B");
else
[Link]("Grade C");
🧩 15. Nested if
int a = 10, b = 20, c = 5;
if(a > b) {
if(a > c)
[Link]("a is largest");
}
🔁 16. Loops in Java
Types:
1. for loop
2. while loop
3. do-while loop
4. for-each loop
Example:
for(int i=1; i<=5; i++)
[Link](i);
🔄 17. Working with While Loop
int i = 1;
while(i <= 5) {
[Link](i);
i++;
🛑 18. Break Keyword
Used to exit a loop prematurely.
for(int i=1; i<=10; i++){
if(i == 5)
break;
[Link](i);
🔁 19. Continue Keyword
Used to skip the current iteration.
for(int i=1; i<=5; i++){
if(i == 3)
continue;
[Link](i);
💡 20. Java Program to Swap Two Variables
✅ Using Temporary Variable
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
[Link]("a=" + a + " b=" + b);
✅ Without Temporary Variable
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
[Link]("a=" + a + " b=" + b);
✅ Using XOR
int a = 5, b = 10;
a = a ^ b;
b = a ^ b;
a = a ^ b;
[Link]("a=" + a + " b=" + b);