java data types. primitive data types java has 8 primitive data types: – char: used to store a...
TRANSCRIPT
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 false eg. true– int, byte, short, long: used to store whole numbers
eg. 4– float, double: used to store fractional numbers eg.
7.1
Primitive Data Types
• Pair the following data types to their examples
char int
boolean
float
double
char
boolean
int
827
%
U
9
false
true
2.003.8
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 typeWhen declaring variables or primitive data types, we use a lower case letter for the data type
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
Mathematics
• We can use the following operands:+ Add- Subtract* Multiply/ Divide% Modulus – remainder after dividing
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 variableint resultA = 7 * 2;int resultB = 23%5;int resultC = 16/4;int resultD = resultA + resultC * resultB;
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 ...”
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)
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 4int solutionA = (int)4.5;4 4.0float solutionB = (float)4;
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 5it will calculate 5 * 3it 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)
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 +
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