Java ProgrammingWriting our First Program in Java Programming Language

Writing our First Program in Java Programming Language

After paving the ground, we are ready to start building on it.
Our first Java program will print a simple hello message to the user. In what follows, we are going to learn together how to write a Java program, compile it, and execute it on a Linux box.
Have fun!!

* * * * * *

Writing our First Program on a Linux Machine

1.Open a terminal on your Linux machine, and start editing a new file: Hello.java

vim Hello.java

2. In the vi editor, type the following code:

public class Hello
{
        public static void main(String [] args)
        {
                System.out.println("Hello, I am a Java programmer");
        }
}

3. Save and exit the file.

4. Now, compile the file using the javac command:

javac Hello.java

5. If you list the files in the current directory, you should find a new file created called Hello.class. This file is the resulting bytecode class file (we know from the last article that Java compiler converts Java code into bytecode).
1
6. Now, execute the program:
2

This is how to write, compile, and execute a Java program on Linux. Now, we need to get this code explained.

How Things Work?!
Trivial program it is, no doubt. But, who said we couldn’t come up with some useful knowledge out of it?!

The program starts with the class declaration:

public class Hello

• This defines a class named Hello. The word public is called an access modifier. Access modifiers control access to classes, methods, and attributes. public tells that the Hello class is accessible from anywhere.
• Class names are usually started with an uppercase letter (although it could start with any letter or underscore).
• The file name (Hello.java) must be exactly the same as the public class defined in it. An important rule to always remember.
• A Java program file can contain more than one class definistion, but only one could be defined as public (the one the file name will be named after).
• Code inside class definition is enclosed within curly braces { }.

public static void main(String [] args)

• This defines a method named main, and specifies it as public.
• static in the method declaration makes the main() method available for use without instantiating (creating object of) the Hello class. This is necessary when defining the main() method, as it is the first code to be executed in a Java program.
• void is the return value expected from the main() method. Void means nothing, so specifying void as the method return means the method won’t return a value.

String [] args

• This is an array of strings that contains the list of command-line arguments passed to the program on execution (if any).
• As with classes, code inside a method is also enclosed within curly braces { }

System.out.println("Hello, I am a Java programmer");

• This is the body of the main() method.
• It prints its arguments to the standard out.
• Java statements are terminated with semi-colons.
• Strings of text need to be delimited by double quotes “ “

– All of these conclusions out of this simple program?!!

Yes, I told you, but you didn’t believe me!!

Java Programming Course for Beginner From Scratch

Using Comments
As a best practice, you should document your work, and your Java programs are not exception. Documenting your code makes it easier for others (and even for you) to understand your code if there is a need to modify it in the future. Comments are embedded within the Java code. The Java compiler completely ignores those comments when compiling the source code.

Java supported two types of comments:

Single-line comments: A single-line comment comments out everything appearing after it and up to the end of the line.

Syntax

//A single line comment. It will be ignored by the compiler

Example

public class Hello		//The class declaration

Multi-line comments: This type of comments can extend to more than one line. When the compiler sees the /* sequence, it considers everything after it as comment. This continues until it encounters the closing sequence */

Syntax

/* 	A multi-line comment.
	This will be ignored by
	The Java compiler  		*/

Example
Let’s re-write our first program with some comments added.

/*	This is our first program
	With some comments added to it   */

public class Hello	// Starting the Class definition
{
        public static void main(String [] args)
        {
		    // This prints a hello message to the standard out
                System.out.println("Hello, I am a Java programmer");
        }
} 	// End of the program

Compressing your code
The above program could be re-written in the following compressed form, and still compile and execute successfully giving the same result:

public class Hello {
public static void main(String [] args){
System.out.println("Hello, I am a Java programmer");}}

The reason is that white spaces and empty lines are also ignored by the Java compiler. Although this compact version comes in only three lines (instead of seven) that appear to be saving lines and keeping your code short, it is less readable. That is why I don’t like it, and never recommend it.

* * * * * *

Conclusions
• In this article, we have written our first program. Despite its simplicity, we have come out with important conclusions out of this program.
• The name of the file containing the Java program must exactly match the name of the public class defined in it.
• Class names usually start with uppercase letters (but may not).
• The main() method is the first code to be executed in a Java program.
• Specifying void as the return of a method means the method will not return any value.
• Java statements are terminated with semi-colons.
• Java supports two types of comments: single-line comments, and multi-line comments. A single-line comment uses the // sequence to comment anything that appears to its right until the end of the line. A multi-line comment uses the sequences /* and */ to delimit a comment that can span one or more lines.
• Empty lines and white spaces are ignored by the Java compiler.

That was part two of our long series on Java programming. Stay here!! Part three is on the way.
We won’t be late!!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -