At this point, we are actually done with the introduction of java programming. Yes, you read it right, all the things we have discussed so far are all introduction. We have not discussed yet in detail on how to pr0gram in java.

If you are still not able to grasp the concept of OOP, please go back on previous java tutorial because that is one of the core foundation of java.

Let’s now begin to discuss the technical details of javal. Let’s start first on the primitive data types.

Why we need to be concerned on Data Types?

All variables in java programming are somewhat special. Java variable are statically type in nature which means that before we can use it, the variable must be declared. When we say that it must be declared first means that we should assign which data type does the variable belongs to.

For example, we cannot just say x=0; and used the value of , instead we have to code like this int x=0;. This tells the compiler that i have a variable with name x of int data type with value 0. We will go through each primitive data type that we can use.

A primitive data type is basically not an object. We don’t instantiate them. They are special build specifically for java programming language.

There are 8 primitive data type which we can choose from depending on our requirements. We will go over each and every data type together with specific behaviors.

byte

A byte data type is an 8 bit signed two’s complement integer. It’s range is from -128 to 127 number. So basically this is the smallest data type which we can use. Because it’s range is very small so as it’s memory consumption. So this become essential if memory saving is concern. I can only see the usage of this data type in handling arrays or other collections. Well under normal circumstances, we rarely see arrays that past 100 indexes, so this byte data type becomes handy in handling the index. But let me put a note here, that if you are in doubt of how many indexes does your array will contain, always use int to handle the index.

To make this clearer, below is an example of using byte in handling index of an array.

package com.javatutorial.example;

public class ByteExample {
	/*
	 * This is an example code in
	 * using byte as index of array
	 */

	public static void main(String[] args) {
		String[] fruits = new String[]{"apple","orange","guava"};
		for(byte index=0;index<fruits.length;index++){
			System.out.println(fruits[index]);
		}

	}

}

Well from the above example we have declared a variable index of type byte and then use it to get the contents of an array. Well in this case it’s sensible to use byte because i am expecting my list of fruits not to reach the 128 threshold.

But what if we breach the threshold of 127? What would be the value of the index? It would be -128. In short, we should be very careful in considering the threshold of each data type we have been using.

short

The primitive datatype short is 16 bit signed two’s complement integer. Basically this is a lot larger compared to byte with a range of -32, 768 to 32,767.  But this is still low if you will be using it for computations. It’s use is similar to byte but a short can handle larger arrays.

int

The int data type is 32-bit signed two’s complement integer. It’s range is from -231 231 – 1. This is the most used data type among the list of primitive data type because it’s range is quite about right. Regardless of memory savings in using short or byte, this is my preferred choice to hold my values. Because the range is quite reasonable, it’s not too low and it’s not that high and can do almost all computations, that’s why this data type is often preferred in dealing with whole numbers.

long

The long data type is 64 bit signed two’s complement integer. Basically a long data type has a range of -263 263 – 1. Basically this is the largest data type that you can use in dealing with int. This is rarely used because the int data type which is already large enough to handle all whole number calculations. But this will be handy if you are expecting to breach the int range.

float

The float data type is a single-precision 32-bit IEEE 754 floating point. This is good in dealing with calculations involving numbers with decimal digits. The same as byte and short this is a preferred data type in storing values for arrays and other collection. This is preferred over double, if memory savings is in question. Just make sure that the values will not breached the range available for float. In situations where precision is required, double data type is preferred.

Make a note that in dealing with currency, where absolute precision is required, using float is not advisable rather BigDecimal is recommended to be used.

double

The double data type is a double-precision 64-bit IEEE 754 floating point. In handling with decimal, this is a preferred choice because it is more precise than float. Same as float, using double data type where data is precision sensitive, it is highly discouraged. In dealing with currency, it is recommended to use BigDecimal.

Literals

As we have said earlier, primitive data types are specially built to java language. They are not derived from any class. A literal is the source code representation of a fixed value; literals are represented directly in the code without requiring computation. It should be noted that a literal can directly be assigned to data type as shown below:

		int x = 32;
		char c = 'a';
		float f = 1.32;

Integer Literals

By the default an integer data type is int. However appending a l or L at the end of the number makes the data type as long. As a general practice make use of the uppercase L instead of the lowercase for readability. The l looks like a 1, so it would make sense to prefer the use of the uppercase.

Values of the integral types byte, short, int, and long can be created from int literals. Values of type long that exceed the range of int can be created from long literals. Integer literals can be expressed by these number systems:

  • Decimal: Base 10, whose digits consists of the numbers 0 through 9; this is the number system you use every day
  • Hexadecimal: Base 16, whose digits consist of the numbers 0 through 9 and the letters A through F
  • Binary: Base 2, whose digits consists of the numbers 0 and 1 (you can create binary literals in Java SE 7 and later)

Below are some examples that cover number system declaration

		// decimal format
		int valDecimal = 121;
		
		// this is the equivalent of 121 to hex
		int valHexadecimal = 0x79;
		
		// equivalent of 121 in binary
		int valBinary = 0b01111001;

If you print the values of valHexadecimal and valBinary, both will result to 121. If you want to do some conversion yourself, you can check out this online conversion table from decimal to several number bases.

Floating Point Literals

The floating point literals by default, is of type double not unless it is appended with F or f which in this case become a type float. Appending d or D is optional to the literal to make it type double because by default this literal is of type double. You have to be very careful on this. If you have a variable of type float, the literal value that should be assigned must have the notation f at the end of the number otherwise it will not compile. This is because by default it is of type double. For example: float f = 1.2;  This will not compile, because 1.2 is by default of type double, and you cannot assign a double to float data type.

Floating point literals is supporting scientific notation e or E, regardless if it’s type float or double.

		// floating type literals
		float width = 1.32f;
		
		// floating type literals of type double
		double length = 1.27;
		
		// floating literals that support scientific notation
		float area = 1.24e2f;
		
		float value = 1.24e32f;

 

If we try to print the  width and length variable from the above example, the values are printed normally. But how about the variable area, will it print 1.24e2? No, it will print the equivalent 124 which is the equivalent of 1.24e2. The variable value will just print 1.24e32, because printing a large number doesn’t make sense at all. This is the same behavior as that of your calculator. If the printed values are too large to fit on the screen of your calculator, the number will be expressed in scientific notation.