Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 3 Hobart College

Similar presentations


Presentation on theme: "Computer Science 3 Hobart College"— Presentation transcript:

1 Computer Science 3 Hobart College
Java Data Types Computer Science 3 Hobart College

2 Primitive data types Java has eight primitive data types:
char: used to store a single character e.g. ‘G’, ‘&’, ‘a’, ‘ ’ boolean: used to store true or false e.g. true, false int, byte, short, long: used to store whole numbers e.g. 42,-5, 0 float, double: used to store numbers with fraction parts, as well as very large number numbers and very small numbers e.g. 42.0, 7.1, , 7.2 x 109, 0.0

3 Primitive data types 9 827 boolean char int 2.00 false float int 3.8
Pair the following data types to their examples 9 827 boolean char int 2.00 false float int 3.8 boolean U char double % true

4 Declare, Instantiate, Initialise
To create and use a primitive data type we… specify the data type, give it a name (identifier), and set an initial value (or leave it as its default value) int age = ; data type identifier value Note: when declaring objects, we use a capital letter for the object type When declaring variables or primitive data types, we use a lower case letter for the data type

5 Mathematics Data types that deal with numbers can be used to calculate the solutions to mathematical equations E.g. int result = 6 + 2; The variable ‘result’ will then contain the value 8

6 Mathematics We can use the following operators + Add - Subtract
* Multiply / Divide % Modulus – remainder after dividing

7 Mathematics Remember that when you do a calculation you must have a variable ready to store the result. For each of the following calculations, determine the value that would be stored in the result. int resultA = 7 * 2; int resultB = 23%5; int resultC = 16/4; int resultD = resultA + resultC * resultB;

8 Mathematics When a calculation involves an int and a float, the answer will be a float. If you try to store the answer in a variable of the wrong data type, you may get a cast error. E.g. “Cannot cast ...”

9 Type Casting We can cast a value to formally change it into another data type. When a double is cast to an int, the whole number is kept, and any faction part ignored. Eg. (int) 4.7 => 4 When an int cast to a double, the fraction part (.0) is added. E.g. (double) 4 => 4.0

10 Type Casting To cast a data type, you put the type in brackets before the value you want to cast. The two examples from the previous slide: 4.7  4 int resultA = (int) 4.7; 4  4.0 double resultB = (double)4;

11 Type Casting In order of operations, e.g. BODMAS, casting is the same as brackets… int result = (int)5.5 * 3; this will first truncate 5.5, giving 5 then calculate 5 * 3 = 15 and set result to 15 If we want to calculate 5.5 * 3 first, we need to do this: int result = (int) (5.5 * 3); this calculates 5.5 * 3 (16.5) first, then truncates the result (16)

12 Displaying values To display a value on the screen, we typically use the drawString() method. The first parameter of the drawString is a text string. g.drawString(“Hello world”,20,20); Strings can be added together with the (+) operator. This is called concatenation. g.drawString(“Hello ” + "world”,20,20);

13 Displaying values The easiest way to convert a numerical value to a string is simply to concatenate it to another string, even an empty one. The numerical value is automatically cast as a string. int result = 42; g.drawString(“” + result,20,20); This displays “42” at (20,20). If you put the variable name in the quotes, it will simply print out the variable name g.drawString(“result”,20,20); This displays “result” at (20,20).

14 Displaying output What is displayed after the following code is run?
int result = 42; double a = 2; double b = 1.9; g.drawString(“The result is: “ + result,20,20); g.drawString(“A is “ +a+ ” and B is ” +(int)b ,20,40); g.drawString(“3 + 2 is “+3+2,20,60);


Download ppt "Computer Science 3 Hobart College"

Similar presentations


Ads by Google