Broad Network


Java Data Types

Java Just After the Basics - Part 1

Forward: In this part of the series I briefly explain Java Data types.

By: Chrysanthus Date Published: 2 Sep 2012

Introduction

This is the first part of my series, Java Just After the Basics. In this part of the series I briefly explain Java Data types.

Prerequisite
I have written a series that precedes this one. In that series, I explained the basics of today’s Java. You should read that series first before coming here, as this series is a continuation. The title of the series is:

- Java Basics

To arrive at the series, just type, “Java Basics”, without the quotes and my name Chrys, in the search box of this page or in the search box of a search engine, and click Search.

The int
Here, int, means integer or whole number. The maximum value of an int is 2,147,483,648 in Java. The minimum is -2,147,483,647. The int data type is for both positive and negative integers.

Initialization example:

    int myInt = 265;

In Java, an int, whether small or big occupies 4 bytes in memory. So, the number, 0, or 1, 15. or –15,  or even 25,478,931, each occupies the same number (4) of bytes in memory. Remember, when typing a number in a program code, you do not include the comma.

The short
This is an int type in which each number occupies 2 bytes. It ranges from -32,768 to 32,767 (inclusive). You use this type instead of int when you want to economize memory.

Initialization example:

    short myShort = 14;

The long
This is an int, where each number occupies 8 bytes in memory. It ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (inclusive). So, if your code deals with whole numbers, some of whose values are beyond the limit of the int range, you have to use the long data type. Note, even when dealing with the long data type, the integer, 2, will still occupy 8 bytes. This is a disadvantage, but the advantage comes when you are dealing with integers that are beyond the range of the int type. If in your code, no integer will occur beyond the range of the int type, then use int instead of long; and if no integer will occur beyond the range of the short type, use short instead of int. You always have to think of economizing computer memory.

Initialization example:

    long myLong = 9223372036854775803L;

Note the L letter at the end of the number. It is an inconvenience, but it has to be there in your code.

The byte
The byte type is the smallest int type. It takes just one byte. It ranges from -128 to 127 (inclusive). So, if you want to economize memory and you need whole numbers smaller than the short data type, then use the byte data type.

Initialization example:

    byte myByte = 5;

The double
You use the double data type, when your code uses numbers with decimal points. The double data type will still take numbers without decimal points (that is, it will still take integers). A double data type number, no matter how big or small, occupies 8 bytes of memory.

Initialization example:

    double myDouble = 4568.365;

Note. A double can take a decimal point but not a comma. The decimal point is placed where it should be placed in manual writing.

The float
When you are dealing with decimal point numbers and you want to economize memory, use the float data type. Any float data type number occupies 4 bytes of memory.

Initialization example:

    float myFloat = 356.36f;

Note: you end a float data type number with, f. In this series (volume) I use the double instead of float, because of the inconvenience of always typing the, f.

Note: do not use the float or double data types for currency (money). I will explain the data type used for money, later.

The char
The char data type is used for coding single characters instead of a string. You type a single character, e.g. B, in single quotes.

Initialization example:

    char myChar = 'E';

Note: you can always use an escape sequence such as, '\n' as a character. The following program will print an empty line for '\n', between 2 lines:

class Temp
    {
        public static void main(String[] args)
            {
                char myChar = 'E';
                char yourChar = '\n';
                System.out.println(myChar);
                System.out.println(yourChar);
                System.out.println(myChar);
            }
    }

Read and try the program.

The boolean
A boolean value is either true or false, without quotes. You need a Boolean data type in order to use a boolean value.

Initialization example:

    boolean myBool = true;

The class
The class is not really a data type. It is what you can use to create your own data type. All the above types are actually primitive object types. You can use the class to instantiate objects of your own data type. Each class has a name and the class name becomes your own data (object) type. You were introduced to this in the prerequisite series titled, Java Basics. In fact I have a whole series just for classes, as Java program is the interaction of objects – more on this later (in a different series).

The String
Now, the string, which you must have seen in the prerequisite Java Basics course, is actually a class. String is the name of a class. Its instantiated objects is what you use. The String data type (beginning with an uppercase letter) has a special way of instantiation. You instantiate a string object type (data type), by just assigning a string literal in double quotes, to the declared variable. A string literal takes double quotes and not single quotes.

Initialization example:

    String myString = "We are the world. We are the Children. We are the ones to make a better day, so let’s start giving; etc.";

Scientific Notation for float and double
You can write double or float numbers in standard form, using the scientific notation. The best way to explain this is by illustration. The following numbers in their initialization statements, are the same:

double d1 = 523.4;
double d2 = 5.234e2;
float f1  = 523.4f;
float f2  = 5.234e2f;

All the numbers are the same numbers written in different forms. The first one is, 523.4 read the normal way. The second one is 5.234 times (X) 10 raised to the power, 2. The first and second numbers are of the double type. The third one is 523.4 read normally; the f indicates that it is of type float. The last one is 523.4 read as 5.234 X 10 to the power 2; the f means it is of type float. All the numbers are the same (have same value). The letter, e, indicates that you are dealing with standard form representation of numbers (in scientific notation).

That is it for this part of the series. We stop here and continue in the next part.

Chrys
NEXT

Related Articles

Java Course

Comments

Become the Writer's Fan
Send the Writer a Message