Java basic types




Hey, welcome to the next tutorial of our beginner’s course.

In this tutorial we’ll learn an important concept, the “java types”.

In the previous tutorial, can you remember, we have written the statement,

There, the statement “Hello World!” belongs to a special type called String.

What is Java Type?

OK. First, let me explain what is the meaning of java type.

Now, imagine a database software that takes some of your personal details. For example,

  • Name
  • Age
  • Married?(Y/N)

Consider the following sample of inputs to the database;

  • Name       : “Muhammad Lee
  • Age           : 25
  • Married? :  N

There, you may see that the types of inputs are pretty much different.

For the name, text type required. For Age, writing text type like “Twenty-five” is unnecessary.

Instead, writing it in number format 25 is much easier and clear.

Finally, in marital status, you are permitted only to write only either Y or N to represent whether the person is married or single. Here our friend, Lee is still single 🙂

Do you know?

Everything that computer runs should be stored in its memory.

Here, if we write this database in a computer, we have to store each of the data “Muhammad Lee”, 25 and N in the memory.

We know that the computer memory is limited.

No matter how big it is in computers nowadays, it will run out of memory if we don’t manage the memory well.

If we can limit the memory usage of these inputs that we have entered, then, it will be a good investment to save memory in a long run with 100000 people in the database.

For example, you can see that the married status is should be either the letter Y or N.

So in any person, only either Y or N is always taken as the input. But when consider the name, it can be a combination of different letters and two names will have different lengths.

Now it is obvious that the memory allocation for marital status should be lower than the memory allocated for the name.

We can just define some standard size memory boxes for each input.

Name     →  [ ]

Age         →  [     ]

Married →  [    ]

The values can be input inside those boxes for two people as follows…

Name     →  [ “Alexandra stan”]

Age         →  [34 ]

Married →  [Y]

Name     →  [“Adolph Blaine Charles David Earl Frederick Gerald Hubert Irvin”]

Age         →  [36 ]

Married →  [N]

Here, you can see that the name box is changing its size with the contents. This is VERY important to understand.

The programmer who writes the program doesn’t know the number of letters in each person’s name in the database. So he just allocates dynamically increasing standard space for the name.

In java, there are various predefined boxes of memory called “Java Types” that we can use in real programming.

Various Java Types

Here is some info about basic java Types that we are going to use extensively. Study this table carefully. Don’t care about the “Default value” column.

Data TypeSize Range Default valueExample
short 2 bytes-32768 to 327670-3245, 231
int 4 bytes-231 to 231-10-39432, 23902
long 8 bytes-263 to 263 – 10-232409, 2323
float 4 bytes-3.4x1038 to 3.4x10380.0 -3e10F, 224F
double

8 bytes-1.7x10308 to 1.7x103080.0-323.23, 12e19
boolean JVM dependentOnly true or false are allowedfalsetrue, false
char 2 bytes 0 to 655350 (blank spaces)‘a’, ‘%’, ‘A’
String Varies with the length of text231 – 1 charactersnull “Hello world!”, “#123test”

Important Points 🙂

# The default value is something that is not required to understand right now. I just gave it here for you to refer later.

# In the numbers like 12e19, the letter “e” represents the powers of 10. In here, it’s 12 x 1019.

# In float, the letter “F” should be used at the end of the number, to imply that it is a floating point number.

# The space allocated for boolean changes according to the Java Virtual Machines used in different operating systems.

# char is the Character type. It is used to store a single character in the keyboard.

# String is a dynamically increasing type according to the content in it.

Another point:

  • short corresponds to two bytes (16 bits)
  • int corresponds to four bytes (32 bits)
  • long corresponds to eight bytes (64 bits)

1 byte = 8 bits

Now if you don’t know what a bit or a byte is, it’s just a measure of the size of the box in computer terms. Higher the bit size, more memory is allocated.

So the type long has high memory than int. Hence, long type can store a very large number that an int cannot store.

In our previous example, we can set the type of each item in database inputs as;

  • Name → String
  • Age → int
  • Married? → boolean

Now perhaps you may have a question: why can’t we use char instead of boolean for the entry Married?? The letters ‘Y’ or ‘N’ are two characters on the keyboard, so why can’t we do that?

Because, there are many characters on the keyboard 🙂 Someone may enter # instead of ‘Y’ or ‘N’. But, in boolean, only true or false are allowed. This ensures the correct response to the question…

That’s all for now. Still we don’t know how to apply this knowledge in writing programs in java. Now let’s go to our next tutorial to gain that…