Data
Variables Programs need to remember values. Ex: If you use a program to record sales, you will want to remember data: A loaf of bread was sold to Sione Latu on 14/02/19 for T$1.00. Customer Name: Sione Latu Date Sold: 14/02/19 Item Sold: Loaf of Bread Item Cost: T$1.00 Variables Places in the computer s memory where we can store information. Values and Objects are stored in variables. Numbers Characters Strings Dates
Value Types vs. Reference Types Value Types only store a value Integer only stores: 1 2 5 Boolean only stores: True False Reference Types store a reference to an object A reference is a location in memory The location is the starting point of an object
Primitive Data Types Primitives - The basic types supported by the programming language. Use lowercase for primitive data types in code Type Size (bits) Value Range byte 8-128 to 127 short 16-32,768 to 32767 int 32-2,147,483,648 to 2,147,483,647 long 64-9.2 x 10 15 to 9.2 x 10 15 float 32-3.4 x 10 38 to 3.4 x 10 38 double 64-1.8 x 10 308 to 1.8 x 10 308 char 16 0 to 65,535 boolean 1 True or False
Primitive Data Type Selection Practice How to choose the right primitive variable type. Byte, Short, Int and Long store whole numbers. Float and Double store fractions. Double is more accurate, so if accuracy is important, use double. If you pick something that is too big, you re wasting memory space. Wasting memory space slows things down. If you pick something that is too small, then you re going to crash. If you need a character, use Char. If you need a true/false value, use Boolean. What is a good data type for: Someone s age? A customer s identification number for a video rental store in Nuku alofa? A very large number with decimals? The price of an item?
Using Primitive Variables 2 Steps to using variables 1. Declare the variable 2. Initialize the variable
Declaring Primitive Variables 1 Declare the variable Tell the computer to reserve a space in memory for the variable. You need to tell the computer 2 things: 1. Name of the variable 2. Type of the variable (What kind of variable you have) Primitive types byte short int long float double char boolean Name Type
Declaring Primitive Variables 2 Use a name that is easy to remember. Do not use x, y, z Variable names must start with a letter, underscore or dollar sign. Begin variables with a lowercase character. Then, use a capital letter for each word. Examples firstname customerid
Initializing Primitive Variables Initialize the variable Assign an initial value to a variable. Char values must be enclosed in single quotes. String values must be enclosed in double quotes. Boolean value should be True or False Initial Value
Declaring and Initializing Primitive Variables in 1 line You can declare and initialize a variable in 1 line.
Converting Variable Values Smaller to Larger It is possible to move a value from a variable with a smaller type to a variable with a larger type Example Maximum Short Value Maximum Long Value Is 9.2 x 10 15
Converting Variable Values Larger to Smaller If a value from a variable with a larger type is moved into a variable with smaller type, it may be too big for the smaller type This will cause an error to occur when you run it Maximum Using this: Short Value is 32767 Results in: Do not do this!
Converting Variable Types You can convert variable types by casting it. To cast, use this format: (<desired data type>)variable int is bigger than a byte Casting from a datatype to a smaller datatype is not recommended! Without this, we Would get an error If the Cast function isn t able to convert the value, you will get an error.
Arithmetic Operators Operator Meaning Example + Addition 11 + 22 - Subtraction 22 11 * Multiplication 5 * 6 / Division 21 / 3 % Modulus 12 percent 2 Declare and Initialize x, y and z Get values from x and y Adds x and y together Assigns the sum of x an y to z
Arithmetic Operator Details Addition You can add strings. Adding strings is called concatenation. This converts all non strings into strings. Make sure you add your numbers before converting them to strings. You can t add other objects.
Arithmetic Operator Details Division Tricky because result may not be an integer. In this case the number gets cut off. NOT rounding Even having a result of float doesn t work. Because Java has rules for dealing with data types. The result of a division between two integers is always an integer. So the float just added a.0 to it. To get around this, we need to diving two floats!
Arithmetic Operator Details Subtraction You can t subtract strings or other objects Multiplication User asterisk (*) instead of x Modulus The remainder of a division Can be used to determine whether a number is divisible by another number Can also determine if a number is even or odd
Order of Operations When you have a lot of operations, they are performed in a certain order. Operations in Parentheses () Exponentiation operations from left to right Multiplication or Division operations from left to right Addition or Subtraction operations from left to right Please Excuse My Dear Aunt Sally. Examples: 3 + 6 + 9 / 3 = 3 + 6 + 3 = 12 (3 + 6 + 9) / 3 = 18 / 3 = 6 (5 + 3) / 2 ^ 2 = 8 / 2 ^ 2 = 8 / 4 = 2