Elementary Java language features

By Jeff Friesen
Java is an object-oriented programming language, but there's more to Java than programming with objects. This first article in the Java 101: Foundations mini-series on elementary language features introduces some of the non-object-oriented features and syntax that are fundamentals of the Java language. Find out why Unicode has replaced ASCII as the universal encoding standard for Java, then learn how to use comments, identifiers, types, literals, and variables in your Java programs.
Note that examples in this article were written using Java 8.
download
Source code for "Elementary Java language features."
Created by Jeff Friesen for JavaWorld.

Unicode and character encoding

When you save a program's source code (typically in a text file), the characters are encoded for storage. Historically, ASCII (the American Standard Code for Information Interchange) was used to encode these characters. Because ASCII is limited to the English language, Unicode was developed as a replacement.
Unicode is a computing industry standard for consistently encoding, representing, and handling text that's expressed in most of the world's writing systems. Unicode uses a character encoding to encode characters for storage. Two commonly used encodings are UTF-8 and UTF-16. You'll learn later in this article how Java's support for Unicode can impact your source code and compilation.

Comments: Three ways to document your Java code

Suppose you are working in the IT department for a large company. Your boss instructs you to write a program consisting of a few thousand lines of source code. After a few weeks, you finish the program and deploy it. A few months later, users begin to notice that the program occasionally crashes. They complain to your boss and he orders you to fix it. After searching your projects archive, you encounter a folder of text files that list the program's source code. Unfortunately, you find that the source code makes little sense. You've worked on other projects since creating this one, and you can't remember why you wrote the code the way that you did. It could take you hours or even days to decipher your code, but your boss wanted a solution yesterday. Talk about major stress! What do you do?
You can avoid this stress by documenting the source code with meaningful descriptions. Though frequently overlooked, documenting source code while writing a program's logic is one of a developer's most important tasks. As my example illustrates, given some time away from the code, even the original programmer might not understand the reasoning behind certain decisions.
In Java, you can use the comment feature to embed documentation in your source code. A comment is a delimited block of text that's meaningful to humans but not to the compiler. When you compile the source code, the Java compiler ignores all comments; it doesn't generate bytecodes for them. Java supports single-line, multiline, and Javadoc comments. Let's look at examples for each of these.

Single-line comments

A single-line comment spans a single line. It begins with // and continues to the end of the current line. The compiler ignores all characters from // through the end of that line. The following example presents a single-line comment:

System.out.println((98.6 - 32) * 5 / 9);
// Output Celsius equivalent of 98.6 degrees Fahrenheit.

A single-line comment is useful for specifying a short meaningful description of the intent behind a given line of code.

Multiline comments

A multiline comment spans multiple lines. It begins with /* and ends with */. All characters from /* through */ are ignored by the compiler. The following example presents a multiline comment:

/*
 An amount of $2,200.00 is deposited in a bank paying an annual
 interest rate of 2%, which is compounded quarterly. What is
 the balance after 10 years?
 Compound Interest Formula:
 A = P(1+r/n)nt
 A = amount of money accumulated after n years, including interest
 P = principal amount (the initial amount you deposit)
 r = annual rate of interest (expressed as a decimal fraction)
 n = number of times the interest is compounded per year
 t = number of years for which the principal has been deposited
*/
double principal = 2200;
double rate = 2 / 100.0;
double t = 10;
double n = 4;
System.out.println(principal * Math.pow(1 + rate / n, n * t));

As you can see, a multiline comment is useful for documenting multiple lines of code. Alternatively, you could use multiple single-line comments for this purpose, as I've done below:

// Create a ColorVSTextComponent object that represents a component
// capable of displaying lines of text in different colors and which
// provides a vertical scrolling capability. The width and height of
// the displayed component are set to 180 pixels and 100 pixels,
// respectively.
ColorVSTextComponent cvstc = new ColorVSTextComponent(180, 100);

Another use for multiline comments is in commenting out blocks of code that you don't want compiled, but still want to keep because you might need them in the future. The following source code demonstrates this scenario:

/*
 if (!version.startsWith("1.3") && !version.startsWith("1.4"))
 {
 System.out.println("JRE " + version + " not supported.");
 return;
 }
*/

Don't nest multiline comments because the compiler will report an error. For example, the compiler outputs an error message when it encounters /* This /* nested multiline comment (on a single line) */ is illegal */.

Javadoc comments

A Javadoc comment is a special multiline comment. It begins with /** and ends with */. All characters from /** through */ are ignored by the compiler. The following example presents a Javadoc comment:
/**
 * Application entry point
 *
 * @param args array of command-line arguments passed to this method
 */
public static void main(String[] args)
{
 // TODO code application logic here
}

This example's Javadoc comment describes the main() method. Sandwiched between /** and */ is a description of the method and the @param Javadoc tag (an @-prefixed instruction to the javadoc tool).
Consider these commonly used Javadoc tags:
  • @author identifies the source code's author.
  • @deprecated identifies a source code entity (e.g., method) that should no longer be used.
  • @param identifies one of a method's parameters.
  • @see provides a see-also reference.
  • @since identifies the software release where the entity first originated.
  • @return identifies the kind of value that the method returns.
  • @throws documents an exception thrown from a method.
Although ignored by the compiler, Javadoc comments are processed by javadoc, which compiles them into HTML-based documentation. For example, the following command generates documentation for a hypothetical Checkers class:
javadoc Checkers
The generated documentation includes an index file (index.html) that describes the documentation's start page. For example, Figure 1 shows the start page from the Java SE 8 update 45 runtime library API documentation.

Figure 1. Java SE 8u45 runtime library API documentation was generated by javadoc.

Identifiers: Naming classes, methods, and more in your Java code

Various source code entities such as classes and methods must be named so that they can be referenced in code. Java provides the identifiers feature for this purpose, where an identifier is nothing more than a name for a source code entity.
An identifier consists of letters (A-Z, a-z, or equivalent uppercase/lowercase letters in other human alphabets), digits (0-9 or equivalent digits in other human alphabets), connecting punctuation characters (such as the underscore), and currency symbols (such as the dollar sign). This name must begin with a letter, a currency symbol, or a connecting punctuation character. Furthermore, it cannot wrap from one line to the next.
Below are some examples of valid identifiers:
  • i
  • count2
  • loanAmount$
  • last_name
  • $balance
  • π (Greek letter Pi -- 3.14159)
Many character sequences are not valid identifiers. Consider the following examples:
  • 5points, because it starts with a digit
  • your@email_address, because it contains an @ symbol
  • last name, because it includes a space
Almost any valid identifier can be chosen to name a class, method, or other source code entity. However, Java reserves some identifiers for special purposes; they are known as reserved words. Java reserves the following identifiers:


The compiler outputs an error message when it detects any of these reserved words being used outside of its usage contexts; for example, as the name of a class or method. Java also reserves but doesn't use const and goto.

Types: Classifying values in your Java code

Java applications process characters, integers, floating-point numbers, strings, and other kinds of values. All values of the same kind share certain characteristics. For example, integers don't have fractions and strings are sequences of characters with the concept of length.
Java provides the type feature for classifying values. A type is a set of values, their representation in memory, and a set of operations for manipulating these values, often transforming them into other values. For example, the integer type describes a set of numbers without fractional parts, a twos-complement representation (I'll explain twos-complement shortly), and operations such as addition and subtraction that produce new integers.
Java supports primitive types, reference types, and array types.

Primitive types

A primitive type is a type that's defined by the language and whose values are not objects. Java supports a handful of primitive types:
  • Boolean
  • Character
  • Byte integer
  • Short integer
  • Integer
  • Long integer
  • Floating-point
  • Double precision floating-point
We'll consider each of these before moving on to reference and array types.

Boolean

The Boolean type describes true/false values. The JVM specification indicates that Boolean values stored in an array (discussed later) are represented as 8-bit (binary digit) integer values in memory. Furthermore, when they appear in expressions, these values are represented as 32-bit integers. Java supplies AND, OR, and NOT operations for manipulating Boolean values. Also, its boolean reserved word identifies the Boolean type in source code.
Note that the JVM offers very little support for Boolean values. The Java compiler transforms them into 32-bit values with 1 representing true and 0 representing false.

Character

The character type describes character values (for instance, the uppercase letter A, the digit 7, and the asterisk [*] symbol) in terms of their assigned Unicode numbers. (As an example, 65 is the Unicode number for the uppercase letter A.) Character values are represented in memory as 16-bit unsigned integer values. Operations performed on characters include classification, for instance classifying whether a given character is a digit.

Comments