Java Programming Notes - Part 1: Basics of Java
Programming
Features of Java
- Simple – Syntax is similar to C/C++, easy to learn.
- Object-Oriented – Everything is represented as an object (Encapsulation, Inheritance, Polymorp
- Platform Independent – "Write once, run anywhere" because of JVM.
- Secure – No explicit pointers, runs inside JVM sandbox.
- Robust – Strong memory management and exception handling.
- Multithreaded – Supports multiple threads for concurrent execution.
- Portable – Compiled code (bytecode) can run on any system.
- High Performance – Uses Just-In-Time (JIT) compiler.
JDK, JRE, JVM and Bytecode
- JDK (Java Development Kit): Tools to develop, compile, and run Java programs. Includes compile
- JRE (Java Runtime Environment): JVM + libraries, used to run programs (no compiler).
- JVM (Java Virtual Machine): Executes Java bytecode, provides platform independence, memory man
- Bytecode: Intermediate `.class` file generated after compilation, platform-independent.
Example: Compiling and Running Java
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Steps:
1. Compile → javac HelloWorld.java → produces HelloWorld.class (bytecode)
2. Run → java HelloWorld → executed by JVM
Platform Independence
- Source code (.java) is compiled into bytecode (.class).
- Bytecode can be executed on any system with JVM installed.
- Provides "Write Once, Run Anywhere" capability.
Variables, Constants, Naming Conventions
- Variables: Named storage for data (int age = 25;)
- Constants: Declared using final (final double PI = 3.14159;)
- Naming Conventions:
- Class → PascalCase (StudentDetails)
- Variable/Method → camelCase (studentName)
- Constant → UPPERCASE (MAX_VALUE)
Data Types
Primitive Data Types:
- byte (1 byte) → -128 to 127
- short (2 bytes) → -32,768 to 32,767
- int (4 bytes)
- long (8 bytes)
- float (4 bytes)
- double (8 bytes)
- char (2 bytes, Unicode)
- boolean (1 bit) → true/false
Non-Primitive Data Types:
- Strings, Arrays, Classes, Interfaces
Operators in Java
- Arithmetic: + - * / %
- Relational: == != > < >= <=
- Logical: && || !
- Assignment: = += -= *= /=
- Unary: ++ --
- Conditional (Ternary): ?:
- Bitwise: & | ^ << >> >>>
Example:
int result = 10 + 2 * 5; // result = 20 (not 60, * has higher precedence)
Type Conversion
Implicit (Widening):
int x = 10;
double y = x;
Explicit (Casting):
double a = 9.8;
int b = (int)a;
Reading Input from Console
import java.util.Scanner;
class InputExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hello, " + name);
}
}
Flow Control in Java
Conditional Statements:
if (x > 0) {
System.out.println("Positive");
} else if (x < 0) {
System.out.println("Negative");
} else {
System.out.println("Zero");
}
Switch Case:
int day = 3;
switch(day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Other day");
}
Loops:
for (int i=1; i<=5; i++) { System.out.println(i); }
int i = 1;
while (i <= 5) { System.out.println(i); i++; }
int j = 1;
do { System.out.println(j); j++; } while (j <= 5);