Namaste World !

Namaste World !

writing your first Java program

ยท

4 min read

In the last blog, we discussed Java from a theoretical standpoint, understanding the what and why and the architecture of the language. in this blog, we will write our first program in java.

Installation

before starting to write and run your java programs you need to install java on your computer, to do so download and install the latest JDK, at the time of publishing this blog the latest was Java 19, downloadable here

you will also need a code editor, I use IntelliJ IDEA. It is one of the best IDEs out there for writing java code, you can use any IDE for writing your code but I highly recommend using IntelliJ. Its community edition is free to use for anyone. you can download it here.

for this tutorial we will not jump to IntelliJ, I will be running the code from the terminal and using VSCode or Notepad for writing the code

Hello Java

whenever we start learning a new language the very first thing we do is Hello World, below is the program for the same.

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

Classes

everything in java is written in classes. classes are named groups of properties.

classes have objects and attributes and to define a class use the class keyword (more on objects and attributes in later parts).

Access Modifiers

An access modifier is used for defining the accessibility or scope of a method or class, there are 3 main types of access modifiers.

  • public

  • protected

  • private

a public class means that whatever is inside this class ( code ) is accessible from anywhere, meaning the contents of this class can be accessed from outside the class also.

Code Explanation

Line-1

public class Main {

public - access modifier

class - keyword to define a class

Main - the name of the class, Main class is also the entry point to our code, Main class tells the computer that the execution starts from here.

also, the filename and main class name must be the same. it does not necessarily have to be Main you can name it anything as long as the filename and the class name are the same.

Line-2

public static void main(String [] args){

public - access modifier

static - we want to run the main function without creating an object of class Main

void - return type of function, void meaning it returns nothing

main - the name of the function (cannot change this name)

(string [] args) - a collection of string command line arguments, used to pass input to the code directly from the terminal while running it

Line-3

System.out.println("Namaste World!");

it is used to print a new line and whatever is provided in ( ) is printed to the console / in the output.

there is another kind of print that prints in the same line

System.out.print();

Code Execution

now, let's head on and open the Terminal(macOS and Linux ) or your CMD(Windows) and type java, if you see an output, your java SDK installation was successful, if not try reinstalling it.

now create a folder on your desktop and inside this folder create a file named Main.java. now copy and paste the code from above into this file and save. If you know how to do this directly from the terminal then that is very good, but if you don't know how to do it, go ahead and do it the way you always do and then you can later open that folder in the terminal from the file explorer itself.

The next step is to compile the code and create bytecode or class file. In the terminal type javac Main.java ( java <filename with extension>), this will create a class file in the same folder ( Main.class ).

now to run this code type java Main ( java <filename> ) and if you did everything right you should have an output.

Congrats! ๐ŸŽ‰ you have successfully written and run your program.

Conclusion

This was all about writing your very first java program, I hope the explanation is clear and precise. Do let me know if you like it and share it with your friends.

Feel free to comment and give suggestions/feedback.

Thank you for reading.

Did you find this article valuable?

Support Yash Dhasmana by becoming a sponsor. Any amount is appreciated!

ย