8/27/15 World Variables in a programming Hello Hello World language n Variables store information n You can think of them like boxes Basic Computation n They “hold” values (Savitch, Chapter 2) n The value of a variable is its current contents TOPICS n Note that this differs from variable in math n In math, a variable is an “unknown” • Variables and Data Types Solving an equation reveals its value n • Expressions and Operators n In programming, variables are “place holders” for values • Integers and Real Numbers Their current value is always known • Characters and Strings n The program changes their values to achieve a goal • Input and Output n 1 2 CS 160, Fall Semester 2015 CS 160, Fall Semester 2015 Hello Hello World World Data Types Creating Variables n You create new variables through declarations. n Variables are like boxes: they hold values n Examples: n But you can’t put an elephant in a shoe box int daysPerYear; char vowel; n Different boxes hold different types of things n You assign values to variables using the n Therefore, variables have data types assignment operator ‘=’. n The data type describes the set of values a n Examples: variable might contain daysPerYear = 365; n The value of a variable is a member of the set vowel = ‘a’; defined by its data type n Examples: int, char, double, boolean, String n Declaration and assignment can be combined. n Java is a strongly typed language – only values of n Examples: int price = 25; the correct type can be put into a variable char c = ‘&’; 3 4 CS 160, Fall Semester 2015 CS 160, Fall Semester 2015 1
8/27/15 Hello Hello World World More about Variables Literals n An uninitialized variable is useless § Literals are values that are directly recognized by Java: n So it’s good practice to initialize when declaring variables § numbers n can be done with one statement: 237, 10, 9, 1.5, 5.8, 99.999 int daysPerYear = 365; § characters n Variables can be re-used: ‘a’, ‘Z’, ‘0’, ‘$’ int daysPerYear = 365; § strings // random code here “hello”, “there” daysPerYear = 110; 5 6 CS 160, Fall Semester 2015 CS 160, Fall Semester 2015 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 § Digits (0 through 9) § Identifiers can be arbitrarily long. § 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, Fall Semester 2015 CS 160, Fall Semester 2015 2
8/27/15 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, Fall Semester 2015 CS 160, Fall Semester 2015 Hello Hello World World Where to Declare Variables Java Types § Declare a variable n In Java, there are two kinds of data types: n Primitive data types § Just before it is used or Let’s start Hold a single, indivisible piece of data n § At the beginning of the section of your with these Pre-defined by the language n program that is enclosed in {}: Examples: int, char, double, boolean n public static void main(String[] args) n Classes Hold complex combinations of data { n Programs may define new classes /* declare variables here */ n Examples: String, System . . . n /* code starts here */ . . . } 11 12 CS 160, Fall Semester 2015 CS 160, Fall Semester 2015 3
8/27/15 Hello Hello World World Primitive Types Primitive Data Types § Integer types: byte, short, int, and long n The 4 most common primitive data types § int is most common Data Type Description Example § Floating-point types: float and double n int - integer values 5 § double is more common n double - floating-point values 3.14 n char - characters ‘J’ § Character type: char n boolean - either true or false true § Boolean type: boolean Notice – there are no quotes around true! 13 14 CS 160, Fall Semester 2015 CS 160, Fall Semester 2015 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, Fall Semester 2015 CS 160, Fall Semester 2015 4
8/27/15 Hello Hello World World Building expressions: operators Expressions n Operators can act on numbers and n A program is a sequence of statements primitive data types n Well, it also needs a header n ‘+’ adds two numbers n But the program body lists statements n ‘*’ multiplies two numbers n ‘-’ subtracts two numbers n Simplest statement is an assignment n ‘/’ divides two numbers n A simple expression looks like: n ‘==‘ tests for equality and returns a Boolean n Operators can also act on other var1 op var2; expressions – build arbitrarily n Where ‘var1’ and ‘var2’ are variables complicated expressions (just like math) n ‘op’ is any operator (e.g. +, -, *) 17 18 CS 160, Fall Semester 2015 CS 160, Fall Semester 2015 Hello Hello World World Variations on Expressions More variations on expressions Variables can be re-used across and n The right hand side of an assignment n even within expressions: can be any mathematical expression: int x = 7; int y = x + (2 * z); n When more than one operator appears x = x + 1; x = x + x; n Parenthesis disambiguate n See above Right hand side is evaluated n n Without parenthesis, operator precedence … and the result is assigned to the lhs n rules apply variable n e.g., multiply before add, left before right n Better to rely on parentheses 19 20 CS 160, Fall Semester 2015 CS 160, Fall Semester 2015 5
8/27/15 Hello Hello World World Integers Integer Arithmetic Operations n Numbers without fractional parts Symbol Operation Example Evaluates to 3, 47, -12 + Addition 45 + 5 50 n Variables store integers with an assignment statement - Subtraction 657 – 57 600 int size = 7; * Multiplication 7000 * 3 21000 n 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, Fall Semester 2015 CS 160, Fall Semester 2015 Hello Hello World World Remainder reminder: % and / Integer Math n For any pair of integers, x and y, there int i = 10/3; always exist two unique integers, q and r, 3 n What’s i = ? x = qy + r and 0 ≤ r < y such that n q is called the quotient, r is the remainder int j = 10 % 3; 1 when x is divided by y n What’s j = ? n The operators, % and / compute them: r = x % y and q = x / y int k = 13 % 5 / 2; n What’s k = ? 1 n Java isn’t mathematically correct for negative numbers 23 24 CS 160, Fall Semester 2015 CS 160, Fall Semester 2015 6
8/27/15 Hello Hello World World Additional Integer Operators Specialized Assignment Operators n 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 n Increment amount += 5; cent++; equivalent to cent = cent + 1; yielding the same results. n Decrement cent--; equivalent to cent = cent – 1; 25 26 CS 160, Fall Semester 2015 CS 160, Fall Semester 2015 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 expressions is evaluated according to the rules of precedence. 27 28 CS 160, Fall Semester 2015 CS 160, Fall Semester 2015 7
Recommend
More recommend