Hello World - CSU1291 - Shoolini U

Practical 1: Hello World in Java

1. Introduction to Java

Java is a versatile, object-oriented programming language designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere" (WORA), meaning that code that runs on one platform does not need to be recompiled to run on another.

1.1 History and Purpose

Java was developed by Sun Microsystems (now owned by Oracle Corporation) in the early 1990s. Its primary purpose was for embedded devices. However, Java found its prominence with the advent of the web, where it became popular for server-side applications, especially for large-scale enterprise software.

The design philosophy behind Java is to be simple, object-oriented, and familiar. It draws a lot of syntax and concepts from C++, but with a simpler object model and fewer low-level constructs.

1.2 Features of Java

Java offers a wide range of features, a few of which include:

2. The Essentials of Java

For writing a simple Java program, the following components represent the essential:

2.1 Java Class Structure

In Java, every application begins with a class. A class serves as a blueprint for objects and can contain data (attributes) and methods (functions) to operate on the data.

The basic structure of a Java class is as follows:

public class ClassName {
    // attributes and methods
}

In this structure, "public" is an access modifier that indicates the class can be accessed from other classes. "class" is a keyword to declare a class, and "ClassName" is a placeholder for the actual name of the class.

2.2 The Main Method

Every Java application requires a main method as its entry point. The main method is where the JVM begins executing the program.

The signature of the main method is:

public static void main(String[] args) {
    // code
}

Here:

2.3 System Output

To display output in Java, we use the "System.out.println()" method. "System" is a built-in class, "out" is an instance of the PrintStream class, and "println()" is a method to print lines.

2.4 Compilation and Execution

Java code, written in a file with the ".java" extension, is compiled to bytecode (with a ".class" extension) using the Java compiler "javac". Once compiled, the bytecode can be executed on any JVM using the "java" command.

3. Writing the Hello World Program in Java

Now, with an understanding of the basic components, let's write the quintessential "Hello World" program in Java:

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

This program consists of a single class "HelloWorld". Inside the class is the main method, which, when executed, prints "Hello, World!" to the console. But is it the only way of printing data in Java? Let us first compile and run this program then we will look into that shortly.

3.1 Compiling the Hello World Program

After writing the program, save it with the same name as the class name appended with .java extension, "HelloWorld.java" in this case. To compile it, open the terminal or command prompt and navigate to the directory containing the file. Then, run:

javac HelloWorld.java

This will generate a "HelloWorld.class" file, which contains the bytecode.

3.2 Executing the Hello World Program

Once compiled, you can execute the program using the following command:

java HelloWorld

This will print "Hello, World!" to the console.

3.3. Output

After following the steps above, executing the "Hello World" program will result in the following output:

Hello, World!

This simple output is a testament to your first successful Java program, and it paves the way for more intricate and complex applications in the future.

4. Displaying Data in Java: Dive into Printing Methods

In Java, presenting data to the console is a fundamental task. Java offers several methods for this purpose, each with its unique usage and advantages. Let's explore these methods using a student's details as an example.

4.1 Introduction to the Student Details Example

We'll be using a simple example throughout this section: displaying a student's name, class, and school. This example will help illustrate the differences and applications of each printing method.

4.2 Using println()

The println() method is perhaps the most straightforward way to print data. It outputs the provided message and then moves to a new line.

// Using println
System.out.println("Student Name: dmj.one");
System.out.println("Class: B.Tech CSE");
System.out.println("School Name: Shoolini University");

4.3 Using print()

The print() method is similar to println() but with a notable difference: it doesn't move to a new line after printing. This allows for consecutive print statements to appear on the same line.

// Using print
System.out.print("Student Name: ");
System.out.print("dmj.one");

4.4 Using printf() and format()

The printf() and format() methods allow for formatted output. They use placeholders, like %s for strings and %d for integers, to specify where and how data should be displayed.

// Using printf
System.out.printf("Student Name: %s, Class: %s", "dmj.one", "B.Tech CSE");

// Using format (similar to printf)
System.out.format("\nStudent Name: %s, School: %s", "dmj.one", "Shoolini University");

Note: Both printf() and format() allow for more advanced formatting options, like controlling the width of the printed data, adding padding, and more.

4.5 Using append()

While append() isn't primarily used for console printing, it's worth mentioning. It's commonly seen with StringBuilder or StringBuffer. The method appends the given character sequence to the current output stream and returns the stream.

// Using append (for demonstration)
System.out.append("Student Name: ").append("dmj.one");

It's crucial to understand the context in which append() is used, as it might not always be the best fit for straightforward console printing.

Having learnt all the basic structures, let's display student details with the simplest possible way in Java:

public class StudentDetails {
    public static void main(String[] args) {
        System.out.println("Student Name: dmj.one");
        System.out.println("Class: B.Tech CSE");
        System.out.println("School Name: Shoolini University");
        System.out.println("Roll Number: 1");
        System.out.println("Date of Birth: 1st April 2010");
    }
}

This program directly prints five fields of student details using the println method. Please experiment with it to explore more.

4. What's on the Horizon?

Having unlocked the gateway to Java with our "Hello, World!" program, you're standing at the brink of limitless possibilities. As we journey forward, our next beacon will be diving deep into the world of Java's Object-Oriented Programming (OOP). OOP is the heart and soul of Java, empowering developers to model real-world scenarios with elegance and precision. Get ready to sculpt code that mirrors life, breathe life into objects, and choreograph their interactions in harmonious symphonies. The world of Java is vast, and this is just the beginning. So, fasten your seatbelts as we embark on this exhilarating voyage!