Initial Steps Towards Java

Before starting with how to write or steps to write a java program, we should initially know about the comments used while writing a program. Comments are description about the features of a program which means that whatever we write in a program should be described using comments. When we write comments, we can understand what the program is doing as well as it helps others to easily follow our code.

Comments

There are three types of comments in java which are described below

  • Single line comments - These are used for making single line as a comment. These start with double slash symbols and after that whatever is written will become a comment till the end of that line.
//Java is awesome
  • Multiline Comments - These are used for representing several lines as comments. These start with /* and ends with */. In between these whatever is written will become a comment.
/*Java is a programming language
  Java is awesome
  Java is object-oriented language*/
  • Documentation Comments - These comments are used to provide description for every feature in a java program. These start wit /**and ends with */.
/** description about class */
class code

Starting Java Program

Importing Classes

When we are writing any program, the first line would generally be (for suppose take in case of C/C++)

#include<stdio.h>

The above sentence means that a request is made to the compiler to include the header file <stdio.h>. Header file is a file, which contains the code of functions that will be needed in a program. In java program we use two classes namely system and string. These classes belong to java.lang package. To import these classes into our program we use

import java.lang.*; 

Here * means all the classes and interfaces of that package are imported into our program.

Writing A Class

Since Java is purely an object-oriented language, we cannot write java program without having at least one class or object. A class code starts with a { and ends with a }. A class contains variables and methods. We can create any number of variables and methods in a class. We will check out the main method now which is a compulsory method. Main method is important because without main method the JVM will not start the execution of a program.

public static void main (string args[])

Java Main Method

Public

JVM is a program written by Javasoft people and main() is the method written by us. Since, main method should be available to the JVM, it should be declared as public. If we doesn’t make itself available to JVM it cannot execute it.

Static

Static methods are the methods, which can be called and executed without creating the objects. Since we want to call main() method without using an object, we should declare main() method as static.

Void

If a method is not meant to return any value it is written as void. As main method doesn’t return any value, void should be written before main method.

Main

Main method is the starting point for JVM to start execution of a program. Without main method JVM will not start the execution of the program.

String args[]

Here args[] is the array name and it is of string type. This means that it can store a group of strings. This array can also store a group of numbers but in the form of strings only.

Printing The Method

To print the method we use system.out.println. Here system is the class name and out is a static variable in system class. Out is called a field in system class. When we call this field, a printStream class object will be created internally. System.out.println gives the printStream class object. This object by default represents the standard output device, i.e the monitor. So, the string will be sent to the monitor.

Difference between print() and println()

Both methods are used to display the result on the monitor. print() method displays the result and then retains the cursor in the same line, next to the end of the result. println() method displays the result and then throws the cursor to the next line.

Subscribe For More Content