Introduction to Java Programming

By Raman Kumar

Updated on Aug 12, 2024

In this tutorial, we'll see the introduction to Java Programming.

Introduction to Java Programming

Java is a versatile, object-oriented programming language developed by Sun Microsystems in 1995 and later acquired by Oracle Corporation. It was designed to have as few implementation dependencies as possible, making it a platform-independent language that can be run on any device that supports the Java Virtual Machine (JVM).

Java's "write once, run anywhere" capability allows developers to create applications that can run on different platforms without the need for recompilation. This cross-platform capability, along with its robust security features and extensive libraries, has made Java one of the most popular programming languages for developing enterprise-level applications, web applications, mobile apps, and even embedded systems.

Java Datatypes

Java provides several data types, which can be categorized into primitive data types and reference data types.

1. int

The int data type is used to store integer values without decimal points. It is a 32-bit signed two's complement integer.

Syntax:

int myNumber = 100;

2. byte

The byte data type is the smallest integer data type in Java. It is an 8-bit signed two's complement integer. The minimum value it can hold is -128, and the maximum value is 127. It is often used for saving memory in large arrays where the memory savings matter.

Syntax:

byte myByte = 10;

3. short

The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767. This data type can also be used to save memory as compared to an int when you are sure that the value will not exceed the short range.

Syntax:

short myShort = 30000;

4. long

The long data type is a 64-bit signed two's complement integer. It is used when a wider range than int is needed.

Syntax:

long myLong = 100000L;

Note: The L suffix indicates that the literal value is of type long.

5. float

The float data type is a single-precision 32-bit IEEE 754 floating point. It is primarily used to save memory in large arrays of floating-point numbers. However, it is less precise than double. This type is generally used when precision is not critical.

Syntax:

float myFloat = 5.75f;

Note: The f suffix indicates that the literal value is of type float.

6. double

The double data type is a double-precision 64-bit IEEE 754 floating point. It is the default data type for decimal values in Java. It is generally used for more precise calculations, such as in scientific computations.

Syntax:

double myDouble = 19.99;

7. char

The char data type is used to store a single 16-bit Unicode character. It is primarily used for storing individual characters.

Syntax:

char myChar = 'A';

Note: Characters are enclosed in single quotes.

8. boolean

The boolean data type has only two possible values: true or false. It is used for simple flags that track true/false conditions.

Syntax:

boolean isJavaFun = true;

Example of Using All Datatypes in a Java Program

public class DataTypesExample {
    public static void main(String[] args) {
        int myNumber = 100;
        byte myByte = 10;
        short myShort = 30000;
        long myLong = 100000L;
        float myFloat = 5.75f;
        double myDouble = 19.99;
        char myChar = 'A';
        boolean isJavaFun = true;

        System.out.println("int: " + myNumber);
        System.out.println("byte: " + myByte);
        System.out.println("short: " + myShort);
        System.out.println("long: " + myLong);
        System.out.println("float: " + myFloat);
        System.out.println("double: " + myDouble);
        System.out.println("char: " + myChar);
        System.out.println("boolean: " + isJavaFun);
    }
}

When you run this program, it will print the values of all the different data types to the console. This example provides a practical demonstration of how these data types are used in Java.

Reference data types, on the other hand, include objects, arrays, and interfaces. Unlike primitive data types, reference types store references to memory locations where the data is actually held. For example, a String in Java is a reference type that points to a sequence of characters stored in memory.

Writing Your First Java Program

To get started with Java, let's write a simple program that prints "Hello, World!" to the console. This is a classic first program that demonstrates the basic structure of a Java application.

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

Explanation of the Code

public class HelloWorld: This line declares a public class named HelloWorld. In Java, every application begins with a class definition. The name of the class should match the filename, so in this case, the file should be named HelloWorld.java.

public static void main(String[] args): This is the entry point of any Java application. The main method is where the program starts executing. It is defined as public so that it can be accessed by the JVM, static so it can be called without creating an instance of the class, and void because it doesn't return any value. The String[] args parameter is used to pass command-line arguments to the program.

System.out.println("Hello, World!");: This line prints the string "Hello, World!" to the console. System.out is a standard output stream, and println is a method that prints the specified string followed by a newline character.

Compiling and Running the Program

To compile and run this Java program, follow these steps:

Save the code in a file named HelloWorld.java.

Open a terminal or command prompt and navigate to the directory where the file is saved.

Compile the program using the Java compiler by running the command:

javac HelloWorld.java

This will generate a HelloWorld.class file, which contains the bytecode that can be executed by the JVM.

Run the compiled program by typing the following command:

java HelloWorld

You should see the output:

Hello, World!

This simple example introduces you to the basic structure of a Java program, and from here, you can start exploring more complex concepts and features of the language.

Checkout our dedicated servers and KVM VPS plans.