Data Types

So far we have only concerned ourselves with Strings. Strings can hold different characters including both letters and numbers, but are not capable of carrying out calculations with number stored within them - consider the following example

String a = "34";
String b = "23";
String c = a + b; //c would be "3423" (and not 57)

Java provides a number of primitive data types, some useful types are as follows:

NameTypeValues storedDetails
boolean A 'Boolean' value Either true or false
char Character Any single Unicode character 16 bit
int Integer from –2147483648 to 2147483647 Uses 32 bits to store the value (min:-231, max: 231-1)
double A 'double precision' float point number Numbers that aren't integers (whole number) Uses 64 bits to store the value

There are also other frequently used types of number:

String is not a primitive data type

It is also important to note that String is not actually a primitive datatype, rather it is a class. For now you can tell the difference because it is named using an initial capital letter S (in String), whereas primitive data types start with a lowercase letter, e.g. int.

Declaring booleans

As booleans can only be true of false, we simply declare them with the keyword true or false as follows:

boolean isMorning = true;
boolean isAfternoon = false;

Declaring chars

We declare a char in a similar way to strings, only using single quotes, and making sure that only one character is placed between the quote, for example:

char capitalA = 'A';
char caret = '^';

Declaring numbers

Numbers can be created in a similar way to strings, using 'literals', that is, we simply type the number in the code as we need it, for example:

int age = 18;
double goldenRatio = 1.61803398875;
Note that for number literals, quotation marks are not used. If we need to specify an literal 'long' then we add the letter 'L' to the number, and if we want to use the less-precise 'float' data type for numbers (and we probably don't want to), then we append the letter 'F' to the number when declaring it as follows:
long possibleLargeNumber = 643152L;
float speed = 70.3F; //do not use float unless absolutely necessary
Both upper and lowercase 'F' and 'L's can be used, but the similarity of the lowercase 'l' and number '1' make uppercase a safer choice.

Task

We've already been able to get data input by a user and store it as a string, but the scanner also provides a method for getting numeric data from user input - example code to get and store an integer is shown here:

Scanner inputScanner = new Scanner(System.in);
int someInteger = inputScanner.nextInt();
inputScanner.nextLine(); //prepares scanner for next line of input
Remember that to use the scanner also requires an import statement at the top of the file (see the Scanner tutorial) if you need to recap)

  1. Write a programme that prompts a user to input an integer number from the user and prints it back out, preceeded by a message saying "your number is: ". The example output (including the user input) should appear as follows:
    Please type an integer followed by enter:
    4
    Your integer is: 4
  2. Test the programme using the number 8 to make sure it works
  3. Test the programme with a string or a non-integer value and it should crash - make a note of the first line of the error and try and understand what it says
  4. Add an additional prompt and code to collect a double value from the user (you will need to make a guess at the method name needed, but the code completion suggestions should help), output the double with a message in a similar way to how you output the integer