☕Java Programming
Java Basics
Java syntax, data types, operators, control flow, arrays, and methods.
BeginnerIntermediateAdvanced
What You Will Learn in Java Basics
Java is a strongly typed, object-oriented, platform-independent programming language that compiles to bytecode and runs on the JVM.
- Java source (.java) → compiled to bytecode (.class) → run by JVM (Write Once, Run Anywhere).
- Every Java program has exactly one public class matching the filename and a main method as entry point.
- Primitive types: byte, short, int, long, float, double, char, boolean.
- Java is pass-by-value; object references are copied, not the objects themselves.
- Wrapper classes (Integer, Double) box primitives for use in Collections.
- String is immutable in Java; use StringBuilder for mutable string operations.
Syntax
public class ClassName {
// fields
dataType variableName;
// main method — entry point
public static void main(String[] args) {
// statements
}
}Complete Code Example
public class HelloWorld {
public static void main(String[] args) {
int age = 20;
double gpa = 8.5;
String name = "Rahul";
System.out.println("Name: " + name + ", Age: " + age + ", GPA: " + gpa);
}
}
// Output: Name: Rahul, Age: 20, GPA: 8.5Example
A variable `int marks = 95;` declares an integer storing a student's marks.
Expected Exam Questions — Java Basics
Q1.What is JVM and why is Java platform-independent?
Answer: JVM (Java Virtual Machine) is an abstract machine that executes Java bytecode. Java source code compiles to bytecode (.class), which is platform-independent. Any OS with a JVM installation can run the same bytecode, making Java platform-independent.
Q2.What is the difference between JDK, JRE, and JVM?
Answer: JVM executes bytecode. JRE = JVM + class libraries (needed to run Java programs). JDK = JRE + compiler (javac) + developer tools (needed to write and compile Java programs).
Q3.What are primitive data types in Java?
Answer: There are 8: byte (8-bit), short (16-bit), int (32-bit), long (64-bit), float (32-bit decimal), double (64-bit decimal), char (16-bit Unicode), boolean (true/false).
Q4.What is the difference between `==` and `.equals()` in Java?
Answer: `==` compares object references (memory address) for non-primitives, or actual values for primitives. `.equals()` compares object content. For Strings: `"a" == "a"` may be true due to string pool, but `new String("a") == new String("a")` is false. Always use `.equals()` for String comparison.
Q5.What is type casting in Java?
Answer: Converting one data type to another. Widening (implicit): int → long → double, no data loss. Narrowing (explicit): double → int, possible data loss, requires cast operator: `int x = (int) 3.14; // x = 3`.
Q6.What is the use of `final` keyword?
Answer: `final` variable: value cannot be changed (constant). `final` method: cannot be overridden. `final` class: cannot be extended (e.g., String class is final).
🔘 MCQ Practice — Java Basics
MCQ 1.Which of the following is NOT a valid Java identifier?
A. _count
B. 2ndValue ✓
C. myVar
D. $price
✓ Correct Answer: 2ndValue
MCQ 2.What is the default value of an `int` instance variable in Java?
A. null
B. 0 ✓
C. undefined
D. -1
✓ Correct Answer: 0
MCQ 3.Which memory area stores Java String literals?
A. Stack
B. Heap
C. String Constant Pool ✓
D. Method Area
✓ Correct Answer: String Constant Pool
Download Java Basics PDF Notes
Get the complete Java Basics notes as a PDF — free for enrolled students, or browse our public study materials library.