Skip to content
Ryan James
Menu

← Java10 min read

Primitive Data Types and Objects

Primitive data types are different depending on the programming language that you're working in. A primitive data type is best to be thought of as a variable where the data that is assigned to it is literally assigned that in the computer memory.

Primitive Data Types and Objects

Primitive data types are different depending on the programming language that you’re working in. A primitive data type is best to be thought of as a variable where the data that is assigned to it is literally assigned that in the computer memory. There are times when you don’t want this to be the case and that’s what objects are used for. In the Java programming language, there are 3 different types of primitive data types: integral, floating point, and boolean. In integral there are multiple data types like int and char. A variable of integral data types can go up to holding a 32-bit number and can be assigned any integer value as long as it’s inside the min/max range of Java - we’ll get to that later. A variable of floating point data type can be a 64-bit number and can be assigned more number types than integers, like decimals. For floating point, there are data types like long and double. The boolean classification only contains the boolean data type and these variables can only be assigned a true or false. Before looking at objects, let’s look at how a primitive data type can be created.

public class PrimitiveIntro
{
    public static void main(String[] args){
        int integralVar = 1;
        double floatingPointVar = 1.0;
        boolean booleanVar = true;
        System.out.println(integralVar + " " + floatingPointVar + " " + booleanVar);
    }
}

When the program is run (remember to run the main method), it will print the variables in the order stated: 1 1.0 true.

Primitive data types can be changed using a wide array of math operators. The usual math operators: +, -, /, and * can be used to create new algorithms that come from standard mathematical algorithms. In addition to this, the modulus (a modulus b results in the remainder from a/b) can be applied by using % between 2 integral or floating point data types. It’s commonly used with integral data types. Also, the incrementor i++ and decrementor i– can be used to add 1 and subtract 1 from a number. Before we look at some code segments, the rule for data type conversions needs to be established. **Whenever a code statement involves primitive data types from different classifications, like integral and floating point, the data type with the larger size will be the final type at the end of the code statement. This can result in rounding errors and data loss. Casting can be used to preserve data types by using (int)var and (double)var to cast var to an int and double data types respectively. Look at the following code segment. What’s the output from running it?

public class PrimitiveIntro
{
    public static void main(String[] args){
        int var1 = 10;
        double var2 = 4.0;
        System.out.println((var1 / var2) + " " + (int)(var1 / var2));
    }
}

When the program above is run, the program will output: 2.5 2. Even though var1 is an int the data type of the divisor will take precedence since it’s a ‘larger’. This can be seen in the output as well as what happens when a double is cast to an int using the correct casting code statement, (int)var. This is what I mean by data being lost, the .5 gets thrown away when the 2.5 gets downcasted to an int data type.

Booleans and conditional statements get thrown together typically but since we’re only talking about primitive data types here, conditional statements will come later. A boolean data type can be created just like the other primitive data types. Only a boolean doesn’t have mathematical operators to change the value it holds, we use logical operators like and (&&), or (||), and not (!) to manipulate the values of a boolean variable. Take a look at the code segment below.

public class PrimitiveIntro
{
    public static void main(String[] args){
        boolean var1 = true;
        boolean var2 = false;
        boolean var3 = var1 && var2;
        boolean var4 = var1 || var2;
        var1 = !var1;
        System.out.println(var3 + " " + var4 + " " + var1);
    }
}

The output of the code segment shown above will be: false true false. The first print statement is taking the ‘and’ state of the 2 variables which will only evaluate true if both variables are true, which is why it’s false. The second print statement is taking the ‘or’ state of the 2 variables which will evaluate to true if either variable is true, which is why it’s true. The last print statement just switches the state from true to false.

So what’s the deal with objects? Objects are a data type, it even has its own class called Object in Java. With objects, you can create your own data type and have it act how you want it to act. We’ll get to creating data types with writing classes later, but for now we can look at how Strings work in Java. In Java, Strings are objects. You don’t have to declare a new String like an object, but you can (Java gives some flexibility here). But to perform operations on a String, with the exception of concatenations, you call the String method you want to use. So enough talking, let’s look at some examples.

public class StringsIntro
{
    public static void main(String[] args){
        String a = "one string";
        String b = new String("second string");
        System.out.println(a + " " + b);
        System.out.println("Are they the same? " + a.equals(b));
    }
}

In the example above, 2 Strings are declared. One is using a String literal and the second is declared using the new keyword along with the String constructor. Every object in Java has a constructor, this is what establishes the data that the object contains. So with this being said, it can be seen that an object can be thought of as a container that holds a lot of different kinds of data. The name of the object is not the data itself, but a reference to the data the object contains. It’s not surprising what is printed on the first line, but the second line is the one that needs explaining. The equals() method is called and this will compare every character in the 2 Strings to determine if they are equal. If not, then it returns false. This is really important: we don’t use == for determining if objects are equal to each other. You need to call their equals() method instead.

There are tons of more methods that can be used with Strings like substring(), charAt(), indexOf(), and compareTo() just to name a few. A lot of these methods will become easier to use the more you practice with them. In the coming sections, we’ll go over writing selection and iteration statements to help write algorithms in Java. This should cement the use of primitive data types and Strings as you start writing your own algorithms to solve problems.

Keep coding!
Ryan