Download presentation

Presentation is loading. Please wait.

Published byKarina Shinners Modified over 2 years ago

1
Introduction to Computing Concepts Note Set 7

2
Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic

3
Memory in a computer Computer uses a few levels of memory to hold data that it is using in processing. 3366 3368 3370 3372 3374 3376 RAM

4
Data Collection of raw facts or figures Perform some processing on data in order to obtain information ▫ Example Data: Test Grades ▫ Example Information: Class Average, Standard Deviation, etc. Data (and info.) usually has an intrinsic Data Type ▫ Numerical Data (test grades)? String Data (names)? 99 Computer Science 3.1415927 Bob, Sam, Jane

5
Data in Java Need some way to store data/info while program is running/processing For this, we use variables ▫ Allow storage a piece of data Will become “larger” or “smarter” as the semester progresses ▫ Each has a particular data type

6
Data Types To efficiently use the computer’s memory, each variable has a data type ▫ Behind the scenes, tells the compiler and JVM how to use memory to store a pieces of data E.g. A number is stored differently than an integer Java is a Strongly Typed Language ▫ The languages enforces rules as to what you can do with the data in variables ▫ E.g. Won’t necessarily let you do this? “Bob” + 27 Doesn’t necessarily make sense to add a string and number…

7
2 Categories of Data Types Primitive Data Types ▫ Holds a single data item such as integer, character, or true/false value Reference Data Types ▫ Data type whose value is actually a memory address

8
Primitive Data Types Data TypeDescriptionExample Values int Stores positive and negative whole numbers in the range of 2 31 to 2 31 -1 29384 5000 long Stores positive and negative whole numbers in the range of approx -9*10 18 to 9*10 18 -1 135792468740 5*10 10 short Stores positive and negative whole numbers in the range from -32768 to 32767 619 -23 float stores numbers with up to 6 or 7 decimals (Single Precision) 349.135 3.1415 double Stores numbers with up to 14 or 15 decimal places (Double Precision) 3.14159265358979 2.3 boolean Stores data in only one of two states: True or False true, false byte Stores positive and negative whole numbers in the range -128 to 127 75 -14 char stores any one of the 65,436 single characters from the Unicode character set ‘a’ ‘;’ ‘M’ ‘.’

9
Declaring Variables declaration statement ▫ Line of code that identifies, or declares, the type and names the identifier or variable. General Form: SomeDataType SomeIdentifierName; int grade; long atomsInBody; double Average; float price;

10
Declaring Variables – Identifier Names Rules for names of Identifiers 1.must start with letter, underscore or dollar sign ($) 2.subsequent characters can be letters, underscores, dollar signs, or digits (0 – 9) 3.Any Length (but be reasonable) 4.Name should be meaningful (no variables named x, y, z) 5.Remember – Java is Case Sensitive 6.Can’t declare 2 variables with the same name in the same method 7.Can’t have a variable that is a reserved word or the same name as a method General Form: SomeDataType SomeIdentifierName; Where have we seen these rules before?

11
In Code public class TestVariables { public static void main (String [] args) { int grade1 = 100; int grade2, grade3; float average; //Other stuff here } Notice that you can declare the variable and provide an initial value all in one statement.

12
check point Declare a variable to hold your GPA Declare a variable to hold your age (in years)

13
Assignment Statements Used to store a new value in a variable uses the assignment operator (=) userAge = 21; boolean flag newUserAge = userAge; sum = grade1 + grade2 + grade; float average = sum / 3.0;

14
Assignment Statements = is right associative ▫ Means always stores what’s on the right side in the variable on the left side ▫ Will evaluate expression on right first (step 1), then perform the assignment int grade1 = 98; int grade2 = 100; int sum = grade1 + grade2; Step 1 Evaluates to 198 Step 1 Evaluates to 198 Step 2 Stores 198 in sum

15
Variables in Output Can use a variable in an output statement Can concatenate a string literal and variable with plus sign userAge = 21; System.out.println(userAge); System.out.println(“Age is “ + userAge);

16
Important Arithmetic Operators in Java OperationSymbolExampleResult Addition+3+47 Subtraction-4-31 Multiplication*4 * 312 Division/5.0/2.02.5 Integer Division /5/22 Modular Division %20 % 3 2 (only the remainder is stored) Cast (dataType) literal or identifier (int)20.3 20 (truncates decimal)

17
Important Arithmetic Operators in Java OperationSymbolExampleResult Addition+3+47 Subtraction-4-3 Multiplication*4 * 312 Division/5.0/2.02.5 Integer Division/5/22 Modular Division%20 % 3 2 (only the remainder is stored) Cast (dataType) literal or identifier (int)20.3 20 (truncates decimal) Might Be New Math Ideas

18
check point What is the result of? ▫ 3 / 2 ▫ 2 / 3 ▫ 5 % 4 ▫ 4 % 5

19
Arithmetic Operators The order of operator precedence is a predetermined order that defines the sequence in which operators are evaluated in an expression Addition, subtraction, multiplication, and division can manipulate any numeric data type When Java performs math on mixed data types, the result is always the larger data type Casts allow programmers to force a conversion from one primitive type to another

20
Numeric Expressions Any Expression that can be evaluated to a number Can include operators, literal values, variables, method calls Only primitive data types may participate in numeric expressions ▫ Any method calls we use in a numeric expression will return a primitive data type. A literal value and a variable (or 2 variables) must be separated by an arithmetic operator

21
What if multiple operators in 1 expression? Follow the order of Precedence ▫ Unless parentheses dictate otherwise, evaluate expressions in the following order Multiplication and/or Division Integer Division Modular Division Addition and/or subtraction ▫ When multiple operations of the same kind are present, Java performs the left to right 18 / 3 – 2 + 4 * 2

22
Let’s Evaluate 18 / 3 – 2 + 4 * 2 6 – 2 + 4 * 2 6 – 2 + 8 4 + 8 12

23
Parentheses in Expressions Change the order of operations when found in an expression, the part inside the parentheses is evaluated first. Then the rest of the expression is evalutated If they are nested, then the inner-most set of parentheses is evaluated first

24
Let’s Evaluate 18 / (3 – 2) + 4 * 2 18 / 1 + 4 * 2 18 + 4 * 2 18 + 8 26

25
check point – Your Turn! 18 / 3 * 2 + (3 * (2 + 5))

Similar presentations

OK

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google