19
1 String Escape Sequences What if we wanted to print a double 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 (\), which indicates that the character(s) that follow should be treated in a special way System.out.println ("I said \"Hello\" to you.");

1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

Embed Size (px)

Citation preview

Page 1: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

1

String Escape Sequences

• What if we wanted to print a double 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 (\), which indicates that the character(s) that follow should be treated in a special way

System.out.println ("I said \"Hello\" to you.");

Page 2: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

2

Escape Sequences

• Some Java escape sequences:

• See Roses.java

Escape Sequence

\b\t\n\r\"\'\\

Meaning

backspacetab

newlinecarriage returndouble quotesingle quotebackslash

Page 3: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

More on String Objects

• Because strings are so common, we don't have to use the new operator to create a String object

title = "Java Software";

• which is a shorcut for:title = new String ("Java Software“);

• This is special syntax that only works for strings

title “Java Software"

Page 4: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

4

Yet More on String Objects• Every string literal, delimited by double quotation marks,

also represents an implicit String object in Java• A common service we want from string objects is to

merge two strings, i.e., append one string to the end of another string

• One possibility to join “hello ” and “world” could be:– “hello ”.join(“world”);

• To have a more natural expression, Java introduces the string concatenation operator (+):– it is used to append one string to the end of another– for example

“hello “ + “world!” generates “hello world!”

“hello”+”world!” is a shorthand of “hello ”.+(“world”)

See Facts.java

Page 5: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

Primitive Data Types

• Strings are just one type of common objects we encounter and are treated specially by Java

• There are other basic (primitive) objects, such as integers etc

• Java predefines classes for such basic types– for example, one predefined class is Integer:

To create an Integer (object): Integer wagePerHour = new Integer(10); System.out.println( wagePerHour );

Page 6: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

Primitive Data Types• Although in pure object-oriented languages (such

as Smalltalk and Ruby), we can consider everything as objects and defines everything as class, for efficiency, Java treats common objects such as numbers, characters, and logical values specially, and introduces them into the language as primitive data types

• Note: this division between primitive types and objects is disliked by some programmers familiar with languages such as Smalltalk and Ruby where everything is an object

Page 7: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

Primitive Data Types• There are exactly eight primitive data types in

Java– four of them represent integers:

• byte (class Byte), short (class Short), int (class Integer), long (class Long)

– two of them represent floating point numbers• float (class Float), double (class Double)

– one of them represents characters:• char (class Character)

– and one of them represents boolean (logical) values:• boolean (class Boolean)

Page 8: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

Numeric Primitive Data Types

• byte, short, int, long, float, and double are numeric data types

• The difference among the various numeric primitive types is their storage sizes and representations, and therefore the ranges and precision of the values they can store

• To understand this, we need to have a rough understanding of computer memory storage

Page 9: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

Memory

9278927892799279

92809280928192819282928292839283928492849285928592869286

A computer can use more cells A computer can use more cells to store data, e.g., 2 bytesto store data, e.g., 2 bytes

1001101010011010Each memory cell has a set Each memory cell has a set number of bits (usually 8 number of bits (usually 8 bits, or one bits, or one bytebyte); ); a bit can a bit can represent 2 values)represent 2 values)

RAM is divided into RAM is divided into many cells; each cell can many cells; each cell can be identified by a numeric be identified by a numeric addressaddress

Primary storage area Primary storage area for programs and datafor programs and data

Also called RAMAlso called RAM MainMemory

- how many values can a byte represent?- how many values can a byte represent?

- how many values can 2 bytes represent?- how many values can 2 bytes represent?

Page 10: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

Numeric Primitive Data• “Objects” of different numeric data types

occupy different number of cells

Type

byteshortintlong

floatdouble

Storage

8 bits16 bits32 bits64 bits

32 bits64 bits

Min Value

-128-32,768-2,147,483,648< -9 x 1018

+/- 3.4 x 1038 with 7 significant digits+/- 1.7 x 10308 with 15 significant digits

Max Value

12732,7672,147,483,647> 9 x 1018

IEEE 754format

Page 11: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

11

Characters

• A char is a single character from a character set

• A character set is an ordered list of characters; each character is given a unique number

• Character literals are represented in a program by delimiting with single quotes:

'a‘ 'X‘ '7' '$‘ ',' '\n'

Page 12: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

12

Character Sets• The ASCII character set is quite popular. It includes:

• Java uses the Unicode character set, a superset of ASCII – uses sixteen bits (2 bytes) per character, allowing for 65,536

unique characters– it is an international character set, containing symbols and

characters from many languages– code chart can be found at:http://www.unicode.org/charts/

uppercase letterslowercase letterspunctuationdigitsspecial symbolscontrol characters

A, B, C, …a, b, c, …period, semi-colon, …0, 1, 2, …&, |, \, …carriage return, tab, ...

Page 13: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

13

Boolean

• A boolean value represents logical value: true or false

• The reserved words true and false are the only valid values for a boolean type

• A boolean can also be used to represent any two states, such as a light bulb being on or off

Page 14: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

14

Variables: Revisited• We already know that a variable must be

declared, specifying the variable's name and the type of information that will be held in it

• As of now, think of a variable as a name for a location in memory cell (we will revisit the concept later)

int total;

int count, temp, result;

Multiple variables can be created in one declaration

data type variable name

Page 15: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

Variables

• A variable can be given an initial value in the declaration

• When a variable is referenced in a program, its current value is used

• See PianoKeys.java

int sum = 0;int base = 32, max = 149;String msg1 = new String( “Hello” );String msg2 = “Hello” ;

Page 16: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

16

Change the Value of a Variable: Assignment Statement

• An assignment statement changes the value of a variable

• The assignment operator is the = signtotal = 55;

• Remember: you can only assign a value to a variable that is consistent with the variable's declared type

• The expression on the right is evaluated and the result is stored in the variable on the left

• The value that was in total is overwritten

• See Geometry.java

Page 17: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

Constants

• A “constant variable” is an identifier that is similar to a variable except that it holds one value for its entire existence

• Why constants:– give names to otherwise unclear literal values– facilitate changes to the code– prevent inadvertent errors

• In Java, we use the final modifier to declare a variable constant, and the convention is to use all capital words to name a constant

final double PI = 3.14159265;

• The compiler will issue an error if you try to assign value to a constant variable after its declaration

Page 18: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

Arithmetic Expressions• An expression is a combination of operators and

operands• Arithmetic expressions (we will see logical expressions

later) are essentially special methods applied to numerical data objects: compute numeric results and make use of the arithmetic operators:

Addition +Subtraction -Multiplication *Division /Remainder %

Page 19: 1 String Escape Sequences What if we wanted to print a double quote character? The following line would confuse the compiler because it would interpret

Division and Remainder

• If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded)

• The remainder operator (%) returns the remainder after dividing the second operand into the first

8 / 12 equals?

14 / 3 equals?

14 % 3 equals?

8 % 12 equals?