variables in a programming
play

Variables in a programming Hello Hello World World language - PowerPoint PPT Presentation

Variables in a programming Hello Hello World World language Variables store information Basic Computation You can think of them like boxes (Savitch, Chapter 2) They hold values The value of a variable is its current


  1. Variables in a programming Hello Hello World World language ■ Variables store information Basic Computation ■ You can think of them like boxes (Savitch, Chapter 2) ■ They “hold” values ■ The value of a variable is its current contents TOPICS ■ Note that this differs from variable in math • Variables and Data Types ■ In math, a variable is an “unknown” • Expressions and Operators Solving an equation reveals its value ■ • Integers and Real ■ In programming, variables are “place holders” for values Numbers Their current value is always known ■ • Characters and Strings The program changes their values to achieve a goal ■ • Input and Output 2 CS 160, Summer Semester 2016 Hello Hello World World Data Types Creating Variables You create new variables through declarations. ■ Variables are like boxes: they hold values ■ Examples: ■ But you can’t put an elephant in a shoe box ■ int daysPerYear; ■ Different boxes hold different types of things char vowel; You assign values to variables using the ■ ■ Therefore, variables have data types assignment operator ‘=’. Examples: ■ The data type describes the set of values a ■ variable might contain daysPerYear = 365; vowel = ‘a’; ■ The value of a variable is a member of the set Declaration and assignment can be combined. defined by its data type ■ Examples: ■ ■ Examples: int, char, double, boolean, String int price = 25; ■ Java is a strongly typed language – only values of char c = ‘&’; the correct type can be put into a variable 3 4 CS 160, Summer Semester 2016 CS 160, Summer Semester 2016

  2. Hello Hello World World More about Variables Literals ■ An uninitialized variable is useless ▪ Literals are values that are directly recognized by Java: ■ So it’s good practice to initialize when declaring variables ▪ numbers ■ can be done with one statement: 237, 10, 9, 1.5, 5.8, 99.999 int daysPerYear = 365; ▪ characters ■ Variables can be re-used: ‘a’, ‘Z’, ‘0’, ‘$’ int daysPerYear = 365; ▪ Strings // random code here “hello”, “there” daysPerYear = 110; 5 6 CS 160, Summer Semester 2016 CS 160, Summer Semester 2016 Hello Hello World World Java Identifiers Java Identifiers ▪ An identifier is a name, such as the ▪ Identifiers may not contain any spaces, name of a variable. dots (.), asterisks (*), or other characters: ▪ Identifiers may contain only 7-11 netscape.com util.* // not allowed! ▪ Letters ▪ Identifiers can be arbitrarily long. ▪ Digits (0 through 9) ▪ The underscore character (_) ▪ Since Java is case sensitive, stuff , ▪ And the dollar sign symbol ($) which has a Stuff , and STUFF are different identifiers. special meaning ▪ The first character cannot be a digit. 7 8 CS 160, Summer Semester 2016 CS 160, Summer Semester 2016

  3. Hello Hello World World Keywords or Reserved Words Naming Conventions ▪ Words such as if are called keywords ▪ Class types begin with an uppercase or reserved words and have special, letter (e.g. String). predefined meanings. ▪ Primitive types begin with a lowercase ▪ Cannot be used as identifiers. letter (e.g. int). ▪ See Appendix 1 for a complete list of Java ▪ Variables of both class and primitive keywords. types begin with a lowercase letters ▪ Examples: int, public, class (e.g. myName, myBalance). ▪ Multiword names are "punctuated" using uppercase letters. 9 10 CS 160, Summer Semester 2016 CS 160, Summer Semester 2016 Hello Hello World World Where to Declare Variables Java Types ▪ Declare a variable ■ In Java, there are two kinds of data types: ■ Primitive data types ▪ Just before it is used or Let’s start Hold a single, indivisible piece of data ■ ▪ At the beginning of the section of your with these Pre-defined by the language ■ program that is enclosed in {}: Examples: int, char, double, boolean ■ ■ Reference data types (Classes) public static void main(String[] args) Hold complex combinations of data { ■ Programs may define new classes /* declare variables here */ ■ Examples: String, Scanner, System . . . ■ /* code starts here */ . . . } 11 12 CS 160, Summer Semester 2016 CS 160, Summer Semester 2016

  4. Hello Hello World World Primitive Types Primitive Data Types ▪ Integer types: byte, short, int, and long ■ The 4 most common primitive data types ▪ int is most common Data Type Description Example ▪ Floating-point types: float and double ■ int - integer values 5 ▪ double is more common ■ double - floating-point values 3.14 ■ char - characters ‘J’ ▪ Character type: char ■ boolean - either true or false true ▪ Boolean type: boolean Notice – there are no quotes around true! 13 14 CS 160, Summer Semester 2016 CS 160, Summer Semester 2016 Hello Hello World World Primitive Types Assignment Statements ▪ An assignment statement is used to assign a value to a variable. answer = 42; ▪ The "equal sign" is called the assignment operator. ▪ We say, "The variable named answer is assigned a value of 42," or more simply, " answer is assigned 42.” ▪ General rule: Var = expression; 15 16 CS 160, Summer Semester 2016 CS 160, Summer Semester 2016

  5. Hello Hello World World Building expressions: operators Expressions ■ Operators can act on numbers and ■ A program is a sequence of statements primitive data types ■ Well, it also needs a header ‘+’ adds two numbers ■ ‘*’ multiplies two numbers ■ But the program body lists statements ■ ‘-’ subtracts two numbers ■ ■ Simplest statement is an assignment ‘/’ divides two numbers ■ ‘==‘ tests for equality and returns a Boolean ■ A simple expression looks like: ■ ■ Operators can also act on other var1 op var2; expressions – build arbitrarily complicated expressions (just like math) ■ Where ‘var1’ and ‘var2’ are variables ■ ‘op’ is any operator (e.g. +, -, *) 17 18 CS 160, Summer Semester 2016 CS 160, Summer Semester 2016 Hello Hello World World Variations on Expressions More variations on expressions Variables can be re-used across and ■ The right hand side of an assignment can ■ be any mathematical expression: even within expressions: int x = 7; int y = x + (2 * z); ■ When more than one operator appears x = x + 1; ■ Parenthesis disambiguate x = x + x; See above Right hand side is evaluated ■ ■ ■ Without parentheses, operator precedence … and the result is assigned to the lhs rules apply ■ variable e.g., multiply before add, left before right ■ Better to rely on parentheses ■ 19 20 CS 160, Summer Semester 2016 CS 160, Summer Semester 2016

  6. Hello Hello World World Integers Integer Arithmetic Operations ■ Numbers without fractional parts Symbol Operation Example Evaluates to 3, 47, -12 + Addition 45 + 5 50 ■ Variables store integers with an assignment statement - Subtraction 657 – 57 600 int size = 7; * Multiplication 7000 * 3 21000 ■ Integer variables may be used like integer literals (i.e., number), e.g., / Division 13 / 7 1 size = size + 1; % Remainder 13 % 7 6 21 22 CS 160, Summer Semester 2016 CS 160, Summer Semester 2016 Hello Hello World World Remainder reminder: % and / Integer Math ■ For any pair of integers, x and y, there int i = 10/3; always exist two unique integers, q and r, 3 ■ What’s i = ? such that ■ q is called the quotient, r is the remainder int j = 10 % 3; 1 when x is divided by y ■ What’s j = ? ■ The operators, % and / compute them: r = x % y and q = x / y int k = 13 % 5 / 2; 1 ■ What’s k = ? ■ Java isn’t mathematically correct for negative numbers 23 24 CS 160, Summer Semester 2016 CS 160, Summer Semester 2016

  7. Hello Hello World World Additional Integer Operators Specialized Assignment Operators ■ Self-assignment ▪ Assignment operators can be combined with int temperature = 32; arithmetic operators including -, *, /, % . temperature = temperature + 10; amount = amount + 5; What is temperature? 42 can be written as ■ Increment amount += 5; cent++; equivalent to cent = cent + 1; yielding the same results. ■ Decrement cent--; equivalent to cent = cent – 1; 25 26 CS 160, Summer Semester 2016 CS 160, Summer Semester 2016 Hello Hello World World Parentheses and Precedence Precedence Rules ▪ Parentheses can communicate the order in ▪ Figure 2.2 Precedence Rules which arithmetic operations are performed ▪ examples: (cost + tax) * discount cost + (tax * discount) ▪ Without parentheses, an expression is evaluated according to the rules of precedence. 27 28 CS 160, Summer Semester 2016 CS 160, Summer Semester 2016

Recommend


More recommend