Broad Network


Java Basic Syntax

Java Basics - Part 2

Forward: In this part of the series, I give you the basic syntax of Java.

By: Chrysanthus Date Published: 1 Sep 2012

Introduction

This is part 2 of my series, Java Basics. In this part of the series, I give you the basic syntax of Java.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Statement
A statement in Java is a short piece of code that ends with a semicolon. An example is:

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

This particular statement sends the string "Hello World!" to the output. In other words, it prints the string "Hello World!" at the Command Prompt window.

Comments
You should have comments in your code. Comments are not executed. Comments are to remind you later of why you typed a particular piece of code. There are two types of comments: single-line comments and multiple-line comments. A single-line comment can only be in one line; something like:

        // This is a single-line comment.

A single-line comment begins with a double forward slash. For a single-line comment, everything to the right of the comment is not executed.

A multiple-line comment begins with /** and ends with */ . An example is:

/** This is a multiple-line comment. It can be of any length, and
you can put whatever you want here. */

A multiple-line comment spans more than one line. The opening delimiter is a forward slash and two asterisks. The closing delimiter is an asterisk and a forward slash.

You will try the following code, which has the two types of comments:

/** This is still my first Java program.
  I am learning Java */
class Comment
    {
        public static void main(String[] args)
            {
                // this statement outputs a string.
                System.out.println("Hello World!");
            }
    }

To try the above source code, type it in a text editor. Save the document with the name comment.java in the java directory. Go to the C:\java> prompt in your Command Prompt window. Type the following compiler command and press the Enter key:

    javac comment.java

This command instructs the compiler to produce the executable class file, Comment.class from Comment.java and save the executable file in the same working java directory.

Note the single and multiple line comments in the source code. The aim of this project is to see if the comments in the source code will have any effect in the execution of the program.

Note that since we want the name of the class file to be Comment.class, after the word, “class” in the program I have written “Comment” and not “hello” as it was in the previous part of the series. Remember, Java is case sensitive, so “Comment” will produce a class file of “Comment.class” and not “coMMent.class” or “cOMMENT.CLASS”, etc. When running a Java program, you have to respect the casing again. That is you have to type “java Comment” and not “java coMMent.class” or “cOMMENT.CLASS”, etc.

At the C:\java> Commend Prompt, type the following command for the executable file and press Enter, to run the program:

    java Comment

The program should output, Hello World! .

As you can see the comments have not affected the execution of the program. Comments are in your source code to remind you in future of what and why you typed a particular code segment.

There are many code samples in this series that you will try (compile and execute). I will not be telling you what to do in order to try a code sample. You will follow the above procedure. Let the source code file and the executable files have the same name, but with different extensions. The source file should have the extension, “.java” and the executable file will automatically have the extension, “.class”. The name of the pair of files is the name of the main class (see later).

Block
A block in Java source code is a set of statements delimited by the curly brackets, { and }. Many blocks are preceded by an expression. Such preceding expression can be considered as part of the block. In the above source code you have the expression, “class Comment” preceding the outer block and the expression, “public static void main(String[] args)” preceding the inner block. The two blocks are examples of blocks. We shall come back to these particular blocks as we carry on with the series.

Source File and Executable File Names
The source file and the executable file can have different names. The extension of the source file should be “.java” and that for the executable file is always, “.class” automatically. The names that preceded the two extensions can be different. You create the source file with a text editor (e.g. notepad) and you save it with any name you want. The name for the executable file, for compiling and execution is obtain by you, from the word just after the word “class” in the source file code (see above code). So the two names can be different. However, I advice you to always use the same name for both files, for the sake of consistency, and modification of the program in future.

Well, that is what I have for this part of the series. We stop here and continue in the next part.

Chrys
NEXT

Related Articles

Java Course

Comments

Become the Writer's Fan
Send the Writer a Message