Introduction to Java : Data Types and Operators Part-1


It is often the case that you want to include comments within your Java code. Comments are little bits of documentation embedded in the actual code that is invisible to the Java compiler but which can be read by a person reviewing your code.

Comments help to document what it is that your code does and how someone should understand the design if they want to modify something. Comments are essential if someone else is going to work with you or take from where you’ve left off.

Comments also help you think about inefficiencies in your own code. Trying to explain how a bit of code works to someone new to it is always one of the best ways to catch your own mistakes. Often the act of documenting helps you realize that you’ve done something in an awkward way. If your code is clean and makes sense, it should be easy to document. If your code is hard to document, perhaps you’d better think about rewriting!

Finally, it is important to learn the commenting style over time. For example, there is little reason to say:
i++; // increment i by one!

Spend your time commenting on how routines work in the large rather than each individual line. Of course, if there are some gnarly single lines of code in your application, feel free to document them. Use your best judgment and find your own voice!

There are actually several ways to denote comments in Java

For single-line comments, you can use the “//” notation. For example, consider the following code:

Note that single line comments can be defined on the same line as your code or on their own line

To specify multiline comments, however, you will use the “/*” and “*/” combination such as in the following example:

As you can see, if you use the “//” notation and your comment spans multiple lines, you must begin each new line with a “//” combination.

Finally, if you are writing comments that should be included as part of a Javadoc document, you should use the “/**” “*/” combination such as in the following example:

Printing to Standard Output

Okay, as you may have guessed from the code in the last section, you can direct Java to print to standard output using the System.out.println() method.

We will talk more about the specifics of methods, but for the time being, it would be good for you to know that you can print to the command line (applications) or to the Java Console (applets) using the following:

For example:

Below is a screenshot of the Netscape Java Console.

[Java Console]

Note that you can access the Java console in Netscape using the Communicator->Java Console menu item. In IE, on the other hand, you actually need to instruct the browser to give you access. You can do this by going to the Tools->Internet Options->Advanced->VM menu item and click “Java Console Enabled”. Then you can open the Java Console using View->Java Console.

[Java Console in IE]

The System class has quite a few methods that allow you to do more than just print of course. To read about these methods, simply use the online documentation. We will talk about how to efficiently use the online documentation a little bit later but you should know that you can find the System class in the java.lang package.


In any programming language, variables provide the foundation upon which all else is built. You can think of a variable as a “place holder”, or a “name” that represents one or more values. In essence, it is a data structure that is provided by the language itself.

The generic syntax for defining variables is as follows:

Thus, for example, we might assign the value of twenty-seven to the variable named “age” with the syntax:

From then on, unless we change the value of age, the script will translate it to twenty-seven, acting as a placeholder for the value.

So if we then say:

The Java Console will display the value “27”.

Naming Variables

In Java, you can name your variables anything you like so long as they are not a “Java Keyword” and contain only characters within the set of Unicode characters. However, a good practice is to use characters within the ranges of “A-Z, a-z, 0-9, or _”.

Of course, variable names should help you understand what is happening in your program. Thus, it is useful to name your variables intelligently, such as “firstName”.

Notice that we created a variable firstName in which the first word was lower case, the second word began with an uppercase letter and there were no spaces. This is pretty standard practice in Java and naming your variables like this is a good habit for you to get into.

Note: many developers use an underscore to prefix private variables such as “_adminName” and ALL_UPPERCASE to denote statics.