Primitive data types

To write any kind of useful program, we are going to need places to store information.  To do this we will create objects called variables which can consist of many different types.  Let’s start by introducing a variable type that will store an integer. Change the main() function of your HelloWorld class as follows:

The first line we added:

	public static void main(String[] args) 
	{
		System.out.println("Hello World");
		
		int i;
		
		i = 5;
		
		System.out.println("i = " + i);
	}

Let’s take a look at this line by line.  Line 5 declares the variable ‘i’ as a type int, which tells Java to allocate space to store a small integer number.  Note that at this point the value of ‘i’ is not defined.  On line 7, we set the value of ‘i’ to the number 5. Finally on line 9 we output some information to the console using the System.out.println function.  Notice that in this case we have a literal string “i = “ combined with the variable ‘i’ using the addition operator ‘+’. When used with strings, the addition operator will concatenate the two strings.  However most java objects have a text representation and when concatenated with a string, that text representation will be used. In the case of the int type, the value currently assigned to the variable will be converted to an ascii string and concatenated with the remaining strings.

So when we run this program, we should see the following output in the Console window at the bottom.

Hello World
i = 5

The first line is from our original println call.  The second line first printed out the literal string “i = “ and then converted the current value of the variable ‘i’ (which in this case was the number 5) to an ascii string and printed that next.

Now we created the variable ‘i’ on one line, and initialized it later.  However, it is possible to combine those two on a single line as follows:

	public static void main(String[] args) 
	{
		System.out.println("Hello World");
		
		int i = 5;
		
		System.out.println("i = " + i);
	}

Now line 5 both declares the variable ‘i’ and initializes it to the value of 5.

The data type int is an example of one of the eight primitive data types supported by Java.  The complete list of data types is as follows:

  • byte: The byte data type is an 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).
  • short: The short data type is a 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).
  • int: By default, the int data type is a 32-bit signed two’s complement integer, which has a minimum value of -231 and a maximum value of 231-1.
  • long: The long data type is a 64-bit two’s complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1.
  • float: The float data type is a single-precision 32-bit IEEE 754 floating point. It has ~6 significant digits.
  • double: The double data type is a double-precision 64-bit IEEE 754 floating point. It has ~15 significant digits.
  • boolean: The boolean data type has only two possible values: true and false.
  • char: The char data type is a single 16-bit Unicode character.

The four types, byte, short, int, and long all represent integers, so which should you use and when?  Then answer is that generally you should always use int unless you have a good reason otherwise.  For example the only good reason to use byte or short is if you have a large amount of data to store and you are concerned with how much memory it will consume (of course, the numbers you need to store in them must be small enough!).  The reason you would use a long type would be because you know that you will using integers which will be too large to store in an int.

Similarly, there the two types, float and double both represent floating point numbers. Generally you should always use double unless you are concerned with how much memory is consumed in which case using a float might be appropriate.

Each of these types can be declared, initialized and printed in much the same way as we did with the int type above.  For example, the following code:

	public static void main(String[] args) 
	{
		System.out.println("Hello World");
		
		int i = 5;
		byte b = 7;
		short s = 44;
		long l = 22;
		float f = 1.34f;
		double d = 2.72;
		boolean bool = true;
		char c = 'x';
		
		System.out.println("i = " + i + " b = " + b + " s = " + s + " l = " + l + 
				" f = " + f + " d = " + d + " bool = " + bool + " c = " + c);
	}

Will produce the following output:

Hello World
i = 5 b = 7 s = 44 l = 22 f = 1.34 d = 2.72 bool = true c = x

Notice how we can simply concatenate as many of these variables together to make one long string.  There is an alternate method of displaying this data which is useful to know.  The String class has a function format which allows us to better control the format of the data we are outputting.  For example, if we wanted to display only one decimal place on the floating point numbers we could use String.format(“%.1f”, f). Using this function we can format the previous example with:

	public static void main(String[] args) 
	{
		System.out.println("Hello World");
		
		int i = 5;
		byte b = 7;
		short s = 44;
		long l = 22;
		float f = 1.34f;
		double d = 2.72;
		boolean bool = true;
		char c = 'x';
		
		System.out.println("i = " + i + " b = " + b + " s = " + s + " l = " + l + 
				" f = " + f + " d = " + d + " bool = " + bool + " c = " + c);
		
		System.out.println(String.format("i = %d, b = %d s = %d l = %d f = %.1f d = %.3f bool = %b c = %c", 
				i, b, s, l, f, d, bool, c));
	}

Which will produce the following output. Note that in this case we have limited the display of the ‘f’ variable to one decimal point and expanded the display of the ‘d’ variable to three decimal points.

Hello World
i = 5 b = 7 s = 44 l = 22 f = 1.34 d = 2.72 bool = true d = x
i = 5, b = 7 s = 44 l = 22 f = 1.3 d = 2.720 bool = true c = x

The first argument of the format function is a string which lays out how the data is to be presented.  Most of the characters in the string will be output exactly as you see them, however the special character ‘%’ is used to mark positions in the string where data will be placed.  For example ‘%d’ will take an integer value and convert it to it’s string equivalent.  Some of the possible formatting characters are:

  • %d: Specifies an integer data type (i.e. either byte, short, int, or long).
  • %f: Specifies a floating point type (i.e. either float or double).
  • %b: Specifies a boolean type
  • %c: Specifies a char type
  • %s: Specifies a String type

After the format string there needs to be one argument (of the appropriate type) for each of the format character in the string.  The advantage of using this technique over simply concatenating the variables is that it is easier to see what the output string will look like, and you have more control over the formatting.

Next: Console Input