• No results found

The Java Programming Language - lecture 3.pptx

N/A
N/A
Protected

Academic year: 2020

Share "The Java Programming Language - lecture 3.pptx"

Copied!
23
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Revision

declare one integer variables, one float

variable, and one character variable and

(3)

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

(4)

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.

(5)

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;

(6)

Increment and Decrement Operators, cont.

Using increment and decrement operators makes

expressions short, but it also makes them complex and

difficult to read.

(7)

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

(8)

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

(9)

The print Method

The

System.out

object provides another

service as well

The

print

method is similar to the

println

method, except that it does not

advance to the next line

Therefore anything printed after a

print

(10)

Example

class PrePostDemo { public static void

main(String[] args){ int i = 3; i++;

System.out.println(i); // "4" ++i;

System.out.println(i); // "5"

(11)

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"

(12)

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

(13)

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

(14)

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."

(15)

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 (

\

)

(16)

Escape Sequences - 2

Some Java escape sequences:

(17)

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:

\\\ /// \\ // \ /

(18)

Output of each

println

statement:

a b c \\

' """

C :

in he downward spiral

println

statement to produce the line of output:

System.out.println ;)"\\\\\\ /// \\\\ // \\ /"(

(19)

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 ".

(20)

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.") ;

(21)

Syntax

syntax

: The set of legal structures and

commands that can be used in a particular

programming language.

some Java syntax:

(22)

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:

(23)

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 }

References

Related documents

Many people are in contact with the parish through this email or the WhatsApp group, but you may think of some who are not getting parish contacts – but they may get family

Let the poem linger with the children and allow them time and space to discuss their initial responses to the poem.. What picture does it put in

The proposed training system consists of four access levels. Level 1 allows users to access most of the available training materials. Level 2 gives permission to registered users

Because it was been previously shown that blood-gas change at ECMO onset is associated with intracranial bleeding in patients receiving VV-ECMO [ 11 ], a nested case–control

To determine correlations between CC-chemokines and HIV-1 disease stage or response to treatment, we examined serum levels of MIP-1 ␣ , MIP-1 ␤ , and RANTES in 60 infected

The aim of our study was to determine the reliability of a novel method quantifying lumbar paravertebral muscle fat content based on conventional T1-W MRI.. Methods: Axial 3-Tesla

As expected, since CD8 + T-cells are precursors to antiviral effector CTLs, many CD8 + T-cell related parameters are significantly correlated with viral load..

Leisure physical activity was also subjectively reported in the baseline questionnaire through the following question; “ How much have you been physically active in your leisure