Java With Us
Home   |   Tutorial   |   Programs  

Java Tutorial

Introduction to Java
Hello World Program
Variables and Data types
More about data types
Displaying text using print and println
Displaying text using printf
Java Comments
Naming conventions for Identifiers
Mathematical operations in Java
Taking input from the user

Classes and Objects
Introduction to object oriented programming
The constituents of a Class
Creating objects and calling methods
Get and Set Methods
Default constructor provided by the compiler
Access Specfiers
Scope and lifetime of Variables
Call by value and Call by Reference

A few more topics
Class as a reference data type
Constants or literals
Final variables
Increment and decrement operators
Manipulating Strings
Overloading constructors and methods
Static methods and variables
The Java API
The Math class
this keyword
Wrapper classes

Control Structures
Control Statements
Repetition statements
Nested loops
Formulating algorithms
Branching Statements

Arrays introduction
Processing arrays using 1oops
Searching and sorting arrays
Array of objects
Multi dimensional arrays
Taking input as command line arguments
Using ellipsis to accept variable number of arguments

Inheritance introduction
Relation between a super class and sub class
Final classes and methods
The protected access specifier
Class Object

Abstract classes and methods

Exception handling
Exception handling introduction
Exception hierarchy
Nested try catch blocks
Throwing exceptions

Java comments

Classes and programs written by you are used not just by yourself but by others too. Under such circumstances, it may happen that certain portions of the code you have written may not be easily understandable by others. This could be an algorithm that you have used or an approach that is part of your class design. In order to explain all such aspects to someone reading your code, Java provides comments. Comments are sentences which are ignored by the compiler. These comments at times may also be helpful to you too, especially in big projects where remembering the purpose of every variable and method might be a difficult task. Java provides three types of comments: single line comments, multiline comments and documentation comments. We will now see how these comments can be incorporated in our program by taking the example of our Addition program which we have written in the beginning of this tutorial.

Single line comments, as implied by the name span over a single line. They start and end on the same line. Two forward slashes ( // )mark the beginning of a single line comment and a new line ( obtained by pressing the enter key) marks its end.

int firstNumber = 3; // This is the first number

Multiline comments spread over many lines. They start with /* and end with */

/* This is
a multiline
comment */

Documentation comments start with /** and end with */. These are special types of comments which are used to document a class. The documentation of a class gives details about its variables, constructors and methods. We shall look into documentation comments later on. For now, we shall see how we can use single line and multiline comments to improve the readability of our program.

This class is used to add
Two numbers
public class Addition {
   public static void main (String args[] ) {
      int firstNumber = 3; // the first number
      int secondNumber = 4; // the second number
      int result = firstNumber + secondNumber ; // the sum of two numbers
      System.out.println ( "The result of adding "+ firstNumber + " and "+ secondNumber + " is "+ result );
   } // end of main method
} // end of class

As you can see in the program above, we have use single line comments to describe the purpose of the variables and also to state what a particular curly brace correspond to. We often use shorter variable names in programs. In order to state the purpose of the variable, we use comments. And in lengthier programs, it becomes difficult to comprehend to what a particular closing brace corresponds to- the closing of a method, a class or some other block. Hence, comments are also used to mark the end of blocks. Blocks are a group of statements within a pair of curly braces. And we have used a multiline comment in the beginning to state the purpose of this program.

When comments appear in Strings, they are not considered as comments. Instead, they form a part of the String itself. Look at the example program below:

public class Example {
   public static void main(String[] args){
      String s="Hello! // This is not a comment";

When this program is run, we get the output as:

Hello! // This is not a comment

The comment within the String was not parsed as a comment. Rather, it was a part of the String itself.

When Unicode characters appear in comments, they are replaced by their corresponding graphic symbol. As we have seen already, Unicode characters are represented by numbers between 0 and 65536. We can also represent them by a backslash character followed by a set of characters. For example, the Unicode character '\u000a' represents a new line. Wherever, this Unicode character appears, it is replaced by a new line and then the code is executed. Look at the following program for example:

public class surf {
   public static void main(String[] args){
      int i=3456; // I is set to 10 \u000a System.out.println(i);

The output would be


The reason is that \u000a was replaced by a new line and then the code was run.

Privacy Policy