+ All Categories
Home > Documents > © 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can...

© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can...

Date post: 18-Jan-2018
Category:
Upload: jacob-ross
View: 218 times
Download: 0 times
Share this document with a friend
Description:
© 2007 Pearson Addison-Wesley. All rights reserved2-3 String Concatenation 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 The + operator is evaluated left to right, but parentheses can be used to force the order
17
© 2007 Pearson Addison-Wesley. All rights reserved 2-1 Character Strings A string of characters can be represented as a string literal 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
Transcript
Page 1: © 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.

© 2007 Pearson Addison-Wesley. All rights reserved 2-1

Character Strings

• A string of characters can be represented as a string literal 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

Page 2: © 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.

© 2007 Pearson Addison-Wesley. All rights reserved 2-2

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

Page 3: © 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.

© 2007 Pearson Addison-Wesley. All rights reserved 2-3

String Concatenation• 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

• The + operator is evaluated left to right, but parentheses can be used to force the order

Page 4: © 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.

© 2007 Pearson Addison-Wesley. All rights reserved 2-4

Escape Sequences

• Some Java escape sequences:

• See Roses.java (page 66)

Escape Sequence

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

Meaning

backspacetabnewlinecarriage returndouble quotesingle quotebackslash

Page 5: © 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.

© 2007 Pearson Addison-Wesley. All rights reserved 2-5

Primitive Data• There are eight primitive data types in Java

• Four of them represent integers: byte, short, int, long

• Two of them represent floating point numbers: float, double

• One of them represents characters: char

• And one of them represents boolean values: boolean

Page 6: © 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.

© 2007 Pearson Addison-Wesley. All rights reserved 2-6

Characters

• A char variable stores a single character

• Character literals are delimited by single quotes:'a' 'X' '7' '$' ',' '\n'

• Example declarations:

char topGrade = 'A';

char terminator = ';', separator = ' ';

• Note the distinction between a primitive character variable, which holds only one character, and a String object, which can hold multiple characters

Page 7: © 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.

© 2007 Pearson Addison-Wesley. All rights reserved 2-7

Boolean

• A boolean value represents a true or false condition

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

boolean done = false;

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

Page 8: © 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.

© 2007 Pearson Addison-Wesley. All rights reserved 2-8

Expressions• An expression is a combination of one or more

operators and operands• Arithmetic expressions compute numeric results and

make use of the arithmetic operators:

• If either or both operands used by an arithmetic operator are floating point, then the result is a floating point

AdditionSubtractionMultiplicationDivisionRemainder

+-*/%

Page 9: © 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.

© 2007 Pearson Addison-Wesley. All rights reserved 2-9

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

14 / 3 equals

8 / 12 equals

4

0

14 % 3 equals

8 % 12 equals

2

8

Page 10: © 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.

© 2004 Pearson Addison-Wesley. All rights reserved 4-10

Classes and Objects• A Java program consists of one or more

classes• A class is an abstract description of objects• Here is an example class:

class Dog { ...description of a dog goes here... }

• Here are some objects of that class:

Page 11: © 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.

© 2004 Pearson Addison-Wesley. All rights reserved 4-11

More Objects• Here is another example of a class:

class Window { ... }• Here are some examples of Windows:

Page 12: © 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.

© 2004 Pearson Addison-Wesley. All rights reserved 4-12

Classes contain data definitions• Classes describe the data held by each of its

objects• Example:

class Dog { String name; int age; ...rest of the class...}

Data usually goes first in a class

• A class may describe any number of objects Examples: "Fido", 3; "Rover", 5; "Spot", 3;

• A class may describe a single object, or even no objects at all

Page 13: © 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.

© 2004 Pearson Addison-Wesley. All rights reserved 4-13

Classes contain methods• A class may contain methods that describe the behavior of

objects• Example:

class Dog { ... void bark() { System.out.println("Woof!"); }}

Methods usually go after the data

• When we ask a particular Dog to bark, it says “Woof!”• Only Dog objects can bark; the class Dog cannot bark

Page 14: © 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.

© 2004 Pearson Addison-Wesley. All rights reserved 7-14

Arrays

• An array is an ordered list of values

0 1 2 3 4 5 6 7 8 9

79 87 94 82 67 98 87 81 74 91

An array of size N is indexed from zero to N-1

scores

The entire arrayhas a single name

Each value has a numeric index

This array holds 10 values that are indexed from 0 to 9

Page 15: © 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.

© 2004 Pearson Addison-Wesley. All rights reserved 7-15

Arrays• For example, an array element can be assigned a

value, printed, or used in a calculation:

scores[2] = 89;

scores[first] = scores[first] + 2;

mean = (scores[0] + scores[1])/2;

System.out.println ("Top = " + scores[5]);

Page 16: © 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.

© 2004 Pearson Addison-Wesley. All rights reserved 7-16

Using Arrays• The iterator version of the for loop can be used

when processing array elements

for (int score : scores) System.out.println (score);

• This is only appropriate when processing all array elements from top (lowest index) to bottom (highest index)

• See BasicArray.java

Page 17: © 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.

© 2004 Pearson Addison-Wesley. All rights reserved 7-17

//********************************************************************// BasicArray.java // Demonstrates basic array declaration and use.//********************************************************************

public class BasicArray{ //----------------------------------------------------------------- // Creates an array, fills it with various integer values, // modifies one value, then prints them out. //----------------------------------------------------------------- public static void main (String[] args) { final int LIMIT = 15, MULTIPLE = 10;

int[] list = new int[LIMIT]; // Initialize the array values for (int index = 0; index < LIMIT; index++) list[index] = index * MULTIPLE; list[5] = 999; // change one array value // Print the array values for (int value : list) System.out.print (value + " "); }}


Recommended