Java Programming for Beginners

Jul 5, 2024

Java Programming for Beginners 🚀

Introduction

  • Instructor: Farhan Hassin Choudhry
  • Platform: FreeCodeCamp
  • Tool Used: Replit (an online IDE)
  • Goal: Learn fundamental concepts of Java programming
  • Pre-requisites: None, everything will be demonstrated within the browser

Setting Up

  1. Replit Account: Create or log into your Replit account at www.replit.com.
  2. Creating a Java Project: Use the Create button to start a new Java Repl project. Name it descriptively, like HelloWorld.
  3. Interface: Adjust settings like layout to your preference (side-by-side or stacked).
  4. Running Code: Click the Run button to compile and execute Java code.

Hello World Program

  • Java Source Files: End with .java and compiled files end with .class.
  • Structure: A basic Java program has a class declaration, a main method declared as public static void main(String[] args).
  • Printing to Console: Use System.out.println("Hello, World!");.

Variables and Data Types

  • Types: Primitive (int, byte, short, long, float, double, boolean, char) and Non-Primitive/Reference (Strings, Arrays).
  • Declaration and Initialization: Variable type, name, and value assignment.
  • Example: int age = 27;
  • Default Values: Numeric types default to 0, boolean defaults to false.
  • Type Conversion: Implicit (smaller to larger type) and explicit (larger to smaller type).

Operators

Types of Operators

  1. Arithmetic Operators: +, -, *, /, %
  2. Assignment Operators: =, +=, -=, *=, /=
  3. Relational Operators: ==, !=, >, <, >=, <=
  4. Logical Operators: &&, ||, !
  5. Increment/Decrement Operators: ++, --

Examples

  • Example of Arithmetic Operators:
    int sum = 10 + 5; // 15
    

Loops

For Loop

  • Structure: for(initialization; condition; update)
  • Example: for(int i = 0; i < 10; i++) { System.out.println(i); }

While Loop

  • Structure: while(condition)
  • Example: int i = 0; while(i < 10) { System.out.println(i); i++; }

Do-while Loop

  • Structure: do { // code } while(condition);
  • Example: int i = 0; do { System.out.println(i); i++; } while(i < 10);

Conditional Statements

If-Else

  • Structure: if (condition) { // code } else { // code }
  • Example:
    int score = 85;
    if (score >= 90) {
        System.out.println("A grade");
    } else if (score >= 80) {
        System.out.println("B grade");
    } else {
        System.out.println("C grade");
    }
    

Switch-Case

  • Structure: switch (variable) { case x: // code; break; ... default: // code }
  • Example:
    int day = 2;
    switch (day) {
        case 1:
            System.out.println("Monday");
            break;
        case 2:
            System.out.println("Tuesday");
            break;
        // other cases
        default:
            System.out.println("Other day");
    }
    

Arrays

  • Declaration: dataType[] arrayName = new dataType[size];
  • Initialization: arrayName[index] = value;
  • Example:
    int[] numbers = new int[5];
    numbers[0] = 10;
    
  • Enhanced For Loop: for(dataType item : arrayName) { // code }

Object-Oriented Programming (OOP)

Classes and Objects

  • Class: Template for objects.
  • Object: Instance of a class.
public class User {
    public String name;
    public LocalDate birthdate;

    public User(String name, String birthdate) {
        this.name = name;
        this.birthdate = LocalDate.parse(birthdate);
    }

    public int getAge() {
        return Period.between(this.birthdate, LocalDate.now()).getYears();
    }
}

Inheritance

  • Super Class (Parent): class SuperClass { ... }
  • Sub Class (Child): class SubClass extends SuperClass { ... }
  • Example:
    public class AudioBook extends Book {
        private int runTime;
    
        public AudioBook(String title, String author, int runTime) {
            super(title, author);
            this.runTime = runTime;
        }
    }
    

Conclusion

  • Practice: Key to mastering Java and OOP concepts.
  • Explore: Continue experimenting with examples and projects to solidify understanding.