Transcript Chapter 2

What will each of the following
lines print?
System.out.println("number" + 6 + 4 + 5);
number645
System.out.println("number" + (6 + 4)+ 5);
number105
System.out.println(6 + 5 + 4);
15
System.out.println(6 + "number" + 5 + 4);
6number54
System.out.println(6 + 5 + 4 + "number);
15number
Chapter 2
Variables, Constants,
Assignments, Expressions
data – How are they different?
Johnny
Memory Storage
• In order to reserve space in memory for
the variable, the computer has to know
what type of data it will be.
• Declare = to tell what type of data the
variable will hold and its name
• Initialize = assign the variable a value
using the = sign
Primitive Data types
Primitive Data Types: Simple data types
the type of data that is stored in a variable:
whole number
decimal number
true or false value ( 1,0)
int
double
boolean
Open up DataTypes in Dr. Java
String data type
String is a class. It is an object variable.
We can create String objects.
String holds words enclosed in quotation
marks.
String name = “Sally”;
Review of Declaring Variables
int sum;
type
identifier
This statement declares sum.
It reserves a space in memory large enough to store an int.
Assignments Statements
sum = 120;
identifer
assignment
value
operator
We are initializing sum to 120.
We use the assignment operator ( = ) to
give a value to a variable.
Create Variables
What type of data should be used in the following? (double, int, char, boolean, or a class)
1. Your age:
2. Your bank account balance:
3. Your middle name:
4. Your height:
5. The speed limit:
Arithmetic Operators
int and double operators:
+
*
/
%
addition
subtraction
multiplication
division
modulus or remainder
Arithmetic Expressions
Expressions: combinations of operators and
operands to perform calculations.
6+7/3*8=
2
1.
Types of Division
integer division – when both operands are of type
integer, we truncate the fractional part.
result = 7 / 3;
result
2
2.
floating point division – if one or both operands are
floating point numbers, then the fractional part is
kept.
dresult
double dresult = 7.0 / 3.0;
2.333333333…
Integer and Double division
Try this in the interactions pane
6/4
6.0 / 4
13 / 2
13.0 / 2
Modulus Operator %
% (mod) remainder operator – returns the
remainder when you divide 2 integers or
doubles.
Example: int result = 7 % 3;
result
2
1
3 7
6
1
Remainder
Order of Operations
Java has to follow the order of operations
when computing an expression
Parenthesis
Multiplication, Division, Modulus (L to R)
Addition, Subtraction, String Concatenation (L to R)
Assignment operator
As a reminder if operators are on the
same level, we work left to right.
Assignments work right to left.
Operator Precedence
• Expressions are solved according to an order of
operations. PEMDAS otherwise they are solved left to
right. You should always use parenthesis to assure
correctness.
* / % first from left to right
+ - next left to right
14 + 16 / 3 % 2;
(14 + 16) / 3 % 2;
3 * 6 - 4 / 2);
16
0
7
Visual
Arithmetic
Video
Java
Text
C++
C#
Basic
Website
Go to the internet
http://www.problets.org/about/topics/index.html
Click on expression in java
Operator Precedence
a + (b-c) * d – e
3 1 2 4
a % (b * c) * d * e
2
1
3
4
int a = 6, b = 10, c = 9;
double w = 12.9, y = 3.2;
96
1.
6 + 10 * 9
a + b * c _______________________
2.
6 % 10 / 3.2
a % b / y _______________________
3.
6 – 10 – 9
a - b – c _______________________
4.
6 – 10 / 9
a - b / c _______________________
5.
a/b
6 / 10
_______________________
0
b/a
10 / 6
_______________________
1
6.
1,875
-13
5
int a = 6, b = 10, c = 9;
double w = 12.9, y = 3.2;
12.9 / 3.2
_______________________
1.
w/y
2.
3.2 / 12.9
y/w_______________________
3.
6 + 12.9 / 10
a + w / b _______________________
4.
4.03125
0.24806201550387597
7.29
Assignment
• Problets website.