 # 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 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:

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

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

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

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

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

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

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

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

Values and Memory Allocation for Integral Data Types

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

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

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

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