Unit 1: Introduction to Java Programming – Detailed Notes with Programs
1. Java Architecture – JVM
Java consists of JDK, JRE, and JVM. JVM executes bytecode and makes Java platform independent.
Example Program:
public class Hello {
public static void main(String[] args) {
[Link]("Java Program Running!");
}
}
2. Keywords
Keywords are reserved words with fixed meaning.
Example Program:
public class KeywordsExample {
public static void main(String[] args) {
int x = 10;
if (x > 5) {
[Link]("x is greater than 5");
}
}
}
3. Features of Java
Simple, Object-Oriented, Platform Independent, Secure, Robust, Multithreaded.
Example:
class Student {
String name;
}
public class FeatureExample {
public static void main(String[] args) {
Student s = new Student();
[Link] = "Amit";
[Link]([Link]);
}
}
4. Console Input/Output
Input uses Scanner; output uses println.
Example:
import [Link];
public class InputOutputExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter age: ");
int age = [Link]();
[Link]("Age: " + age);
}
}
5. Variables and Identifiers
Variables store values; identifiers are names.
Example:
public class VariablesExample {
public static void main(String[] args) {
int age = 20;
String name = "Amit";
[Link](name + " is " + age);
}
}
6. Scope of Variables
Local, Instance, and Static scope.
Example:
public class ScopeExample {
int a = 5;
public void show() {
int b = 10;
[Link](a + b);
}
public static void main(String[] args) {
new ScopeExample().show();
}
}
7. Data Types
Primitive and non-primitive.
Example:
public class DataTypeExample {
public static void main(String[] args) {
int x = 10;
double y = 5.5;
char ch = 'A';
boolean flag = true;
[Link](x + ", " + y + ", " + ch + ", " + flag);
}
}
8. Type Conversion
Implicit and explicit.
Example:
public class ImplicitConvert {
public static void main(String[] args) {
int a = 10;
double b = a;
[Link](b);
}
}
public class ExplicitConvert {
public static void main(String[] args) {
double d = 10.7;
int a = (int)d;
[Link](a);
}
}
9. Access Modifiers
public, private, protected, default.
Example:
class Box {
public int h = 10;
private int w = 5;
public int getW() { return w; }
}
public class AccessExample {
public static void main(String[] args) {
Box b = new Box();
[Link](b.h);
[Link]([Link]());
}
}
10. Operators
Arithmetic, Logical, Relational.
Example:
public class OperatorExample {
public static void main(String[] args) {
int a = 10, b = 3;
[Link](a + b);
[Link](a > b);
}
}
11. If Statement
Example:
public class IfExample {
public static void main(String[] args) {
int age = 20;
if (age >= 18) [Link]("Adult");
}
}
12. If-else Statement
Example:
public class IfElseExample {
public static void main(String[] args) {
int marks = 40;
if (marks >= 35) [Link]("Pass");
else [Link]("Fail");
}
}
13. Switch Statement
Example:
public class SwitchExample {
public static void main(String[] args) {
int day = 2;
switch(day) {
case 1: [Link]("Mon"); break;
case 2: [Link]("Tue"); break;
default: [Link]("Invalid");
}
}
}