0% found this document useful (0 votes)
64 views16 pages

CH 04

The assignment operator assigns values to variables. It has the syntax "target = expression" where the expression evaluates to a value that is then assigned to the target variable. Arithmetic expressions use operators like addition, subtraction, multiplication, and division to compute numeric results. Relational operators compare values and logical operators combine boolean expressions. Type casting converts between compatible data types.

Uploaded by

manojabhole
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views16 pages

CH 04

The assignment operator assigns values to variables. It has the syntax "target = expression" where the expression evaluates to a value that is then assigned to the target variable. Arithmetic expressions use operators like addition, subtraction, multiplication, and division to compute numeric results. Relational operators compare values and logical operators combine boolean expressions. Type casting converts between compatible data types.

Uploaded by

manojabhole
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Assignment Operator

• Syntax:
target = expression;

• Expression: operators and operands that evaluate


to a single value
• value is then assigned to target

• target must be a variable (or constant)

• value must be compatible with target's data

type
Examples:
int numPlayers = 10; // numPlayers holds 10
numPlayers = 8; // numPlayers now holds 8
int legalAge = 18;
int voterAge = legalAge;
• The next statement is illegal
int height = weight * 2; // weight is not defined
int weight = 20;
• Generates the following compilation error:
cannot resolve symbol
Arithmetic Expressions
• An expression is a combination of one or more
operands and their operators
• Arithmetic expressions compute numeric results and
make use of the arithmetic operators:
Addition +
Subtraction -
Multiplication *
Division /
Remainder %
If either or both operands associated with an
arithmetic operator are floating point, the result is a
floating point
Arithmetic Operators
Operator Operation

+ addition

- subtraction

* multiplication

/ division

% modulus (remainder
after division)
Arithmetic Assignment Operators
Operator Example Equivalent

+= a += 3; a = a + 3;

-= a -= 10; a = a - 10;

*= a *= 4; a = a * 4;

/= a /= 7; a = a / 7;

%= a %= 10; a = a % 10;
Modulus Operator
• When dividing two integers:
• the quotient is an integer
• the remainder is truncated (discarded)

• To get the remainder, use the modulus operator


with the same operands
Increment or Decrement
Operators
++ increment by 1
-- decrement by 1
Example:
count++; // count = count + 1;
count--; // count = count - 1;

• Postfix version (var++, var--): use value of var in


expression, then increment or decrement.
• Prefix version (++var, --var): increment or
decrement var, then use value in expression
Relational operators
> greater than
>= greater than or equal to
< less than
<= less than or equal to
== equal to
!= not equal to
Logical Operators
• Boolean expressions can use the following logical
operators:
! Logical NOT
&& Logical AND
|| Logical OR
• They all take boolean operands and produce
boolean results
• Logical NOT is a unary operator (it operates on one
operand)
• Logical AND and logical OR are binary operators
(each operates on two operands) 9
Logical Operators
• Conditions can use logical operators to form complex
expressions
if ((total < MAX+5) && !found)
System.out.println ("Processing…");

Logical operators have precedence relationships


among themselves and with other
operators
All logical operators have lower precedence than the
relational or arithmetic operators
Logical NOT has higher precedence than logical AND
and logical OR 10
The Conditional Operator
• Java has a conditional operator that evaluates a
boolean condition that determines which of two
other expressions is evaluated

• The result of the chosen expression is the result of


the entire conditional operator

• Its syntax is:


condition ? expression1 : expression2
• If the condition is true, expression1 is evaluated; if
it is false, expression2 is evaluated
11
Operator Precedence
Operator Order of Operation
evaluation
() left - rightparenthesis for explicit grouping
++ -- right - leftpreincrement, predecrement
++ -- right - leftpostincrement, postdecrement
* / % left - right multiplication, division,modulus
+ - left - rightaddition or String
concatenation, subtraction
= += -= right - left assignment
*= /=
%=
Type Casting
• Syntax:
dataType variable2 = variable1;
• Rules:
1. variable1 needs to be defined before this
statement appears in the source code
2. variable1 and variable2 need to be compatible
data types; in other words, the precision of
variable1 must be lower than or equal to that of
variable2.
Compatible Data Types
Any type in right column can be assigned to
type in left column:
Data Type Compatible Data Types
byte byte
short byte, short
int byte, short, int, char
long byte, short, int, long, char
float float, byte, short, int, long, char
double float, double, byte, short, int, long, char
boolean boolean
char char
Implicit Type-Casting
• This is a valid assignment:
float salesTax = .05f;
double taxRate = salesTax;

• This is invalid because the float data type is lower


in precision than the double data type:
double taxRate = .05;
float salesTax = taxRate;
Explicit Type Casting
• Syntax:
(dataType)( expression )
Note: parentheses around expression are optional
if expression consists of 1 variable

• Useful for calculating averages

You might also like