Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.

Similar presentations


Presentation on theme: "Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or."— Presentation transcript:

1 Java Data Types

2 Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or false eg. true – int, byte, short, long: used to store whole numbers eg. 4 – float, double: used to store fractional numbers eg. 7.1

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

4 Primitive Data Types To create and use a primitive data type we need to declare the data type to be used We then give it a name Lastly we give it a value int age = 17; 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 The data types that deal with numbers can be used to calculate the solutions to mathematical equations Eg. int result = 6 + 2; The variable ‘result’ will then contain the value 8

6 Mathematics We can use the following operands: + 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. Complete the following calculations and indicate what would be stored in the variable int resultA = 7 * 2; int resultB = 23%5; int resultC = 16/4; int resultD = resultA + resultC * resultB;

8 Mathematics When I use an int and a float, my answer will be a float. If I try to store the answer in the wrong data type I will get an error about “Cannot cast...”

9 Type Casting We can ‘cast’ data types to change into other data types. If I were to cast a float (4.5) to an int, it would truncate all decimals and keep the whole number (4) If I were to cast an int (4) to a float, it would add decimals, and since it has no extra information that will be.0 (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.5  4 int solutionA = (int)4.5; 4  4.0 float solutionB = (float)4;

11 Type Casting In order of operations, e.g. BODMAS, casting is the same as brackets… Eg. int result = (int)5.5 * 3; this will truncate 5.5 and leave 5 it will calculate 5 * 3 it will store the result in the variable ‘result’ If we wanted to calculate 5.5 * 3 and get an integer result we would have: int result = (int) (5.5 * 3); this calculates 5.5 * 3 (16.5), then truncates the result (16)

12 Displaying output To get your results to appear on the screen, you must use a drawString However, if you put the variable name in the quotes, it will simply print out the variable name To avoid this you need to concatenate the variable to a string using +

13 Displaying output For example: g.drawString(“The result is: “+result, 20, 20); In the brackets of the drawString anything before the first comma is text to be displayed But we must make one big string to display by adding (+) all the pieces of information together


Download ppt "Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or."

Similar presentations


Ads by Google