Lesson 2: Built-In Data Types


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
  • Integers
  • Real Numbers
  • Booleans

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).

String a;
String b;
String c;

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"

Integers

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
short a;
short b;
short c;

a = 32760;
b = 10;
c = a + b;

The value of c would be -32,766

Integers

Real Numbers

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

Examples:

3.14159

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

Booleans

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”: !

 

Examples:

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

Truth Table

a

b

a && b

a || b

false

false

false

false

false

true

false

true

true

false

false

true

true

true

true

true

char
String

Characters or sequence of characters

short
int
long

Integers of Whole Numbers

float
double

Real Numbers or Floating Point (decimals)

boolean

true or false

Comparisons

Comparison operators are mixed-type operations that take operands of one type (such as int or double) and produce a result of type boolean.

Operator

==

Meaning

Equal To

Operation

a is equal to b

True

2 == 2

False

3 == 2

!=

Not Equal To

a is not equal to b

3 != 2

2 != 2

<

Less Than

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

>

Greater Than

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

Type Conversion

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.

Explicit casts:

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.

 

Example:

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

Example:

String str1 = “I have “;
String str2 = str1 + 2 + “ apples”;
System.out.println(str2);

The result would be “I have 2 apples”.