fuddlepixelFuddlepixel coz tech is fun();

Beginning Java with program class, main function and comments

Beginning Java Programming by understanding in detail the Hello World program, class, main function, comments and command line tools.

By Shubham Ramdeo
31 July 2018 · 4 mins read
# java 

Beginning Java with program class, main function and comments

We have already installed the Java Development Kit (JDK) for our system. With this article, we are beginning Java programming by writing a simple Hello Java program, also learning how to use the java command line tools.

If you haven’t installed JDK yet, you can read the complete tutorial here, you also need to understand some basic concepts of object oriented programming which we have already discussed here.

Hello Java class

We are beginning java with the simple Hello, World program which we have already tested in the last article. So now we can discuss it in detail.

As we have discussed in previous articles that Java is a pure object oriented language, therefore, everything in Java is an Object.

Even the whole program itself is an object.

So now suppose, if our program is an object, it has to belong to some class also. Let us call it a class Hello.

In Java, we conventionally name classes with first letter capital. So its Hello, not hello.
  • The name of this class should also be the name of the file.

Beginning java with main function

Now there is a catch. In a java program, there can be multiple objects of multiple classes having multiple methods etc. How would java know from where to execute the program? Therefore, it is made a rule that, java program begins with the class having the main function.

So if there are a number of classes, in a program, the class which has the main function will be executed first. And therefore, every java program has at least one class, which has the main function.

This main function is usually declared as follows:

<class_name_of_program> {
    ...
    public static void main(String args[]){
        ...
    }
    ...
}

Now let’s add some functionality. How about our Java code say something output? “Hello Java!”. So to print something on screen, we can use a function called system.out.println().

System.out.println(" <your text here> ");

Now add all this up into a complete program.

  • Our class name is Hello.
  • So our file name should also be Hello.
  • There will be a main function in it.
  • That main function will print Hello Java!.
class Hello {
    public static void main(String args[]){
        System.out.println("Hello Java!");
    }
}

javac and java

Saving it as Hello.java, now we will compile it. To compile we will use javac which is Java Compiler. It takes our java files and converts it into java class which have bytecodes that are understood by Java (Java Virtual Machine to be precise, we will understand these concepts later).

So, open your command prompt and do:

javac Hello.java

If you are having any issues in the above command, or you don’t know what the heck is this, it is the compilation process which was setup and explained in the last part.

After using the javac command, it will create a Hello.class file for us. Now all we need to do is to run it using java command. So in the same directory, do:

java Hello

And this brings the output as follows:

Hello Java!
Just for Fun: Check out collection of Hello World program in every language at this awesome website http://helloworldcollection.de/

Adding comments

Doesn’t our code look so plain and dry? We can add comments to add important remarks and explanation for different parts of the code as follows.

There are two types of comments.

The block comments, in which we can comment out multiple lines between /* and */

/* whatever
written inside me
will get
IGNORED!
*/

And the second type is line comment. So anything written after // will get ignored. It works for a single line only.

int x; //this is x and I am comment!
float y;

So adding comments to our original code, we have

/*
Hello.java
Found at fuddlepixel! LOL
*/

class Hello {
    public static void main(String args[]){
        System.out.println("Hello Java!");
    } // Every bracket should be closed properly.
}

Saving it and running it, we will see No Change. Obviously, because comments are ignored!

So this was a brief explanation of a simple hello world program. Hello World programs always help, even in beginning java. If you have any doubts, please feel free to comment down below.

In the upcoming articles, we will discuss how to create multiple classes and use them. Until then, keep learning programming from zero at fuddlepixel.com


Share this :



Update me weekly ✉

Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.

I'm in ♥


Or follow us
   


About author

Shubham Ramdeo

Shubham Ramdeo

Shubham Ramdeo is the founder of Fuddlepixel. He is a geek, a writer, involved with many technical and humanity projects.

Meet me!



Recent





Related

java 

Top