Primitive Data Types and Basic Language Rules for Java
Last time we discussed some basic concepts related to object-oriented programming. Two major ideas we defined were objects and variables. In this article, we will continue our discussion of variables by explaining how they hold and interact with data.
Back before man could walk upright and before manicurists began to pluck grizzly hair from the knuckles of our slope-browed ancestors, man has been deciding, in a very primitive way, what type of objects fit into what types of objects. Could water fit into a stylish saber-toothed-tiger bag? Could an enormous prehistoric coconut be contained within the skull cavity of a giant sloth? And who would win in a race, a giant turtle or that enormous sloth?
Millennia later, man is asking similar questions within the programming world. When we last left off, we were discussing variables and their naming conventions. Now that we know how to name them, we will learn what type of data best fits into each one.
Before you can use a variable, you have to declare your variable, which means that you must give it both a name and a type. This is how a declaration looks:
long beer = 10000;
This tells the program that the type of variable is long, with the delicious name of beer, and that it holds the value 10,000. When declaring a variable, it isn't necessary to assign it a value. We'll discuss this more later in the Literals section.
Below is a list of the different Primitive Data Types.
Byte is an 8-bit data type that holds values in the range of -128 to 127. It is sometimes used to save space in arrays, in place of integers, as it does not take up as much space. Why use an integer to store the number 100, when you can use a byte and save four times the space? The default value is 0.
Short is a 16-bit data type that holds values in the range of -32,768 to 32,767. Similar to our friend the byte, the Short (he prefers vertically challenged) can also be used to save space within an array, replacing an integer, as it is twice as small as an integer. The default value is 0.
Int is a 32-bit data type that holds values from -2,147,483,648 to 2,147,483,647. This is the standard data type for numbers; however, if you find yourself needing more space you can use long, or if you want to save memory, you can use the byte or short in its place, so long as the number falls within the value range of the aforementioned data types. The default value is 0.
Long is a 64-bit data type that holds values from 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. If you can't fit your number in that range, too bad for you. Default value is 0L.
Float is a 32-bit data type. It is used for floating point numbers (not for precise numbers, such as currency). Its default value is 0.0f.
Double is a 64-bit data type. It too is used for floating point numbers (and again, not for precise numbers such as currency). To save space, you may use float instead. Its default value is 0.0d
Boolean is a data type that holds two types of values: True or False. By default, its value is set at False.
Char is used to store any character. It is a 16-bit data type with the value of U0000. It works with the unicode system, within a range of u0000' (or 0) to 'uffff (or 65535).
String is not technically a data type, but it can be used to hold sentences, and so forth. Its default value is null.
Literals are a string constant or an explicit number. A literal is a constant value assigned to a variable.