Revision
•
declare one integer variables, one float
variable, and one character variable and
Shortcut Assignment Operators
Operator Example
Equivalent
+=
i += 8
i = i + 8
-=
f -= 8.0
f = f - 8.0
*=
i *= 8
i = i * 8
/=
i /= 8
i = i / 8
Increment and Decrement Operators
Operator Name Description
++var preincrement The expression (++var) increments var by 1 and evaluates to the new value in var after the increment.
var++ postincrementThe expression (var++) evaluates to the original value in var and increments var by 1.
--var predecrement The expression (--var) decrements var by 1 and evaluates to the new value in var after the decrement.
Increment and Decrement Operators, cont.
int i = 10;
int newNum = 10 * i++; int newNum = 10 * i;
i = i + 1;
Same effect as
int i = 10;
int newNum = 10 * (++i); i = i + 1;
int newNum = 10 * i;
Increment and Decrement Operators, cont.
Using increment and decrement operators makes
expressions short, but it also makes them complex and
difficult to read.
Example
Consider the following code snippet.
int i = 10;
int n = i++%5;
–
What are the values of i and n after the code is
executed?
–
What are the final values of i and n if instead of
The println Method
•
System.out.println
: A statement to instruct the computer
to print a line of output on the console.
– pronounced "print-linn"
– sometimes called a "println statement" for short
•
The
System.out
object represents a destination (the monitor
screen) to which we can send output
System.out.println ("Whatever you are, be a good one.")
;
object method
The print Method
•
The
System.out
object provides another
service as well
•
The
method is similar to the
println
method, except that it does not
advance to the next line
•
Therefore anything printed after a
Example
class PrePostDemo { public static void
main(String[] args){ int i = 3; i++;
System.out.println(i); // "4" ++i;
System.out.println(i); // "5"
•
string
: A sequence of text characters that can be
printed or manipulated in a program.
–
sometimes also called a
string literal
–
strings in Java start and end with quotation mark
"
characters
–
Examples:
"hello"
"This is a string"
String Concatenation - 1
•
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
String Concatenation - 2
•
The + operator is also used for arithmetic addition
•
The function that it performs depends on the type
of the information on which it operates
•
If both operands are strings, or if one is a string and
one is a number, it performs string concatenation
•
If both operands are numeric, it adds them
•
A string may not span across multiple lines.
"This is not
a legal String."
•
A string may not contain a
"
character. (The
'
character is okay)
"This is not a "legal" String either."
"This is 'okay' though."
Escape Sequences - 1
•
What if we wanted to print a 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.");
•
An
escape sequence
is a series of characters that
represents a special character
•
An escape sequence begins with a backslash character (
\
)
Escape Sequences - 2
•
Some Java escape sequences:
•
What is the output of each of the following
println
statements?
System.out.println("\ta\tb\tc") ;
System.out.println ;)"\\\\"(
System.out.println ;)"'"(
System.out.println ;)""\"\"\"(
System.out.println("C:\nin\the downward spiral") ;
•
Write a
println
statement to produce the following line of output:
\\\ /// \\ // \ /
•
Output of each
println
statement:
a b c \\
' """
C :
in he downward spiral
•
println
statement to produce the line of output:
System.out.println ;)"\\\\\\ /// \\\\ // \\ /"(
•
What
println
statements will generate the following output?
This program prints a
quote from the Gettysburg Address .
" Four score and seven years ago ,
our 'fore fathers' brought forth on this continent a new nation
".
•
What
println
statements will generate the following output?
A "quoted" String is
' much' better if you learn
the rules of "escape sequences ".
•
println
statements to generate the output:
System.out.println("This program prints a") ;
System.out.println("quote from the Gettysburg Address.") ;
System.out.println ;)(
System.out.println("\"Four score and seven years ago,") ;
System.out.println("our 'fore fathers' brought forth on") ;
System.out.println("this continent a new nation.\"") ;
•
println
statements to generate the output:
System.out.println("A \"quoted\" String is") ;
System.out.println("'much' better if you learn") ;
System.out.println("the rules of \"escape sequences.\"") ;
System.out.println ;)(
System.out.println("Also, \"\" represents an empty String.") ;
Syntax
•
syntax
: The set of legal structures and
commands that can be used in a particular
programming language.
•
some Java syntax:
•
syntax error
or
compiler error
: A problem in the
structure of a program that causes the compiler to
fail.
–
If you type your Java program incorrectly, you may
violate Java's syntax and cause a syntax error.
1 public class Hello {
2 pooblic static void main(String[] args) {
3 System.owt.println("Hello, world!")_
4 }
5 } Hello.java:2: <identifier> expected pooblic static void main(String[] args) { ^
Hello.java:5: ';' expected
compiler output:
• Error messages do not always help us understand what is wrong:
Hello.java:2: <identifier> expected
pooblic static void main(String[] args) {
^
– We'd have preferred a friendly message such as, "You misspelled public"
• The compiler does tell us the line number on which it found the error...
– But it is not always the true source of the problem.
1 public class MissingSemicolon {
2 public static void main(String[] args) {
3 System.out.println("A rose by any other name")
4 System.out.println("would smell as sweet");
5 }