Data Types
Java is a "strongly typed language".
That means that type of every variable must be declared.
Consider the following simple variable assignments:
boolean isASwingDancer = true;
int age = 30;
In other words, Java requires that if a variable is meant to hold a boolean, it is declared as such.
The same goes for ints and every other data type.
Once you declare a variable, it can never be changed to hold another type of value.
The reason for typing is so that Java can manage the usage of the computer's memory when a Java program is running.
Each type of variable is allotted a different amount of memory depending on how much it needs.
A two-digit integer for example, takes up much less space than a 12-digit decimal.
Thus, when we are typing a variable, what we are really doing is telling the computer how much memory to make available for that variable's value.
The benefit of strong typing is that Java code can be easily ported from one machine to another.
However, you should be aware that if you choose to type a variable one way, and then assign to it a value that it cannot hold, the value will be truncated to fit in the variable and your program will produce incorrect results.
|
In Java, there are eight types of variables: int, short, long, byte, float, double, char, and boolean.
Let's look at each one of these so we can better understand what they are.
| Type | Memory in Bytes |
Comments
|
| int | 4 |
An integer between -2,147,483,648 and 2,147,483,647.
This is the most commonly used integer type because how often are you counting values over 2 billion?
|
| short | 2 |
An integer between -32,768 to 32,767.
If your variable will be bounded, using a short instead of an int is a good way to save memory.
|
| long | 8 |
An integer between -9,223,372,036,854,775,808L to 9,223,372,036,854,775, 807L.
If you are counting numbers that large, you must be working for NASA or the accounting Dept. for Congress.
|
| byte | 1 |
Uses 8 bits to represent a number from -128 to 127
|
| float | 4 |
float is used to represent integers with fractional parts such as 12.3456.
Valid values span 6-7 decimal digits
|
| double | 8 |
A double works like an even more precise float.
Valid values span 15 decimal digits.
In most cases, you will use a double instead of a float since the memory use is not usually too burdensome and the precision is quite a bit better.
|
| char | 2 |
The char type is used to represent single characters between single quotes using Unicode encoding.
|
| boolean | 1 |
This type of variable can be either true or false.
|
NEXT
Selena Sol contributes to the JavaBoutique's Introduction to Java. Selena curently works for Barclays Capital in London, one of the leading global investment banks in Europe and has worked as a software developer for the National Center for Human Genome research, Microline Software, Neuron Data, and Electric Eye in Singapore. Selena is perhaps best-known for creating the Public Domain Web Script Archive (Extropia) and writing several books on Web Programming (Perl, CGI, Java).
Email: selena@extropia.com
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.