Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.

Similar presentations


Presentation on theme: "Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember."— Presentation transcript:

1 Data Tonga Institute of Higher Education

2 Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember data: A loaf of bread was sold to Sione Latu on 14/02/04 for T$1.00.  Customer Name: Sione Latu  Date Sold: 02/14/04  Item Sold: Loaf of Bread  Item Cost: T$1.00 Variable – Place in the computer’s memory where information is stored.

3 Value Types vs. Reference Types A variable stores a value or object. Value Types only store values  int only stores: 1 2 5  boolean only stores: True False Reference Types only store references to an object  Object - A self-contained entity that contains data and procedures to manipulate the data.  A reference is a location in memory  The location is the starting point of an object

4 Primitive Data Types TypeSize (bits)Value Range byte8-128 to 127 short16-32,768 to 32767 int32-2,147,483,648 to 2,147,483,647 long64-9.2 x 10 15 to 9.2 x 10 15 float32-3.4 x 10 38 to 3.4 x 10 38 double64-1.8 x 10 308 to 1.8 x 10 308 char160 to 65,535 boolean1True or False Primitives - The basic types supported by the programming language. Use lowercase for primitive data types in code

5 Primitive Data Type Selection Practice 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? 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.

6 Using Primitive Variables 2 Steps to using variables 1. Declare the variable 2. Initialize the variable

7 Declaring Primitive Variables 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 Type Name

8 Declaring Primitive Variables Guidelines 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

9 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

10 Declaring and Initializing Primitive Variables in 1 line You can declare and initialize a variable in 1 line.

11 Demonstration Declaring and Initializing Variables

12 Converting Data Types Smaller to Larger It is possible to automatically move a value from a smaller data type to larger data type Example Maximum Short Value Maximum Long Value Is 9.2 x 10 15

13 Converting Data Types Larger to Smaller If a value from a larger data type is moved into a smaller data type, the value may be too big for the new data type This will cause an error Using this: Results in: Do not do this! Maximum Short Value is 32767

14 Converting Data Types Force conversion of data types by casting it. To cast, use this format: ( ) If the Cast function isn’t able to convert the value, you may not get the answer you expect. int is bigger than a byte Without this, we would get an error Forcing a cast from a larger data type to a smaller data type is not recommended! But the final value Is -24, which is strange!

15 Demonstration Converting Data Types

16 Arithmetic Operators  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 OperatorMeaningExample +Addition11 + 22 -Subtraction22 – 11 *Multiplication5 * 6 /Division21 / 3 %Modulus12 percent 2

17 String Concatenation 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.

18 Demonstration String Concatenation

19 Division Tricky because result may not be an integer. In this case the number gets cut off. NOT rounded! 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 divide two floats!

20 Demonstration Division

21 Multiplication and Modulus Multiplication  Use 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

22 Order of Operations When you have a lot of operations, they are performed in a certain order.  Parentheses ()  Multiplication or Division operations from left to right  Addition or Subtraction operations from left to right. Examples:  3 + 6 + 9 / 3 = 3 + 6 + 3 = 12  (3 + 6 + 9) / 3 = 18 / 3 = 6


Download ppt "Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember."

Similar presentations


Ads by Google