The Idea will be to know the very basic thing to know in java. All I remember is system.print() and that it’s a object oriented language.
***So Today let’s learn the foundations
- How the language works :
- Data types :
- Variables and operators :
- Input and Output :
- Conditionals :
- Loops :
- How To Run A Simple Java program
How the Java works
Java works through a combination of compilation and interpretation, following the Write Once, Run Anywhere (WORA) philosophy. Here’s a concise breakdown of how it works:
1. Source Code Compilation
You write Java code in .java files using the Java programming language.
2. Bytecode Generation
The Java Compiler (javac) compiles .java files into Bytecode, stored in .class files. Bytecode is an intermediate, platform-independent code.
3. Execution by JVM
The Java Virtual Machine JVM executes the bytecode. The JVM is platform-specific but runs the same bytecode on any system.
4. Just-In-Time (JIT) Compilation
To improve performance, the JVM uses a Jit Compiler to convert frequently-used bytecode into native machine code at runtime.
5. Memory Management
Java has automatic garbage collection — unused objects are automatically cleared from memory by the JVM.
In Short:
- You write Java → Compile to bytecode → Run on JVM → JIT-compiled to native code.
Let me know if you want a visual diagram or deeper insight into JVM internals.
Data types
| Type | Description |
|---|---|
| Primitive | Built-in types (8 total) |
| Reference | Objects, arrays, collections |
Primitive Types
Java has 8 primitive data types, categorised into integers, floating-point numbers, characters, and booleans.
Integer Types
byte
- Size: 8 bits
- Range: -128 to 127
- Use case: Memory-efficient storage, file IO
byte b = 100;short
-
Size: 16 bits
-
Range: -32,768 to 32,767
-
Use case: Space-saving in large arrays
short s = 1000;int
- Size: 32 bits
- Range: ~ -2.1 billion to +2.1 billion
- Use case: Default integer type
int i = 123456;long
- Size: 64 bits
- Range: Massive (±9 quintillion)
- Use case: IDs, timestamps, big counters
long l = 9876543210L; // note the 'L' suffixFloating-Point Types
float
- Size: 32 bits
- Precision: ~6–7 decimal digits
- Use case: Less precise, smaller memory
float f = 3.14f; // must use 'f' suffixdouble
- Size: 64 bits
- Precision: ~15 decimal digits
- Use case: Default for decimal numbers
double d = 3.1415926535;Character Type
char
- Size: 16 bits (Unicode)
- Range: 0 to 65,535
- Use case: Storing characters, symbols
char c = 'A';
char symbol = '#';It’s actually an integer under the hood — can be cast to/from int.
Boolean Type
boolean
- Size: JVM-dependent (usually 1 bit logically)
- Values:
trueorfalse - Use case: Control flow, flags
boolean isActive = true;
boolean isValid = false;| Type | Size | Example | Notes |
|---|---|---|---|
byte | 8-bit | byte b = 10; | For small values or IO |
short | 16-bit | short s = 100; | Rarely used |
int | 32-bit | int x = 1000; | Default integer |
long | 64-bit | long l = 1L; | Use for large numbers |
float | 32-bit | float f = 3.5f; | Must use f suffix |
double | 64-bit | double d = 2.1; | Default for decimals |
char | 16-bit | char c = 'X'; | Stores Unicode characters |
boolean | 1-bit* | boolean b = true; | Only true or false |
Notes
- Primitives are not objects: No methods, no nulls
- Stored directly on the stack when used in methods
- Cannot be used in collections — use wrapper classes like
Integer,Double, etc. - Java uses type promotion automatically (e.g.,
byte + int = int)
Reference Types
Reference types are types that refer to objects stored on the heap.
Unlike primitive types (which store actual values directly), reference types store a reference (i.e., memory address) to the object.
Examples of Reference Types
| Type | Description | Example |
|---|---|---|
| Classes | Custom or built-in objects | String, User, Scanner |
| Arrays | Ordered collections of elements | int[] arr = new int[5]; |
| Interfaces | Reference to any class implementing it | Runnable, List, Map |
| Enums | Special classes with constant values | Day.MONDAY |
Reference Type Behaviour
| Feature | Description |
|---|---|
| Stored in Stack (ref) | The reference itself lives on the stack |
| Object in Heap | The actual object is on the heap |
| Default Value | null (if not initialized) |
| Garbage Collected | If no references exist, memory is reclaimed |
| Methods and Fields | You can access object methods/fields via dot notation |
Example: String Reference
String name = "Alice";nameis a reference variable- The string
"Alice"is stored on the heap nameholds a reference to that object
If you do:
name = null;Now the reference is gone — "Alice" is eligible for garbage collection.
Example: Array Reference
int[] nums = {1, 2, 3};-
numsis a reference variable (on the stack) -
The array
[1, 2, 3]is stored on the heap -
You access it via the reference
Example: Class Reference
User user = new User("John");userholds a reference to aUserobject in heap- Access with
user.getName()oruser.name
Reference Type Pitfalls
-
NullPointerException
- Trying to access a method or field on a
nullreference
String name = null; System.out.println(name.length()); // NPE - Trying to access a method or field on a
-
Shallow vs. Deep Copy
- Assigning one reference to another copies the reference, not the object
User a = new User("A"); User b = a; // both point to the same object -
Equality
==checks if two references point to the same object.equals()checks logical equality (if overridden)
Summary: Primitives vs. Reference Types
| Feature | Primitive Type | Reference Type |
|---|---|---|
| Stored Value | Actual value | Memory address (reference) |
| Memory | Stack | Stack (ref) + Heap (object) |
| Null Support | No | Yes |
| Default Value | 0, false, etc. | null |
| Object Methods | Not allowed | Yes (obj.toString(), etc.) |
✅ TL;DR
Reference types store pointers to objects on the heap.
They enable object-oriented features, dynamic data structures, and collections — and are at the core of how Java applications work.
Great — here’s a quick, clear summary of each of these core Java topics to get you running fast.
Variables and Operators
Variables
Used to store data. You must declare a type.
int x = 10;
String name = "Alice";
boolean isActive = true;Operators
| Type | Examples | Purpose |
|---|---|---|
| Arithmetic | +, -, *, /, % | Basic math |
| Assignment | =, +=, -=, etc. | Value assignment |
| Comparison | ==, !=, >, <, >=, <= | Returns boolean |
| Logical | &&, ` | |
| Unary | ++, -- | Increment/decrement |
| Ternary | ? : | Short if-else |
int a = 5, b = 3;
int sum = a + b; // 8
boolean isGreater = a > b; // trueInput and Output
Output (print to console)
System.out.println("Hello");
System.out.print("Same line");
System.out.printf("Formatted: %d\n", 42);Input (via Scanner)
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
String name = sc.nextLine();
System.out.print("Enter age: ");
int age = sc.nextInt();Conditionals
if, else if, else
if (age >= 18) {
System.out.println("Adult");
} else if (age > 13) {
System.out.println("Teen");
} else {
System.out.println("Child");
}switch
int day = 3;
switch (day) {
case 1: System.out.println("Mon"); break;
case 2: System.out.println("Tue"); break;
default: System.out.println("Other");
}Loops
for loop
for (int i = 0; i < 5; i++) {
System.out.println(i);
}while loop
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}do-while loop
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);for-each (arrays, collections)
int[] nums = {1, 2, 3};
for (int n : nums) {
System.out.println(n);
}✅ TL;DR
| Feature | Example |
|---|---|
| Variables | int x = 5; |
| Operators | x + y, a > b, a && b |
| Input | Scanner sc = new Scanner(System.in); |
| Output | System.out.println("Hi"); |
| Conditionals | if/else, switch |
| Loops | for, while, do-while, for-each |
How to Run A Java Program
Make the File
touch Hello.javapublic class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
The class name must match the file name
Compile the Program
javac Hello.java***==This will make the Bytecode file of the Hello.java
Run The Program
java HelloOutput
Hello, world!Behind the Scenes
| Step | What Happens |
|---|---|
javac | Compiles .java → .class (bytecode) |
java | Starts JVM and runs bytecode |
🔟 Java Coding Problems for Practice
1. Sum of Two Numbers
- Input two integers from the user.
- Print their sum.
Input: 5, 7 → Output: 122. Even or Odd
-
Input a number.
-
Print if it’s even or odd using conditionals.
3. Find Maximum of Three Numbers
-
Input 3 integers.
-
Use
if-elseorMath.max()to find the largest.
4. Simple Calculator
-
Input two numbers and an operator (
+,-,*,/). -
Use
switchto perform the operation.
5. Check Prime Number
-
Input a number.
-
Use a loop to check if it’s a prime number.
6. Reverse a Number
-
Input an integer.
-
Reverse it using a loop.
Input: 123 → Output: 3217. Sum of Digits
-
Input a number.
-
Use a loop to calculate the sum of its digits.
Input: 234 → Output: 2 + 3 + 4 = 98. Fibonacci Series (n terms)
-
Input
n. -
Print first
nterms of Fibonacci using a loop.
Input: 5 → Output: 0 1 1 2 39. Find the Largest Element in an Array
-
Input
nnumbers into an array. -
Use a loop to find the max value.
10. Count Vowels in a String
-
Input a string.
-
Count how many vowels (
a, e, i, o, u) it has.