Getting Started with Java: A Beginner’s Guide πŸš€

Deepak ModiDeepak Modi
7 min read

Java is one of the most popular programming languages, widely used for web development, mobile applications, enterprise software, and more. Whether you're a beginner or transitioning from another language, this guide will help you understand the fundamentals and get started with Java programming.


1️⃣ What is Java?

Java is a high-level, object-oriented programming language developed by James Gosling at Sun Microsystems in 1995 (now owned by Oracle). It follows the WORA (Write Once, Run Anywhere) principle, meaning Java programs can run on any system with a Java Virtual Machine (JVM).

πŸ“Œ Key Features of Java

βœ… Platform Independent: Runs on any OS using JVM.
βœ… Object-Oriented: Uses classes and objects for structure.
βœ… Robust & Secure: Strong memory management, exception handling, and security features.
βœ… Multi-threading Support: Enables parallel execution for better performance.
βœ… Automatic Garbage Collection: Manages memory allocation and deallocation.


2️⃣ Installing Java on Your System

To start coding in Java, you need to install the Java Development Kit (JDK). The JDK includes the compiler (javac), runtime (java), and other tools for Java development.

πŸ“Œ Steps to Install Java

  1. Download the latest JDK from the Oracle website or OpenJDK.

  2. Install the JDK and set the Environment Variables (JAVA_HOME and Path) on your system.

  3. Verify installation by running:

     java -version
     javac -version
    

    If Java is installed correctly, it will display the version information.


3️⃣ Writing Your First Java Program

Now that you have Java installed, let's write and run your first program.

πŸ“Œ Hello World in Java

Create a file named HelloWorld.java and write the following code:

// Class Declaration
public class HelloWorld {
    // Main Method (Entry Point)
    public static void main(String[] args) {
        System.out.println("Hello, World!");  // Print Output
    }
}

πŸ“Œ How to Compile and Run the Program

  1. Compile the Code:

     javac HelloWorld.java
    

    This generates a HelloWorld.class file (bytecode).

  2. Run the Program:

     java HelloWorld
    

    Output:

     Hello, World!
    

4️⃣ Java Basics: Understanding Syntax & Structure

πŸ“Œ Java Program Structure

Let's break down each component of the Java program structure and explain the keywords used:

// Class Declaration
public class HelloWorld {
    // Main Method (Entry Point)
    public static void main(String[] args) {
        System.out.println("Hello, World!");  // Print Output
    }
}

Each Java program consists of:
βœ… Class: Every program is inside a class (HelloWorld in our example).
βœ… Main Method (main): The entry point for execution.
βœ… Statements: Java uses semicolons (;) to end statements.
βœ… Curly Braces {}: Defines blocks of code.

1. Class Declaration

  • public: An access modifier. It means the class is accessible from other classes.

  • class: Keyword used to define a class.

  • HelloWorld: The name of the class. By convention, class names should start with an uppercase letter.

2. Main Method (Entry Point)

  • public: An access modifier. It means the method is accessible from other classes.

  • static: This means the method belongs to the class rather than instances of the class. It can be called without creating an object of the class.

  • void: The return type of the method. It means the method does not return any value.

  • main: The name of the method. This is the entry point for any Java program.

  • String[] args: An array of String arguments passed to the method. It allows the program to accept command-line arguments.

3. Statements

  • System.out.println("Hello, World!");: This statement prints the text "Hello, World!" to the console.

    • System: A class in the java.lang package.

    • out: A static member of the System class, which is an instance of PrintStream.

    • println: A method of the PrintStream class that prints the argument passed to it followed by a new line.

4. Curly Braces {}

  • { … }: Curly braces are used to define the beginning and end of a block of code. In this case, they define the blocks for the class and the main method.

5️⃣ Java Variables & Data Types

Java is a statically typed language, meaning every variable must have a defined data type.

πŸ“Œ Variable Declaration

int age = 25;  // Integer variable
double price = 99.99;  // Decimal number
char grade = 'A';  // Single character
boolean isJavaFun = true;  // Boolean value
String name = "Deepak";  // String (text)

πŸ“Œ Primitive Data Types

Data TypeSizeExampleRange
boolean1 bitBoolean flag = true;true or false
byte1 byte (8 bits)byte b = 127;-128 to 127
char2 bytes (16 bits)char letter = 'A';0 to 65,536
short2 bytes (16 bits)short s = 32000;-2^15 to 2^15-1
int4 bytes (32 bits)int num = 100;-2^31 to 2^31-1
long8 bytes (64 bits)long bigNum = 100000L;-2^63 to 2^63-1
float4 bytes (32 bits)float pi = 3.14f;3.4e-038 to 3.4e+038
double8 bytes (64 bits)double price = 99.99;1.7e-308 to 1.7e+308

πŸ”Ή Note: String is not a primitive type but is widely used for text data.


6️⃣ Operators in Java

Java provides various operators for performing arithmetic, comparison, and logical operations.

πŸ“Œ What are Operators?

Operators are special symbols that perform operations on variables and values.

πŸ“Œ Types of Operators

πŸ‘‰ Arithmetic Operators

Used for mathematical operations like addition, subtraction, multiplication, etc.

int a = 10, b = 5;
System.out.println(a + b);  // Addition (+)
System.out.println(a - b);  // Subtraction (-)
System.out.println(a * b);  // Multiplication (*)
System.out.println(a / b);  // Division (/)
System.out.println(a % b);  // Modulus (%)

πŸ‘‰ Relational (Comparison) Operators

Used to compare values and return true or false.

System.out.println(a > b);  // true
System.out.println(a == b); // false
System.out.println(a != b); // true

πŸ‘‰ Logical Operators

Used to perform logical operations.

System.out.println((a > b) && (a > 0));  // true (AND)
System.out.println((a < b) || (a > 0));  // true (OR)

7️⃣ Control Flow Statements

πŸ“Œ If-Else Condition

if (age >= 18) {
    System.out.println("You are an adult.");
} else {
    System.out.println("You are a minor.");
}

πŸ“Œ Switch Case

int day = 3;
switch (day) {
    case 1: System.out.println("Monday"); break;
    case 2: System.out.println("Tuesday"); break;
    default: System.out.println("Other Day");
}

πŸ“Œ Loops in Java

πŸ‘‰ For Loop

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

πŸ‘‰ While Loop

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

8️⃣ Functions in Java

In Java, functions (or methods) are blocks of code that perform specific tasks and can be called upon to execute when needed.

πŸ“Œ Defining and Calling Functions

A function is defined within a class. Also, a function defined within a class is known as method. So, every function is method in Java. To call a function, you simply use its name followed by parentheses containing the arguments, if any.

public class MathOperations {
    // Function Definition
    public static int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        int result = add(10, 20);
        System.out.println("Sum: " + result);
    }
}
  • public static int add(int a, int b): This is the function header.

    • public: Access modifier.

    • static: Indicates that the method belongs to the class, not an instance of it.

    • int: Return type of the method.

    • add: Name of the method.

    • (int a, int b): Parameters the method accepts.

  • return a + b;: The body of the method, which defines what the method does.


9️⃣ Object-Oriented Programming (OOP) in Java

Java is an object-oriented programming (OOP) language. It follows the principles of:
βœ… Encapsulation: Wrapping data and methods into a single unit (class).
βœ… Inheritance: Acquiring properties from another class.
βœ… Polymorphism: One interface, multiple implementations.
βœ… Abstraction: Hiding implementation details.

πŸ“Œ Example of a Class and Object

class Car {
    String brand = "Tesla";
    void honk() {
        System.out.println("Beep Beep!");
    }
}

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

πŸ”Ή Summary & Next Steps

βœ… Java is a platform-independent, object-oriented, and secure language.
βœ… We learned about syntax, data types, operators, control flow, functions, and OOP.
βœ… The next step is to practice coding and explore advanced topics like Collections, Exception Handling, and Java Frameworks.


πŸ”— Best Resources to Learn Java

πŸ“Œ Java Documentation: Oracle Docs
πŸ“Œ Online Practice: LeetCode Java
πŸ“Œ Video Tutorials: Java by CodeWithHarry

I will cover specific Java topics in further blogs. Do let me know if you have any suggestions! πŸš€

1
Subscribe to my newsletter

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

Written by

Deepak Modi
Deepak Modi

Hey! I'm Deepak, a Full-Stack Developer passionate about web development, DSA, and building innovative projects. I share my learnings through blogs and tutorials.