Presentation is loading. Please wait.

Presentation is loading. Please wait.

Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.

Similar presentations


Presentation on theme: "Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined."— Presentation transcript:

1 Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined using operators ► Arithmetic expressions manipulate numeric variables following 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 logical (boolean) values ► Logical expressions combine boolean variables or expressions and yield boolean values

2 Expressions ► An expression is a series of variables combined using unary and/or binary operations ► An algebraic expression has a numeric value, a relational or logical expression has a boolean value ► Example  X*Y is an algebraic expression  If the variables X and Y have different types then they must be converted to a common type before the expression X*Y is evaluated  The type of the value of the entire expression is the same as the common type the variables are converted to

3 Rules for implicit conversion ► The value of the expression in an assignment statement may be converted to the type of the resultvariable ► The value of one of the operands of a binary operation may be converted before the operation is performed ► Some conversions are done implicitly. These conversions are the widening conversions that always have valid results  byte to int  float to double  short to int  int to long  int to float

4 Explicit conversion: The cast operation ► In Java you can explicitly convert the type of a variable or expression within a larger expression using a cast operator  The value of the variable or expression is not changed  The value used in the larger expression is converted to the requested type ► Sample expressions including casts  (int)(Floatone+Floattwo)  (float)Integerone + Float1 + Float2  (double)(int1)+double(short2) * double2  (float)(int1)/int2

5 Integral Expressions ► All operands are integers ► Examples: 2 + 3 * 5 3 + x – y/7 x + 2 * (y – z) + 18

6 Floating-point Expressions ► All operands are floating-point numbers ► Examples: 12.8 * 17.5 – 34.50 x * 10.5 + y - 16.2

7 Mixed Expressions ► Operands of different types ► Examples: 2 + 3.5 6/4 + 3.9 ► Integer operands yield an integer result; floating-point numbers yield floating-point results ► If both types of operands are present, the result is a floating-point number ► Precedence rules are followed

8 Initialization ► Memory locations associated with defined constants must be initialized when the constants are defined ► Memory locations associated with variables may be initialized anywhere in the program  Initialization may occur in the declaration statement  Initialization may be done after declaration using an assignment statement  Initialization may also be done by ‘reading’ the value ► Memory locations associated with variables should have their values defined before they are used.  It is good programming practice to initialize variables at the start of your program

9 Values and Memory Allocation for Integral Data Types

10 The class String ► 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 numbers  Length is the number of characters in a string

11 Strings and the Operator + ► Operator + can be used to concatenate two strings or a string and a numeric value or character ► Example:

12 Parsing Numeric Strings ► ► String to int Integer.parseInt(strExpression) ► ► String to float Float.parseFloat(strExpression) ► ► String to double Double.parseDouble(strExpression) *strExpression: expression containing a numeric string

13 Input ► Standard input stream object: System.in ► Input numeric data to program  Separate by blanks, lines, or tabs ► To read a line of characters: 1. Create an input stream object of the BufferedReader 1. Create an input stream object of the class BufferedReader 2. Use the method readLine

14 Input ► Standard input stream object: System.in ► To read a line of characters: Create an input stream object of the BufferedReader ► To read a line of characters: Create an input stream object of the class BufferedReader InputStreamReader charReader = new InputStreamReader(System.in); BufferedReader keyboard = new BufferedReader(charReader): new BufferedReader(charReader): ► Read the data into a variable of type string inputStr = keyboard.readLine(); ► Parse the string

15 Output ► Standard output object: System.out ► Methods  print  println  flush ► Syntax System.out.print(stringExp);System.out.println(stringExp);System.out.flush();

16 Commonly used escape sequences


Download ppt "Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined."

Similar presentations


Ads by Google