Now that you’ve had experience in creating a Java program, let’s make our programs more interesting by introducing the concept of data type and variables. We’ll also introduce the concept of comments, which help us tell other programmers what our programs do and what our code is trying to achieve.
Open up Eclipse and create a new class in your project called DataTypes.java. We’re not going to do anything to special in this program, we’re just going to introduce this concepts one by one.
Integers or ints are all positive and negative numbers including zero. We use this as the default data type for number values unless we have concerns about memory. Let’s create a int variable and give it a value. I’ll also use a single line comment to explain what a int is:
We create single line comments by starting them with //. As you can see we have two lines of comments and it would be better if we had a way to creating multiple line comments. We’ll explore that later.
Ints only allow values between -2,147483,648 and 2,147,483,648. Notice that unlike Strings, we don’t need to put double quotes around our int values, we just need to declare it.
Also, notice how our int has a name of ‘myFavNumber’. This is how we create variables, we declare a type, give it a name and MOST of the time we give it or initialize a value for it. There are some instances where we don’t initialze a value for our variables straight away, but we’ll come to that when we need to.
Booleans are true or false values. We use them when we want to set a value on or off, true or false etc.
A common convention for booleans is to start them with the word ‘is’. This isn’t mandatory, but it does help use determine whether we want to set out booleans to true or false when working with them.
Chars, or characters, are used to represent single characters. This would include a single character on a keyboard or any other character that you think of.
Let’s have a close look at different types of primitive numbers. Bytes are 8-bit signed two’s complement integers with a minimum value of -128 and a maximum value of 127. Bytes are used to save space in large arrays in place of normal integers because bytes are 4 times smaller than integers. Here’s how to initialize a byte:
Did you see the new type of comment? When we have comments covering multiple line, we use the /* symbol to start them and the */ to end them.
Shorts are twice as small as integers, which can help us save memory. The minimum value of shorts are -32,768 and the maximum value is 32,767. Let’s see it in action:
Longs are used when we need ints with a wide range. We initialize a long value with a L after the value like this:
Let’s move onto the decimal types. Floats are mainly used to save memory in arrays of float numbers. We don’t use floats for precise values. When we declare floats, we use f like this:
Finally let’s look at doubles. Doubles are used as the default data type for decimal values and like floats, we never use doubles for precise values. Here’s an example:
So there’s a fairly quick round-up of the primitive data types in Java. We also covered how to create single-line and multi-line comments in Java and we looked a little bit at variables. In the next post, we’re going to look at another data type, Strings.