Java Programming As a Beginner

HimanshiHimanshi
3 min read

Here are basic but detailed notes for Java, including description, syntax, examples, and output/results. These are beginner-friendly and essential for understanding Java fundamentals.


๐ŸŒŸ 1. Introduction to Java

  • Java is a high-level, class-based, object-oriented programming language developed by James Gosling at Sun Microsystems in 1995.

  • It follows the WORA principle: Write Once, Run Anywhere using the Java Virtual Machine (JVM).


๐Ÿ“Œ 2. Basic Java Structure

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}

โœ… Output:

Hello, Java!

๐Ÿงฑ 3. Data Types

TypeDescriptionExample
intInteger valuesint x = 10;
floatDecimal (4 bytes)float y = 3.14f;
doubleLarger decimaldouble z = 9.81;
charSingle characterchar c = 'A';
booleanTrue/falseboolean b = true;
StringSequence of charsString s = "Hi";

๐Ÿงฎ 4. Variables and Operators

int a = 5;
int b = 3;
int sum = a + b;
System.out.println("Sum is: " + sum);

โœ… Output:

Sum is: 8

๐Ÿ” 5. Conditional Statements

int age = 18;
if (age >= 18) {
    System.out.println("Adult");
} else {
    System.out.println("Minor");
}

โœ… Output:

Adult

๐Ÿ”„ 6. Loops

โžค For Loop

for (int i = 1; i <= 5; i++) {
    System.out.println("Number: " + i);
}

โœ… Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

โžค While Loop

int i = 1;
while (i <= 3) {
    System.out.println("i = " + i);
    i++;
}

๐Ÿ“ฆ 7. Arrays

int[] nums = {10, 20, 30};
System.out.println(nums[1]);

โœ… Output:

20

๐Ÿงฐ 8. Methods (Functions)

public class Main {
    static void greet() {
        System.out.println("Hello from a method!");
    }

    public static void main(String[] args) {
        greet();
    }
}

โœ… Output:

Hello from a method!

๐Ÿงฑ 9. Object-Oriented Programming

โžค Class and Object

class Car {
    String model = "Tesla";
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();
        System.out.println(myCar.model);
    }
}

โœ… Output:

Tesla

๐Ÿงฉ 10. Inheritance

class Animal {
    void sound() {
        System.out.println("Animal sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.sound();
    }
}

โœ… Output:

Dog barks

1
Subscribe to my newsletter

Read articles from Himanshi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Himanshi
Himanshi