02 ~ Starting With First Program in JAVA

02 ~ Starting With First Program in JAVA

Structure of the JAVA program

As we all know from the previous blog that we write our java program in a file with extension `.java` so let’s start how will we write programs in a file.

  1. Everything in the source code must be inside a class: What this means is that every file with the extension .java represents a class. The class name must match the file name, meaning if your file is Example.java, the class inside should also be named Example.

    ex:- if the file name is Main.java then the class name must be Main inside the file. and every thing else must be written inside that class.

class Main {
// Every code must be inside this class
}
  1. The class name that matches the file name must be a public class: What does "public class" mean? Simply put, it means you should write public in front of the class name. (Technically, public is an access modifier, and we’ll cover this in great detail in an upcoming blog. For now, just remember to write public before the class name.) In simple terms, public means that the class can be accessed from anywhere.

    ex :-

     public class Main {
     // Every code must be inside this class
     }
    
  2. There must be a main method: What is this "main method"? And what is a "method"? Let’s clarify. A method is a block of code. The code that should be executed must be placed inside the main method. Why is it called "main"? Well, every program needs a starting point, and the main method is where the execution begins.

    How to write main function :-

     public class Main {
         public static void main(String[] args) {
    
         }
     }
    
    • Public :- This is an access modifier, meaning the method is accessible from anywhere. (If this is unclear, don’t worry—we'll cover it later.)

    • static :-This keyword means the method can be called without creating an object. (We will discuss this more in detail when we cover Object-Oriented Programming, so don’t worry about it for now.)

    • void :- This indicates that the method does not return any value when it finishes. (We’ll dive deeper into this when we study OOP concepts, so don’t worry about this either for now.)

    • main :- This is the name of the method, and as mentioned, it is conventionally used as the entry point of the program.

    • string[] args :- This is an array of strings passed as arguments to the method. We will discuss this in more detail during our study of OOP.

Hello World in JAVA

Now that we understand the structure of a Java program, let’s see how to print "Hello, World!" to the terminal. We’ll also learn how to compile and run our first Java program. First, let’s create a file called Main.java.

Below is our first Java program, and it’s ready for compilation. As we discussed in the previous blog, Java code is first compiled into bytecode, which is then interpreted and executed line by line.

public class Main{
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
  • To compile the code, we use the javac compiler, which comes bundled with the JDK.

    javac Main.java This command will create a .class file containing the bytecode.

  • Now run this Main class java Main this will run the code and print Hello World! on the terminal screen.

Now, let’s break down what System.out.println means and what it does.

System.out.println("Hello, World!") means the following:

  • System: Refers to the system where the Java program is running.

  • out: Refers to the output stream associated with the program.

  • println: A method that prints the provided text followed by a new line.

In this case, it prints "Hello, World!" to the output window (terminal).

Now that we know how to print output to the terminal, let’s move on to learning how to take input in Java.

Taking Inputs in JAVA

We can take input in Java using a package called Scanner. Now, you might wonder: what is a package? A package is simply a folder containing classes (code) that can be used by anyone if the class is public. The Scanner class is part of a package that comes with Java and can be used in our code to take inputs.

To use the Scanner class, we first need to import it into our code using the import keyword.

import java.util.Scanner;

Since Scanner is a class, we need to create an object of it in order to use its functionality. (If you’re not familiar with the concepts of packages, classes, and objects, don’t worry—we’ll cover these topics in more detail when we study Object-Oriented Programming (OOP) in Java.)

For now, let’s just focus on how to import the Scanner class, create an object of it, and use it to take inputs.

// importing a Scanner class in our code
import java.util.Scanner;

public  class Main {
    public static void main(String[] args) {
        // Creating object of scanner class and defining where to get the input from in this case taking input from keyboard.
        // We cann also take input from files.
        Scanner input = new Scanner(System.in);

        // Let's take input of an integer value and print it on the screen right away.
        System.out.println(input.nextInt());
    }
}

In Java, we can use various methods to take input of different data types, such as int, float, String, etc. For example, nextInt is used to read an integer value.

Primitive datatypes in JAVA

What is meant by "primitive"? A primitive data type is a data type that cannot be broken down any further. But what does "cannot be broken down" mean? Let me explain with an example.

Consider the String data type:

string name = gautam

Can this string be broken down further? Yes, it can be, and it will be broken down into individual characters. Each character of the string can be stored in a char data type.

Now, take an int (integer) data type as another example:

int number = 10;

Can an int be broken down into any other data type? No, it cannot. Therefore, int is a primitive data type.

Any datatype that cannot be broken further is called primitive datatype.

Some primitive datatype

  • int: Used to store integer values (whole numbers).

  • char: Used to store single character values, such as 'a', 'b', or '1'.

  • float: Used to store floating-point numbers (numbers with decimals), e.g., 3.14. It has less precision compared to double.

  • double: Also used to store floating-point numbers, but with more precision and a larger range than float.

  • long: Used to store large integer values, larger than what int can hold.

  • boolean: Used to store one of two possible values, either true or false.

Let’s do two examples to use primitive data types.

Ex 1 :- Store the values in all above primitive datatype and print it in the screen.

public class Main {

    public static void main(String[] args) {
        int i = 73;

        char c = 'g';

        float f = 13.44f;

        double d = 13468.76423;

        long l = 1234567888888888L;

        boolean b = false;

        System.out.println(i);

        System.out.println(c);

        System.out.println(f);

        System.out.println(d);

        System.out.println(l);

        System.out.println(b);
    }
}

Ex 2 :- Take input of an age of a person and print it on the screen

import java.util.Scanner;

public class age_input {
    public static void main(String[] args) {
        System.out.println("Please enter the age of person");

        Scanner in = new Scanner(System.in);

        int age = in.nextInt();

        System.out.println("The age of person you provided is "+ age );
    }
}

That’s it for this blog! In the next one, we will dive into conditionals and loops.

Until then, keep learning, keep sharing, and keep supporting!