Learnin' Java Series - Part 1

Subscribe to my newsletter and never miss my upcoming articles

Java is an Objected oriented and Platform-independent programming langugae, and is quite popular and used by giants in the Industry. You can kickstart learning about OOPs in Java. A Very friendly (better than C++) language with a great community behind. Let's skip the talk and get our feet wet.

First and foremost, It's a necessity to define one public class inside the java file which contains the main method. And as a matter of good practice: We will keep the filename and name of the class (which contain the main method) the same.

Let's create our first file: Sum.java

public class Sum {

};

So, The Compiler first searches for the public class and In that class, It searches for the main method and executes it. We haven't added the main method yet. Let's add.

public class Sum {
  public static void main(String args[]){
     int a,b;
     a = 5;
     b = 6;
     System.out.print("Sum is "+a+b);
  }
};

A lot of things happened in the code. Let's breakdown.

We added public static void main(String args[]){ } in the Sum class. The Main method should have the same signature always because inside the class, JVM search for the method which is public static void and is named main and accepts String args[] as parameters. what they mean is a part of the following blog posts. Inside the main method, we wrote the code for adding two numbers and printing the sum. We just have to remember that:

  • It's important to define one (only) public class inside the java file. -More than one public class aren't allowed inside a single file. But you can have multiple private and protected classes inside a single file. (public, private, protected to be covered in coming posts)
  • The name of the class (which contains the main method) should be the same as the filename. (Best practices)
  • the main method should always be defined as: public static void main(String args[])

So we have our boilerplate code for Java file with filename: XYZ.java

public class XYZ {
    public static void main(String args[]){
        //Code
    }
};

Printing In Java

Print anything you want on the console using the function provided by the Java library. Note that we didn't need to import the package System which provides the print function. System.out.print("Anything you want to print here");

Whatever we pass into the print function, gets converted into a string implicitly (we'll deep dive into it later) then get printed out on the output stream (Console).

Compiling and Running Java source code.

Step1) Open the folder (which contains the Java source code file) in command prompt using the command

cd /d <FolderAddress>

For example, I save Sum.java file in L:\Java I will type:

cd /d L:\Java

Step2) Compile java file using the command:

javac Sum.java

Then, a file names Sum.class will appear in your folder. It is a compiled bytecode that is executed by JVM.

Step4) To Run, type in the command prompt:

java Sum

and The output will appear on the Command Prompt

Article1.PNG

Victoria Lo's photo

Great intro to Java! It was my first language.. Very nostalgic to read a post on it. Thanks! :)

Lokendra Bohra's photo

Learning Algorithms and solving Leetcode

Thank you to you too :) It is indeed a good language

Lokendra Bohra's photo

To avoid the command prompt, Code at repl.it It is a browser-based IDE, where you can code in 50+ languages and run with just a click.