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:

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.

TypeMemory in BytesComments
int4An 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?
short2An 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.
long8An 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.
byte1Uses 8 bits to represent a number from -128 to 127
float4float is used to represent integers with fractional parts such as 12.3456. Valid values span 6-7 decimal digits
double8A 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.
char2The char type is used to represent single characters between single quotes using Unicode encoding.
boolean1This 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:

You can also declare multiple variables of one type in one expression such as in the following example:

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:

Of course you can also declare variables and assign values to them at the same time using the following syntax:

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.

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:

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:

Consider the list of methods in the following objects:

Boolean
final static Boolean FALSECreates a Boolean with a false value
final static Boolean MAX_VALUEReturns 65535 (this was placed in the boolean class by mistake)
final static Boolean MIN_VALUEreturns 0 (this was placed in the boolean class by mistake)
final static Boolean TRUECreates 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
Character
final static int MAX_RASIXThe 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 RADIXSame 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
Double
final static double MAX_VALUEreturns the max value
final static MIN_VALUEReturns the min value
final static double NaNA double that is not a number
final static double NEGATIVE_INFINITY-1.0/0.0
final static double POSITIVE_INFINITY1.0/0.0
Double(double value)Returns a Double representation of the double value.
Double (String s) throws NumberFormatExcepionReturns 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 NumberFormatExceptionConverts String to Double
Float
final static float MAX_VALUEreturns the max value
final static float MIN_VALUEReturns the min value
final static float NaNA double that is not a number
final static float NEGATIVE_INFINITY-1.0/0.0
final static float POSITIVE_INFINITY1.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 NumberFormatExcepionReturns 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 NumberFormatExceptionConverts String to Float
Integer
final static int MAX_VALUEreturns the max value
final static int MIN_VALUEReturns the min value
Integer(int value)Returns an Integer representation of the int value.
Integer (String s) throws NumberFormatExcepionReturns 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 NumberFormatExceptionReturns 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 NumberFormatExceptionConverts String to Integer
Long
final static long MAX_VALUEreturns the max value
final static long MIN_VALUEReturns the min value
Long(long value)Returns a Long representation of the long value.
Long (String s) throws NumberFormatExcepionReturns 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 NumberFormatExceptionReturns 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 NumberFormatExceptionConverts String to Long