Chapter3-Numerical Data and Expression
Chapter3-Numerical Data and Expression
STIA1113 : Programming I
Lecture Outlines
• This topic focuses on:
– character strings
– primitive data
– the declaration and use of variables
– expressions and order of precedence
Character Strings
• A string literal is represented by putting double
quotes around the text
• Examples:
"This is a string literal."
"123 Main Street"
"X"
• Every character string is an object in Java, defined
by the String class
• Every string literal represents a String object
The println Method
object method
information provided to the method
name
(parameters)
The printf Method
- To Format Output
Usage (Syntax):
System.out.printf(formatString , items);
where
-formatString: a string with format specifiers
- items: list of data/variables to be displayed
%[flag][width][.precision]format type
e.g.
int x= 10; double y = 5.373123;
System.out.printf(“Value of x is %d and
value of y is %6.2f %n”, x , y);
Note:
items MUST match specifiers in order, in number and in exact type
Formating Output
-Flag
Flag Description
- Left justifies the converted
value
, Include comma separator for >= 1000
value
( Enclose ( ) for negative value
Formating examples
String Concatenation
• The string concatenation operator (+) is used to
append one string to the end of another
"Peanut butter " + "and jelly"
• It can also be used to append a number to a
string
• A string literal cannot be broken across two lines
in a program
• See Facts.java
//********************************************************************
// Facts.java Author: Lewis/Loftus
//
// Demonstrates the use of the string concatenation operator and the
// automatic conversion of an integer to a string.
//********************************************************************
System.out.println ();
continue
continue
Output
We present the following facts for your extracurricular edification:
Output
24 and 45 concatenated: 2445
24 and 45 added: 69
Quick Check
What output is produced by the following?
X: 25
Y: 65
Z: 30050
Escape Sequences
• What if we wanted to print the quote character?
• The following line would confuse the compiler
because it would interpret the second quote as
the end of the string
System.out.println ("I said "Hello" to you.");
• See Roses.java
//********************************************************************
// Roses.java Author: Lewis/Loftus
//
// Demonstrates the use of escape sequences.
//********************************************************************
int total;
int count, temp, result;
The result is the rightmost type from the following list that occurs
in the expression.
byte-->short -->int-->long-->float-->double
Example
double doubleVariable = 7;
is possible even if doubleVariable is of type
double.
Assignment Evaluation
The expression on the right-hand side of the
assignment operator (=) is evaluated first.
Examples:
int count = 0;
char grade = 'A';
Initializing Variables
Syntax
type variable_1 = expression_1,
variable_2 = expression_2, …;
Simple Screen Output
System.out.println("The count is "
+ count);
Examples
865000000.0 can be written as 8.65e8
0.000483 can be written as 4.83e-4
Example: 1 + 2 + 3 is (1 + 2) + 3 which is 6
Example: 1 - 2 - 3 is (1 - 2) - 3 which is -4
55 (not the same as 1 - (2 - 3) which is 2)
Sample Expressions
Some Arithmetic Expressions in Java
Integer division with /
Strangely, 14 / 4 evaluates to 3, not 3.5.
• In Java, when we divide integers, the result is also an integer: the integer
quotient.
• The integer quotient of dividing 14 by 4 is 3.
The integer remainder of dividing 14 by 4 is 2.
• Imagine that you were doing long division:
3 52
4 ) 14 27 ) 1425
12 135
2 75
54
21
• Examples: 35 / 5 is 7, 84 / 10 is 8, 156 / 100 is 1
• Dividing by 0 causes a runtime error in your program.
57
Integer remainder with %
The % operator computes the remainder from a division of
integers.
• Example: 14 % 4 is 2
• Example: 218 % 5 is 3
3 43
4 ) 14 5 ) 218
12 20
2 18
15
3
What are the results of the following expressions?
• 45 % 6
• 2 % 2
• 8 % 20
58 • 11 % 0
Applications of % operator
What expression obtains the last digit (units place) of a number?
Example: From 230857, obtain the 7.
59
Operator precedence
How does Java evaluate 1 + 3 * 4? Is it (1 + 3) *
4, or is it 1 + (3 * 4)?
• 1 * 2 + 3 * 5 / 4 1 + 2 / 3 * 5 - 4
• \_/ \_/
|
2 + 3 * 5 / 4 |
• \_/ 1 + 0 * 5 - 4
| \___/
2 + 15 / 4 |
• \___/ 1 + 0 - 4
| \______/
2 + 3
• |
\________/
| 1 - 4
5 \___/
|
-3
64
Precedence questions
What values result from the following expressions?
– 9 / 5
– 695 % 20
– 7 + 6 * 5
– 7 * 6 + 5
– 248 % 100 / 5
– 6 * 3 - 9 / 4
– (5 - 7) * 4
– 6 + (18 % (17 - 12))
65
Real numbers (double)
• The expressions we have seen so far used integers (type int),
but Java also can manipulate real numbers with a decimal point
(type double).
Examples: 6.022 -15.9997 42.0 2.143e17
69
Mixed types example
• 2.0 + 10 / 3 * 2.5 - 6 / 4
• \___/
|
2.0 + 3 * 2.5 - 6 / 4
• \_____/
|
2.0 + 7.5 - 6 / 4
• \_/
|
2.0 + 7.5 - 1
• \_________/
|
9.5 - 1
• \______________/
|
70 8.5
Specialized Assignment Operators
• equivalent operations
count++;
++count;
count = count + 1;
count--;
--count;
count = count - 1;
Increment and Decrement
Operators in Expressions
• after executing
int m = 4;
int result = 3 * (++m)
result has a value of 15 and m has a value of 5
• after executing
int m = 4;
int result = 3 * (m++)
result has a value of 12 and m has a value of 5
Summary
You have become familiar with Java
primitive types (numbers, characters, etc.).