In part two of this ten part series, I discussed some basic Java language syntax. In this article, I will discuss some of the Java primitive types and describe the basics of a Java class. I will also touch on the JDK (Java Development Kit) and describe how to compile a small class.
Java Part 3: Primitive Types - Primitive types in Java (Page 2 of 6 )
There are a variety of primitive types defined in the Java language. They are common to the C/C++ language, and are described below. There are a number of complexities and intricacies associated with these types, but for now the basics will suffice.
The boolean Type
In Java and other languages, the Boolean type represents a truth value with two possible states: On or off, true or false. Booleans are used in conditional statements such as if and else, while and the test condition in for loops.
The char type
The char type represents a single unicode character. It is the building block of Strings, which are used to represent text in Java. You can declare a new char variable, c, with the value of a, like this:
char c = 'a';
There are many useful functions for working with chars that are built into the Java language, such as isDigit() and isLowerCase(). Functions like these return Boolean values, but there are various other functions that return other types of primitive values.
The int type
Numerical types in Java fall into many categories, such as byte, short, int and long. For now we will focus on the int type, which is the most commonly used numerical type.
The int type represents whole integer values ranging from -231 to 231 – 1. Int’s are natural numbers and can be both positive and negative.
The float types
Real numbers with fractional components are represented in Java as either float or double values. Floats are represented in Java as 32 bit values and, as the name suggests, doubles are 64 bit floating-point values.
The String type
In Java, the string type is not really a primitive type; it is a class. However, due to its popularity, it was added to the java language so that it could be used in programs as simply as the previously mentioned primitives. One very handy addition to Java is the fact that the String class has many methods to manipulate and deal with Strings.
Arrays
If you have any experience with C/C++, then you will be glad to know that arrays in Java are the same as arrays in C. We can declare an array that will hold 100 int's with this line of code:
int[] array = new int[100];
There are various other ways to declare the same array structure, but this one will do for now, because it is identical to the syntax used by the C compiler. All indexes in the array are initialized to zero by default. We can set the value of each index in the array with a for-loop construct, like this:
int i;
for( i = 0; i < 100; i++ ){
array[i] = i;
}
The first position in the array is indexed at zero, and the final position in the array is the size of the array – 1; in this case, 100 –1, or 99. So, the array now holds the integer values 0 to 99. Arrays can hold primitive types as well as objects.
Now that we have a good understanding of some of the primitive types that are available to us in Java, let’s write a very simple class and compile it.