Introduction to Java : Data Types and Operators Part-2
Data Types
Java is a “strongly typed language”. That means that type of every variable must be declared.
Consider the following simple variable assignments:
1 2 | 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. |
Declaring, Assigning, and Casting Variables
Declaring Variables
As we just said, if you want to use a variable, you must specifically declare its type. To declare a variable’s type you simply use the type followed by the variable name. Consider the following examples:
1234 byte b;short age;long nationalDebt;boolean isMale;
You can also declare multiple variables of one type in one expression such as in the following example:
1 int age, yrsEmployed, numChildren;
Variable Assignment and Initialization
Once you have declared the type of a variable, you are free to initialize it and assign to it some value.
Assignment and initialization is simple. You simply use variable name = some value. For example, consider the following code:
12 int age;age = 28;
Of course you can also declare variables and assign values to them at the same time using the following syntax:
1 int age = 28;
Casting (Changing from one type to another)
What if you want to multiply 2 x 1.5? Or more generically, int x double? Will the result be an int or a double or something else?
Well, when in doubt, Java will convert to the less restrictive type to be safe. Thus, in the above example, the result will be a double since double is less restrictive.
The following chart shows how types will be caste if possible.
1 2 | byte --> short --> int --> long --> float --> double |
However, what if you are going the other way? Suppose you have two doubles and you want to make an int out of the product
To do this type of caste, you simply perform an assignment using the type to be casted to in parentheses before the value to be casted. Consider the following example in which we caste from a double to an int:
1 2 | double d = 123.456; int i = (int) d; |
In this case, it will be assigned a value of “123”.
Variable Type Helper Objects
So far, the variable types we have discussed are called “primitive types” because they represent simple values that have built-in characteristics. They are fixed elements. Java also provides a set of “reference types” that include object arrays, strings, and other more complex data structures.
We’ll talk more about reference types later when we are using them. However, it is good to note that Java provides reference “wrapper” types for each primitive type that include manipulative functionality.
These classes can all be found in java.lang and include: Boolean, Character, Double, Float, Integer, Long, and String.
Each class contains helper methods that allow you to more easily work with the values. One of the most useful features provided by the wrapper classes is the ability to easily change from one type to another. Thus, you can change an integer value to its string representation using something like:
1 | Integer(20).toString(); |
Consider the list of methods in the following objects:
final static Boolean FALSE | Creates a Boolean with a false value |
final static Boolean MAX_VALUE | Returns 65535 (this was placed in the boolean class by mistake) |
final static Boolean MIN_VALUE | returns 0 (this was placed in the boolean class by mistake) |
final static Boolean TRUE | Creates a Boolean with a true value |
Boolean(String s) | Creates a Boolean based on the string. true is true. Everything else is false. |
Boolean (boolean value) | Creates a Boolean with specified boolean value |
boolean booleanValue() | returns the boolean value of the Boolean object |
boolean equals(Object object) | Returns true if the objects are equal, false if not. |
static boolean getBoolean(String s) | Returns true if s is true. Otherwise it returns false. |
int hashCode() | Returns the Boolean’s hash code. |
String toString() | Returns the string representation of the Boolean. |
static Boolean valueOf(String s) | Returns a Boolean representation of the string |
final static int MAX_RASIX | The maximum radix that can be used in converting a number to an int (represented as a character). The value is 36. |
final static int MIN RADIX | Same as above but returns 2 |
Character (char value) | Returns a Character representation of value. |
char charValue() | Returns the char representation of the Character object |
static int digit(char ch, int base) | Converts ch to int. |
boolean equals(Object o) | Tests for equality between the objects. |
static char forDigit(int digit, int base) | Returns the char representation of the digit in the specified base. |
int hashCode() | Returns the hash code. |
static boolean isDigit(char c) | Returns true if the char value is between 0 and 9 |
static boolean isJavaLetter(char c) | Returns true if the ch is a alphabet character, $, or _ |
static boolean isLetter(char c) | Returns true of the char is a letter |
static boolean isLetterOrDigit(char c) | Returns true of the char is a letter or a digit |
static boolean isLowerCase(char c) | Returns true of character is lower case. |
static boolean isSpace(char c) | Returns true of the character is a space, tab, newline, or return |
static boolean isUpperCase(char c) | Returns true of character is upper case. |
static char toLowerCase(char c) | Converts a char to lower case. |
String toString() | Returns the String representation of the Character |
static char toUpperCase(char c) | Returns the Uppercase value of the Character |
final static double MAX_VALUE | returns the max value |
final static MIN_VALUE | Returns the min value |
final static double NaN | A double that is not a number |
final static double NEGATIVE_INFINITY | -1.0/0.0 |
final static double POSITIVE_INFINITY | 1.0/0.0 |
Double(double value) | Returns a Double representation of the double value. |
Double (String s) throws NumberFormatExcepion | Returns a Double representation of the string value. |
static long double doubleToLongBits(double value) | Returns the bit representation |
double doubleValue() | Returns the double representation of the Double. |
boolean equals(Object o) | Tests for equality |
float floatValue() | Returns the float representation of the Double |
int hashCode() | Returns the hash code |
int intValue() | Returns the int representation of the Double. |
boolean isInfinite() | Returns true if equal POSITIVE or NEGATIVE infinity |
static boolean isInfinite(double d) | Returns true of true. |
boolean isNaN() | Returns true if Double is NaN |
static double longBitsToDouble(long l) | Double representation of the bits |
long longValue() | Returns the long representation of the Double. |
String toString() | Returns the String representation of the Double |
static String toString(double d) | Converts d to String |
static Double valueOf(String s) throws NumberFormatException | Converts String to Double |
final static float MAX_VALUE | returns the max value |
final static float MIN_VALUE | Returns the min value |
final static float NaN | A double that is not a number |
final static float NEGATIVE_INFINITY | -1.0/0.0 |
final static float POSITIVE_INFINITY | 1.0/0.0 |
Float(double value) | Returns a Float representation of the double value. |
Float(float value) | Returns a Float representation of the float value. |
Float (String s) throws NumberFormatExcepion | Returns a Float representation of the string value. |
double doubleValue() | Returns the double representation of the Float. |
boolean equals(Object o) | Tests for equality |
static int floatToIntBits(float value) | Returns the bit representation |
float floatValue() | Returns the float representation of the Float |
int hashCode() | Returns the hash code |
static float intBitsToFloat(int value) | Returns the float representation of the int bits |
int intValue() | Returns the int representation of the Float. |
boolean isInfinite() | Returns true if equal POSITIVE or NEGATIVE infinity |
static boolean isInfinite(float f) | Returns true of true. |
boolean isNaN() | Returns true if Double is NaN |
long longValue() | Returns the long representation of the Float. |
String toString() | Returns the String representation of the Float |
static String toString(float f) | Converts f to String |
static Float valueOf(String s) throws NumberFormatException | Converts String to Float |
final static int MAX_VALUE | returns the max value |
final static int MIN_VALUE | Returns the min value |
Integer(int value) | Returns an Integer representation of the int value. |
Integer (String s) throws NumberFormatExcepion | Returns an Integer representation of the String value. |
double doubleValue() | Returns the double representation of the Integer. |
boolean equals(Object o) | Tests for equality |
float floatValue() | Returns the float representation of the Integer |
static Integer getInteger(String propName) | Returns System property with given key |
static Integer getInteger(String propName, int val) | Returns System property with given key |
int hashCode() | Returns the hash code |
int intValue() | Returns the int representation of the Integer. |
long longValue() | Returns the long representation of the Integer. |
static int parseInt(String s) throws NumberFormatException | Returns the int representation of the String |
String toString() | Returns the String representation of the Integer |
static String toString(int i) | Converts i to String |
static Integer valueOf(String s) throws NumberFormatException | Converts String to Integer |
final static long MAX_VALUE | returns the max value |
final static long MIN_VALUE | Returns the min value |
Long(long value) | Returns a Long representation of the long value. |
Long (String s) throws NumberFormatExcepion | Returns a Long representation of the String value. |
double doubleValue() | Returns the double representation of the Long. |
boolean equals(Object o) | Tests for equality |
float floatValue() | Returns the float representation of the Long |
static Long getLong(String propName) | Returns System property with given key |
static Long getLong(String propName, long val) | Returns System property with given key |
int hashCode() | Returns the hash code |
int intValue() | Returns the int representation of the Long. |
long longValue() | Returns the long representation of the Long. |
static long parseLong(String s) throws NumberFormatException | Returns the long representation of the String |
String toString() | Returns the String representation of the Long |
static String toString(long l) | Converts l to String |
static Long valueOf(String s) throws NumberFormatException | Converts String to Long |