This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Expressions Expressions
► ► An expression can be a single variable, or can include a series of An variables. If an expression includes multiple variables they are combined using operators combined Arithmetic expressions manipulate numeric variables following Arithmetic the rules of algebra and have numeric values. Integral expressions manipulate integer values Floating point or decimal expressions manipulate floating point numbers ► ► Relational expressions compare numerical values and have Relational logical (boolean) values logical Logical expressions combine boolean variables or expressions Logical and yield boolean values and Expressions Expressions
► An expression is a series of variables combined using An unary and/or binary operations unary ► An algebraic expression has a numeric value, a An relational or logical expression has a boolean value relational ► Example X*Y is an algebraic expression If the variables X and Y have different types then they must If be converted to a common type before the expression X*Y is evaluated evaluated The type of the value of the entire expression is the same as The the common type the variables are converted to the Rules for implicit conversion Rules
► The value of the expression in an assignment The statement may be converted to the type of the resultvariable resultvariable ► The value of one of the operands of a binary The operation may be converted before the operation is performed performed ► Some conversions are done implicitly. These Some conversions are the widening conversions that always have valid results have byte to int float to double short to int int to long int to float Explicit conversion: The cast operation Explicit
► In Java you can explicitly convert the type of a variable In or expression within a larger expression using a cast operator operator The value of the variable or expression is not changed The value used in the larger expression is converted to the The requested type requested ► Sample expressions including casts (int)(Floatone+Floattwo) (float)Integerone + Float1 + Float2 (double)(int1)+double(short2) * double2 (float)(int1)/int2 Integral Expressions Integral
► All operands are integers ► Examples: 2+3*5 3 + x – y/7 x + 2 * (y – z) + 18 Floatingpoint Expressions Floatingpoint
► All operands are floatingpoint numbers ► Examples: 12.8 * 17.5 – 34.50 x * 10.5 + y  16.2 Mixed Expressions Mixed
► Operands of different types ► Examples: 2 + 3.5 6/4 + 3.9 ► Integer operands yield an integer result; floatingpoint Integer numbers yield floatingpoint results numbers ► If both types of operands are present, the result is a If floatingpoint number floatingpoint ► Precedence rules are followed Initialization Initialization
► ► Memory locations associated with defined constants must Memory be initialized when the constants are defined be Memory locations associated with variables may be Memory initialized anywhere in the program initialized Initialization may occur in the declaration statement Initialization may be done after declaration using an assignment Initialization statement statement Initialization may also be done by ‘reading’ the value Initialization
► Memory locations associated with variables should have Memory their values defined before they are used. their It is good programming practice to initialize variables at the start It of your program of Values and Memory Allocation for Integral Data Types Integral The class String The
► Used to manipulate strings ► String Sequence of zero or more characters Enclosed in double quotation marks Null or empty strings have no characters Numeric strings consist of integers or decimal Numeric numbers numbers Length is the number of characters in a string Strings and the Operator + Strings
► Operator + can be used to concatenate two strings or Operator a string and a numeric value or character string ► Example: Parsing Numeric Strings Parsing
► String to int Integer.parseInt(strExpression) ► String to float Float.parseFloat(strExpression) ► String to double Double.parseDouble(strExpression) *strExpression: expression containing a numeric string Input Input
Standard input stream object: System.in ► Input numeric data to program
► ► Separate by blanks, lines, or tabs
1. Create an input stream object of the class Create To read a line of characters:
BufferedReader BufferedReader 2. Use the method readLine Input Input
Standard input stream object: System.in ► To read a line of characters: Create an input To stream object of the class BufferedReader BufferedReader InputStreamReader charReader = InputStreamReader new InputStreamReader(System.in); new BufferedReader keyboard = BufferedReader new BufferedReader(charReader): BufferedReader(charReader): ► Read the data into a variable of type string inputStr = keyboard.readLine(); ► Parse the string
► Output Output
► Standard output object: System.out ► Methods print println println flush ► Syntax System.out.print(stringExp); System.out.println(stringExp); System.out.flush(); Commonly used escape sequences Commonly ...
View
Full
Document
This note was uploaded on 01/16/2010 for the course CMPT 101 taught by Professor Various during the Spring '03 term at Simon Fraser.
 Spring '03
 various

Click to edit the document details