Introduction to Java Programming

Gaurav Sah
3 min readNov 11, 2024

--

Java is one of the most popular and versatile programming languages. Developed by Sun Microsystems (now owned by Oracle) in 1995, it has become a cornerstone for building desktop applications, web applications, and even Android apps. Java’s core philosophy, “Write Once, Run Anywhere” (WORA), ensures that code written in Java can run on any platform that supports the Java Virtual Machine (JVM).

Key Features of Java

  1. Object-Oriented: Java supports object-oriented programming (OOP), which focuses on objects and classes, making the code modular, flexible, and easy to maintain.
  2. Platform-Independent: Java code runs on the JVM, making it platform-independent. You can write code once and run it anywhere without modification.
  3. Robust and Secure: Java includes strong memory management, automatic garbage collection, and exception handling, ensuring stability and security.
  4. Multi-threaded: Java allows concurrent execution of programs using threads, improving the efficiency of applications.
  5. Rich API: Java has a comprehensive standard library (API) that simplifies tasks like networking, input/output, and data manipulation.

Java Syntax Overview

Here’s a simple “Hello, World!” program in Java to get you started:

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

Explanation:

  • public class HelloWorld: Defines a class named HelloWorld. In Java, every application begins with a class declaration.
  • public static void main(String[] args): This is the main method. It's the entry point of any Java application.
  • System.out.println("Hello, World!");: This line prints "Hello, World!" to the console.

Setting Up Java on Your System

To start coding in Java, you need to:

  1. Install Java Development Kit (JDK): Download and install the latest JDK from Oracle’s official website.
  2. Set Up Environment Variables:
  • Add JAVA_HOME to your system environment variables.
  • Include the JDK’s bin directory in your PATH.

3. Choose an IDE: Popular choices include:

  • Eclipse: Feature-rich and widely used for Java development.
  • IntelliJ IDEA: Known for its advanced code completion and productivity features.
  • Visual Studio Code: Lightweight with extensions for Java support.

Basic Java Program Structure

Java programs consist of classes and methods. Here’s a breakdown of a basic program:

// Example.java
public class Example {
// This is a method
public static void main(String[] args) {
int number = 10; // Declaration of a variable
System.out.println("The number is: " + number);
}
}

Output:

The number is: 10

Explanation:

  • int number = 10;: This declares an integer variable named number and assigns it a value of 10.
  • System.out.println(...): Used to display output.

Compiling and Running a Java Program

To run a Java program:

  1. Open a terminal or command prompt.
  2. Navigate to the directory where your Java file is located.
  3. Compile the code using the javac command:
javac Example.java

4. Run the compiled program using the java command:

bashjava Example

Output:

The number is: 10

Why Learn Java?

  • Versatility: Java can be used for web applications, desktop applications, games, and mobile apps.
  • High Demand: Java is in high demand in the job market, making it a valuable skill for aspiring developers.
  • Strong Community Support: Java has a large and active community, providing many resources and libraries.

Java vs. Other Languages

--

--

Gaurav Sah
Gaurav Sah

Written by Gaurav Sah

Full Stack Developer and entrepreneur specializing in MERN stack. I build web apps and write about tech, trends, and startups. Let's create something amazing!

No responses yet