1.1 History and Philosophy of Java
History
- Developed by Sun Microsystems
- James Gosling is known as father of Java
- Originally called Oak
- Later renamed Java
Philosophy / Features of Java
| Feature | Meaning |
|---|---|
| Simple | easy syntax |
| Object Oriented | based on objects/classes |
| Platform Independent | runs anywhere |
| Secure | no pointer access |
| Robust | strong memory management |
| Multithreaded | multiple tasks simultaneously |
| Distributed | network support |
| Portable | machine independent |
| Dynamic | runtime loading |
1.2 Object Oriented Programming (OOP)
OOP Principles
1. Class
Blueprint/template.
class Car{}
2. Object
Real instance of class.
Car c = new Car();
3. Encapsulation
Wrapping data and methods together.
class A{
private int x;
}
4. Inheritance
Acquire properties from parent class.
class B extends A{}
5. Polymorphism
One thing many forms.
A obj = new B();
6. Abstraction
Hide implementation details.
Using:
- abstract class
- interface
Advantages of OOP
- Code reusability
- Security
- Easy maintenance
- Real-world modeling
- Flexibility
1.3 Java Development Kit (JDK)
JDK Components
| Component | Work |
|---|---|
| javac | compiler |
| java | interpreter |
| javadoc | documentation |
| JVM | executes bytecode |
JVM, JRE, JDK Difference
| JVM | JRE | JDK |
|---|---|---|
| Executes bytecode | JVM + libraries | JRE + tools |
Java Program Execution Process
Source Code (.java)
↓
Compiler (javac)
↓
Bytecode (.class)
↓
JVM
↓
Output
1.4 First Simple Java Program
class Hello{
public static void main(String args[]){
System.out.println("Hello");
}
}
Important Keywords
| Keyword | Meaning |
|---|---|
| class | defines class |
| public | accessible everywhere |
| static | no object needed |
| void | returns nothing |
| main | starting point |
| String args[] | command line arguments |
Compilation
javac Hello.java
Execution
java Hello
1.5 Packages in Java
Package
Collection of related classes.
package mypack;
Types of Package
- Built-in package
- User-defined package
Important Packages
| Package | Use |
|---|---|
| java.lang | basic classes |
| java.util | Scanner, collections |
| java.io | file handling |
| java.sql | JDBC |
| javax.swing | GUI |
Import Statement
import java.util.Scanner;
1.6 Java Data Types
Primitive Data Types
| Type | Size | Example |
|---|---|---|
| byte | 8 bit | 10 |
| short | 16 bit | 20 |
| int | 32 bit | 100 |
| long | 64 bit | 1000L |
| float | 32 bit | 2.5f |
| double | 64 bit | 5.67 |
| char | 16 bit | 'A' |
| boolean | true/false | true |
Non Primitive Types
- String
- Array
- Class
- Interface
Integer Types
int x = 10;
long y = 1000L;
Floating Types
float a = 5.6f;
double b = 9.87;
Character Type
char ch = 'A';
ASCII/Unicode supported.
Boolean Type
boolean flag = true;
String
String s = "Java";
Important String Methods
| Method | Work |
|---|---|
| length() | size |
| charAt() | character |
| toUpperCase() | uppercase |
| equals() | compare |
Arrays
Declaration
int a[];
Creation
a = new int[5];
Initialization
int a[] = {1,2,3};
1.7 Literals
Fixed values in program.
10
'A'
true
"Hello"
Hexadecimal
int x = 0x12;
Octal
int x = 012;
Binary
int x = 0b1010;
Escape Sequences
| Escape | Meaning |
|---|---|
| \n | newline |
| \t | tab |
| \\ | backslash |
| \" | double quote |
String Literal
String s = "Hello";
Stored in string pool.
1.8 Variables and Constants
Variable
int x = 10;
Types of Variables
| Type | Meaning |
|---|---|
| local | inside method |
| instance | inside class |
| static | shared |
Constant
final int MAX = 100;
Cannot change.
1.9 Operators
Arithmetic Operators
+ - * / %
Unary Operators
++
--
Relational Operators
>
<
>=
<=
==
!=
Return boolean.
Logical Operators
&&
||
!
Bitwise Operators
&
|
^
~
<<
>>
Assignment Operators
=
+=
-=
*=
Ternary Operator
x>y ? x : y;
Operator Precedence
() highest
* / %
+ -
= lowest
1.10 Type Casting
Widening Casting
Automatic.
int x = 10;
double y = x;
Narrowing Casting
Manual.
double x = 9.8;
int y = (int)x;
1.11 Control Statements
if Statement
if(condition){
}
if else
if(a>b){
}
else{
}
else if Ladder
if(){
}
else if(){
}
else{
}
switch Statement
switch(choice){
case 1:
break;
default:
}
Loop Statements
for loop
for(int i=0;i<5;i++)
while loop
while(condition)
do while loop
do{
}while(condition);
Runs at least once.
break Statement
Terminates loop.
break;
continue Statement
Skips current iteration.
continue;
Important Practical Programs
- Sum of two numbers
- Odd/even check
- Largest among 3 numbers
- Factorial
- Fibonacci series
- Prime number
- Palindrome
- Array sorting
- Matrix addition
- String reverse
MOST IMPORTANT EXAM QUESTIONS
- Features of Java
- Difference between C++ and Java
- JVM vs JRE vs JDK
- Primitive vs non-primitive datatype
- Array vs String
- break vs continue
- while vs do while
- switch vs if else
Important Viva
Why Java is platform independent?
Because bytecode runs on JVM.
Why String is not primitive?
Because it is a class/object.
Difference between == and equals()
| == | equals() |
|---|---|
| compares reference | compares content |
Difference between compile time and runtime error
| Compile Time | Runtime |
|---|---|
| syntax error | occurs during execution |
0 comments:
Post a Comment