## Episode 7: Primitive types

Welcome back to Java tutorials. In this episode you will return back to Eclipse and start coding in Java again. At the end of this episode, Java primitive types will have practically no secrets for you. I suppose that you are now quite armed with a good knowledge of binary numbers to attack this episode. Anyway, you always have Episode 6 in case you have something to clarify or questions to ask regarding numeral systems and base-2 system in particular. So feel confident and let’s go.

### Digital information units

Have you ever heard of words byte, kilobyte, or megabyte? You probably did. These are actually units used to count an amount of digital information. Like How much memory or disk storage your computer has at its disposal.

To simplify things, digital information is information made of bits. In fact, a bit represents the smallest amount of information a computer can deal with (the value of a bit stores one or zero). Actually it is two small that it is more practical to use its multiples : byte, kilobyte, megabyte and gigabyte.

The first multiple of bit is byte (or B): it consists of 8 bits

The next multiple is kilobyte (or KB). Normally the prefix kilo in base-10 system means 1000 times. But since we are dealing with binary data we need to use a power of 2 instead of a power of 10. The chosen power of two has been 1024 (which is 210)  because it is quiet close to 1000. So now you know that a kilobyte is equal to 1024 byte.

Using the same logic a megabyte (or MB) can be seen as a kilo-kilobyte or 1024  times 1024 times a byte (220 byte)

And let’s stop at the gigabyte (or GB)which is 230 byte. There are still other multiples but these are the most used.

The above table summarizes how to convert between the different seen units of digital information starting from byte (again the bit is so small to be used ). Do not be scared by the negative singes. 1B =2-20 MB means simply that 1B =1 / 220 MB

### int

You are already used to the type int from previous episodes. It is the most used primitive type in Java. The primitive type int represent signed integers on 4 bytes (that is 32 bits) therefor it can represent numbers from -231 (which is equal to -2147483648) to 231-1 (which is equal to 2147483647) inclusive.

Here is a usual declaration and use of an int variable :

```1 2 3 4 5 6 7 8 9 10 public class Main {     public static void main(String[] argv)     {         int a = -2147483648;         int b = 2147483647;           System.out.println("The int type can represent numbers from " + a + " to " + b + " inclusive");     } }```

The result is :

`The int type can represent numbers from -2147483648 to 2147483647 inclusive`

Look at what will happen if you try to represent the 2147483648. try the following program on Eclipse :

```1 2 3 4 5 6 7 8 9 public class Main {     public static void main(String[] argv)     {         int a = 2147483648;           System.out.println("value of a is : " + a);     } }```

You get a nice compilation error isn’t it ?

The compiler tells you clearly that the literal number 2147483648 cannot be represented by an int variable because it is outside the range. Again, the smallest number an int variable can hold is -2147483648 and the biggest is 2147483647. If you try to assign a value which is outside of this range (like 2147483648 or -2147483649) you get a compilation error right away :).

### byte

You already know what a byte is. But add to that that Java has a primitive type called byte which represents signed integers on one byte (8 bits). This is the smallest primitive numeric number in Java. It can represent numbers from -27 (which is equal to -128) to 27-1 (which is equal to 127).

Here is an example of declaring and using a byte variable :

```1 2 3 4 5 6 7 8 9 10 public class Main {     public static void main(String[] argv)     {         byte a = -128;         byte b = 127;           System.out.println("The byte type can represent numbers from " + a + " to " + b + " inclusive");     } }```

And the result is :

`The byte type can represent numbers from -128 to 127 inclusive`

Again if you try to assign a value greater to 127 or less than -128 you will get a compilation error, but it will be different from the last one we get with int. Try this program on Eclipse:

```1 2 3 4 5 6 7 8 public class Main {      public static void main(String[] argv)      {           byte a = 128;           System.out.println("The value of a is : " + a);      } }```

Here is what you get on Eclipse

This time the compiler tells you that he cannot convert from int to byte. Why ? Because a literal number is by default of type int. Here the compiler finds the literal 128 which cannot be of type byte since it dose not belong to the range -128..127 so he considers automatically that it is of type int. And trying to assign an int to a byte is like trying to contain the water of a bottle in a 4 times smaller cup. You just can’t do it :).

### short

This is another Java integer primitive type. It represents signed integers coded on two bytes (16 bit) so its range is from -215 (which is equal to -32768) to 215-1 (which is equal to 32767) inclusive. Here is an illustrative program :

```1 2 3 4 5 6 7 8 9 10 public class Main {     public static void main(String[] argv)     {         short a = -32768;         short b = 32767;           System.out.println("The short type can represent numbers from " + a + " to " + b + " inclusive");     } }```

And the result is :

`The short type can represent numbers from -32768 to 32767 inclusive`

### char

This is a tricky one. Java has a primitive type called char which can represent unsigned integer numbers coded on two bytes (16 bit). So Its range is from zero to 216-1(which is equal to 65535) . But here is the trick: the word char stands for character. In addition to being seen as an integer number a char value is also seen as a character. And unlike the other numeric types, the string representation of a  char value is not the integer value it is representing but the corresponding character.

You are lost, aren’t you ? The table below will clarify things :

The table above gives the correspondence between some integer values you can assign to a char variable (codes) and the resulting string representation of that char variable. So for example if you the code  71 corresponds to character ‘G’. This means that if you initialize a char variable with the value 71 and you try to print its content then what you will get is G (not 71). Let’s give it a try :

```1 2 3 4 5 6 7 8 9 public class Main {     public static void main(String[] argv)     {        char c = 71;          System.out.println(c);     } }```

And the result is amazingly :

`G`

The previous program supposes that you know that 71 is the code for character ‘G’. But what if you don’t ? Well, there is amuch simpler way to declare a charcater using char literals. A char literal is just the character you want to assign (G in this example) enclosed by simple quotes.

Here is our modified and simpler program :

```public class Main {     public static void main(String[] argv)     {        char c = 'G';          System.out.println(c);     } }```

And the result is the same :

`G`

### long

Now what if you are performing some computations on numbers bigger than 2147483647 (the limit of int) ? No problem, Java has his biggest primitive integer type in backup: the long type. long represents signed integer types on 8 bytes (that is 64 bits which is the double of int 32 bits) and can represent values from -2-63 (which is equal to -9223372036854775808 wow 😯 ) to 263-1 (which is equal to 9223372036854775807 wow 😯 ). I don’t know what you are planing to calculate but those are really huge numbers ! Anyway, here is an illustrative program :

```1 2 3 4 5 6 7 8 9 10 public class Main { public static void main(String[] argv) { long a = -9223372036854775808; long b = 9223372036854775807;   System.out.println("long can represent integers from " + a + " " + b + " inclusive"); } }```

And the result is … two compilation errors 😀

Actually it is the same error but twice :mrgreen:. Do you remember what we said ? Integer literals are by default of type int, and since those huge values are out of range of type int, you get a nice compilation error telling you so

But how to tell the compiler that the literals -9223372036854775808 and 9223372036854775807 are not of type int but of type long ? The answer is: add the letter L to them so that you obtain : -9223372036854775808L and 9223372036854775807L. Actually you can use small letter l as well but I don’t recommend it at all since small letter l can be, depending on the used font, confused with the digit 1 :).

The resulting program is :

```1 2 3 4 5 6 7 8 9 10 public class Main {     public static void main(String[] argv)     {        long a = -9223372036854775808L;        long b = 9223372036854775807L;           System.out.println("long can represent integers from " + a + " to " + b + " inclusive");     } }```

And the result is :

`long can represent integers from -9223372036854775808 to 9223372036854775807 inclusive`

### float and double

The float and double types represent respectively simple and double precision floating numbers. Which means non integer numbers like 1.5 or 0.23. They are coded respectively on 4 bytes and 8 bytes (that is respectively 32 bits and 64 bits) but in a way which is different from the way integer types are coded. Using a clever “scientific” format, float is capable using the same number of bits as int, of representing much larger(or smaller ;)) values. Here is for example the largest positive number representable by float :

`3.4028235E38`

The above number is to be read as follows: 3.4028235 × 1038 or if you want :

`340282350000000000000000000000000000000`

That is even so much larger than the largest number representable by long.

Here is the smallest positive number representable by float (remember that zero is not positive ):

`1.4E-45`

Which is equal to 1.4 × 10-45 or if you want :

`0.0000000000000000000000000000000000000000000014`

double can represent even bigger numbers and with more precision (more digits after the decimal point)

To declare a float or double variable, I recommand to always use the suffixes F and D (like L for long). Here is an example :

```1 2 3 4 5 6 7 8 9 10 public class Main {     public static void main(String[] argv)     {         float f = 156.6778F;         double d = 130.97D;           System.out.println("f : " + f + "  d : " + d);     } }```

The result is simply :

`f : 156.6778  d : 130.97`

We will see double and float in more details in a future episode.

### What is next ?

There still a last primitive type in Java to which we will dedicate the next episode. It is the simplest type in Java: boolean. A boolean variable can only have two possible values: true or false. This means that true and false are the only two literals of this type.

Here is a very simple example of use :

```1 2 3 4 5 6 7 8 9 10 public class Main {     public static void main(String[] argv)     {         boolean yes = true;         boolean no = false;           System.out.println("yes : " + yes + "    no : " + no);     } }```

The result is :

`yes : true    no : false`

The rest is for next time :). See you.

January 18, 2013 at 22:55 | Java tutorials| Be the first to comment this post