[Java] Variable Types

If you are currently like WTF, then you’ve done it. Didn’t I tell you not to try division? To put everyone on the same page, let’s edit in the following.


public class Calc {

    public static void main(String[] args) {

       int a = 3;
       int b = 2;
       int result = a/b;

        System.out.print("3/2 = ");
        System.out.println(result);

    }

}

And you will be surprised to find the result to be:

3/2 = 1

I don’t care what country we are in, but 3 divided by 2 does not equal to 1. Well, deal with it, this is the computer world!

Alight, just joking. Now step back a moment, when we declare the variables, we starts off with the keyword int. What does int stand for? Well, integer, a whole number.

With 3 / 2 = 1.5, we are setting 1.5 to an integer type variable. Since we can’t store anything after the decimal point to an integer by definition, we have to do something about that. We have two choice normally, either round up, or round down. In mathematics, we normally round down anything below .5, and round up anything equals or above .5. Well, in the computer world, they just truncate it and cut out everything after the decimal point. That’s why 3 / 2 = 1.

Alright, so how do we solve this problem that the computer have? To do that, we need to learn about some of the primitive data types that I use in Java.

int – integer, can hold number from -2,147,483,648 to 2,147,483,647

double – think of it is a rational number type, can hold pretty anything number within reasonable range

boolean – can only hold “true” or “false”

char – character, can hold 1 character in the ASCII code format

There are actually more primitive data type, however, to me, most of them useless because they are somewhat redundant. For example, float is different than double only in the number range that the variable can hold; float is a 4 bytes variable while double is a 8 bytes variable. Since double takes up more memory, it has a larger number range.

In our current technological stage, let’s consider the differences between 4 bytes and 8 bytes. A Blue-ray disc can hold up to 128 GB of data, 1 GB is roughly 1, 000, 000, 000 bytes.  So as you can see, there is no reason why we shouldn’t with double instead of float.

Alright, such a long story for a simple task, we will just change the integer typed result variable into a double!

double result = a/b;

Hmm, not quite. What the heck? The result is still 1! Why?

Remember that a and b are still integer type? When you write an equation with only integer, the output will be an integer.

So here are your options, you can either change a or b to a double, or you can do a cast type. You do that by the following:

double result = (double) a/b;

And there you have it, you got some new toys in your box, int and double, now go play with them!

Here’s something to leave you with a thought.


public class Calc {

    public static void main(String[] args) {

       int a = 3;
       int b = 2;
       double result = (double) a/b;

       boolean statement = false;

       //Note that ASCII code of a is 97, b is 98, c is 99
       char first = 'a';
       char second = 'b';
       char third = 'c';

       char forth = (char) (third + 1);

        System.out.println("3/2 = "+result);

        System.out.println("5*8/3 = " + (5*8/3) );
        System.out.println("5*8/3 = " + (5*8.0/3) );

        System.out.println(first+second+third);
        System.out.println(""+first+second+third);
        System.out.println(""+forth);

        System.out.println();

        System.out.println(statement);

    }

}

And the result should be:

3/2 = 1.5
5*8/3 = 13
5*8/3 = 13.333333333333334
294
abc
d

false

Continue to Next Lesson

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s