A data type is a set of values and a set of operations defined on them. A simple example with be a set of numbers with operations defined on them.
Operations such as Add, Subtract, Multiply, Divide
In Mathematics, we think of numbers as being infinite. In Computer Programming, we work with a finite number of possibilities.
There are 8 different built-in types of data in Java. Other types are available in system libraries:
- Characters and Strings
- Real Numbers
Characters and Strings
A char is an alphanumeric character of symbol. Examples would be letters in the alphabet, symbols or numbers. We do not usually perform operations on characters other than assigning values to variables.
A String is a sequence of characters. The most common operations we perform is concatenation (2 Strings can be chained together to make a new String).
a = “Hello,”;
b = “Bob”;
c = a + b;
Declares 3 variables of type String
Assigns values to the 3 variables. The variable c has the value "Hello, Bob"
An integer is a whole number and has no decimal points. There are 3 types of integer data types.
- short – an integer number between -215 and 215 – 1
Range of values are between -32,768 and 32,7867
- int – an integer number between -231 and 231 – 1
Range of values are between -2,147,483,648 and 2,147,483,647
- long – an integer number between -263 and 263 – 1
Range of values are between -9,233,372,036,854,775,808 and 9,223,372,036,854,775,807
A Real Number is a floating point number which has decimals. Floating point numbers can be represented by a string of digits with a decimal point or with a notation similar to scientific notation
6.022E23 is equivalent to 6.022 x 1023
- float – single precision floating point number. Generally about 6 – 7 decimal places of accuracy
- double – double precision floating point number. Generally about 15 decimal places of accuracy
The boolean type has only two possible values: true or false
Booleans are the foundation of Compute Science. The most important operations defined for booleans are AND, OR and NOT.
- AND is represented by two (2) ampersands: &&
- OR is represented by two (2) pipe symbols: | | (character above the enter key on a QWERTY keyboard)
- NOT is represented by an exclamation mark and is often referred to as “bang”: !
AND: (a && b) is true if both a and b are true…and false otherwise
OR: (a || b) is true if either a or b is true (or if both are true)…and false otherwise
NOT: !a is true if a is false…and false otherwise
a && b
a || b
Characters or sequence of characters
Integers of Whole Numbers
Real Numbers or Floating Point (decimals)
true or false
Comparison operators are mixed-type operations that take operands of one type (such as int or double) and produce a result of type boolean.
a is equal to b
2 == 2
3 == 2
Not Equal To
a is not equal to b
3 != 2
2 != 2
a is less than b
2 < 13
2 < 2
Less Than or Equal To
a is less than or equal to b
2 <= 2
3 <= 2
a is greater than b
13 > 2
2 > 13
Greater Than or Equal To
a is greater than or equal to b
3 >= 2
2 >= 3
It is quite often necessary, or we are forced, with converting data from one type to another.
Example: Integer values must be converted to Real Numbers
Explicit type conversion:
Call methods such as Math.round(), Integer.parseInt(), and Double.parseDouble()
Automatic type conversion:
For primitive numeric types, the system automatically performs type conversion when we use a value whose type has a larger range of values than expected.
Java has some built-in type conversion methods for primitive types that you can use when you are aware that you might lose information. Mixing data types can often cause unexpected results.
double d = 1.2;
int i = 3;
int value = i * d;
The above code will cause the compile-time error: “possible lossy conversion from double to int” error message.
Automatic conversions for strings:
The built-in type String has special rules. You can create any type of data to a String by using the “+” operator
String str1 = “I have “;
String str2 = str1 + 2 + “ apples”;
The result would be “I have 2 apples”.