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

TypeDescription
PrimitiveBuilt-in types (8 total)
ReferenceObjects, 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' suffix

Floating-Point Types

float
  • Size: 32 bits
  • Precision: ~6–7 decimal digits
  • Use case: Less precise, smaller memory
float f = 3.14f; // must use 'f' suffix
double
  • 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: true or false
  • Use case: Control flow, flags
boolean isActive = true;
boolean isValid = false;

TypeSizeExampleNotes
byte8-bitbyte b = 10;For small values or IO
short16-bitshort s = 100;Rarely used
int32-bitint x = 1000;Default integer
long64-bitlong l = 1L;Use for large numbers
float32-bitfloat f = 3.5f;Must use f suffix
double64-bitdouble d = 2.1;Default for decimals
char16-bitchar c = 'X';Stores Unicode characters
boolean1-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

TypeDescriptionExample
ClassesCustom or built-in objectsString, User, Scanner
ArraysOrdered collections of elementsint[] arr = new int[5];
InterfacesReference to any class implementing itRunnable, List, Map
EnumsSpecial classes with constant valuesDay.MONDAY

Reference Type Behaviour

FeatureDescription
Stored in Stack (ref)The reference itself lives on the stack
Object in HeapThe actual object is on the heap
Default Valuenull (if not initialized)
Garbage CollectedIf no references exist, memory is reclaimed
Methods and FieldsYou can access object methods/fields via dot notation

Example: String Reference
String name = "Alice";
  • name is a reference variable
  • The string "Alice" is stored on the heap
  • name holds 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};
  • nums is 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");
  • user holds a reference to a User object in heap
  • Access with user.getName() or user.name

Reference Type Pitfalls

  1. NullPointerException

    • Trying to access a method or field on a null reference
    String name = null;
    System.out.println(name.length()); // NPE
  2. 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
  3. Equality

    • == checks if two references point to the same object
    • .equals() checks logical equality (if overridden)

Summary: Primitives vs. Reference Types

FeaturePrimitive TypeReference Type
Stored ValueActual valueMemory address (reference)
MemoryStackStack (ref) + Heap (object)
Null SupportNoYes
Default Value0, false, etc.null
Object MethodsNot allowedYes (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

TypeExamplesPurpose
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; // true

Input 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

FeatureExample
Variablesint x = 5;
Operatorsx + y, a > b, a && b
InputScanner sc = new Scanner(System.in);
OutputSystem.out.println("Hi");
Conditionalsif/else, switch
Loopsfor, while, do-while, for-each

How to Run A Java Program

Make the File

touch Hello.java
public 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 Hello

Output

Hello, world!

Behind the Scenes

StepWhat Happens
javacCompiles .java.class (bytecode)
javaStarts 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: 12

2. 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-else or Math.max() to find the largest.


4. Simple Calculator

  • Input two numbers and an operator (+, -, *, /).

  • Use switch to 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: 321

7. Sum of Digits

  • Input a number.

  • Use a loop to calculate the sum of its digits.

Input: 234 → Output: 2 + 3 + 4 = 9

8. Fibonacci Series (n terms)

  • Input n.

  • Print first n terms of Fibonacci using a loop.

Input: 5 → Output: 0 1 1 2 3

9. Find the Largest Element in an Array

  • Input n numbers 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.