Java: Your First Program

At this point, it is essential to go through the a basic java program to learn the essentials.

Package

The program begins with the following statement:

package welcome;

The information in the welcome package is transferred to the compiler.

A package refers to a group of associated classes and interfaces. Do not worry if these topics don’t sound familiar, as they will be covered in later lessons.  

Upon writing package welcome, the compiler is being directed to incorporate this file in the welcome package. The compiler creates a folder called “welcome” and saves it in that folder. All the files of the same package are stored in the same folder.

Upon navigating to the “NetBeansProjects” folder, a folder titled “Welcome” will appear. Typically, the “NetBeansProject” folder is present in the “Documents” folder. In case this folder is not found, leverage the search function on the computer.  

Look for the “src” folder within the “Welcome” folder, which, in turn, will contain the “welcome” folder.

In this folder, the files of the welcome package are present. Package names are always in lowercase. Do remember that Java is a case-sensitive language.

Therefore, “Welcome” and “welcome” are not the same. 

The primary benefit associated with declaring packages is to avoid naming conflicts. Two or more files can be named the same if they are a part of different packages. This concept is similar to how two or more files with the same name can be stored on a computer if they are saved in separate folders. Later, the lesson delves into the process of creating different packages. 

Apart from the packages that are created by coders, there are several pre-existing packages in Java, which offer directly usable code. For example, the java.io package contains code for input and output, while in Java.awt package, code is present in bundles for the implementation of a graphical user interface. Before use, coders must import them. The steps to do that are discussed a little later.  

The Welcome Class

Now, the lesson will look closer at the Welcome class. In the example above, curly braces are used to determine the beginning and end of the class. Every opening bracket must come with a corresponding closing brace.

public class Welcome { // start of Welcome class
  // rest of the code
} // end of Welcome class

The main() method is present within the Welcome class.

The main() Method

The following line of code is the most confusing part for beginner programmers. For now, remember that the main method is required for each program to run. Thoroughly understanding every part of this line means that you’re a seasoned programmer.

public static void main(String[] args) { // start of main method
  // rest of the code
} // end of main method

For every Java application, the main() method is the entry point. Each time a Java application is started, the calling of the main() method occurs. 

The words String[] args, within the parenthesis of the main() method, can take an array of strings as input. The concept of arrays will be discussed in a later lesson. 

There are only two lines of code in the main() method. The first line, which reads    

//Print Welcome to Java Programming! 

The compiler completely ignores this comment. The second statement is  

System.out.println("Welcome to Java Programming!");

Consequently, the output window showcases the result, “Welcome to Java Programming!” This statement ends with a semi-colon. In Java, every statement must end with a semi-colon (;), similar to C and C++.

Finally, two closing braces are included following the System.out.println(“Welcome to Java Programming!”); statement to indicate the end of the earlier opening braces.

Comments

Let’s take a look at comments.

It was previously stated that the compiler ignores 

//Print Welcome to Java Programming!

because it is a comment.

In simple words, a comment is not a part of the program and is only incorporated to enhance its readability. Therefore, the compilation of comments into bytecode does not occur.

To add comments, incorporate two forward slashes (//) in front of each line in the following manner: 

// Example of a comment
// Another example of a comment
// Final example of a comment

Alternatively, /* … */ can also be used for multiline comments like this

/* Example of a comment
Another example of a comment
A final example of a comment
*/

Lastly, comments can be placed at the end of the line as follows

System.out.println("Welcome to Java Programming!"); // Prints to the screen.

These types of comments are used often to clarify the specific line of code that it follows.

Leave a Reply

%d bloggers like this: