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:

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.

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:

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:

Will produce the following output:

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:

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.

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

Pierre Pierre, who replica handbag 185cm tall, gucci replica handbags the "Diamond King" in his hermes replica handbags . He has been selected as one of the handbag replica most beautiful 50 people in the "People" magazine. It is also known as replica handbags most elegant and quiet in the world. Unforgettable prince.