Character Strings A string of characters can be

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 © 2007 Pearson Addison-Wesley. All rights reserved 2 -1

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 © 2007 Pearson Addison-Wesley. All rights reserved 2 -2

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 © 2007 Pearson Addison-Wesley. All rights reserved 2 -3

Escape Sequences • Some Java escape sequences: Escape Sequence b t n r " ' \ Meaning backspace tab newline carriage return double quote single quote backslash • See Roses. java (page 66) © 2007 Pearson Addison-Wesley. All rights reserved 2 -4

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 © 2007 Pearson Addison-Wesley. All rights reserved 2 -5

Characters • A char variable stores a single character • Character literals are delimited by single quotes: 'a' 'X' '7' '$' ', ' 'n' • Example declarations: char top. Grade = '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 © 2007 Pearson Addison-Wesley. All rights reserved 2 -6

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 © 2007 Pearson Addison-Wesley. All rights reserved 2 -7

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: Addition Subtraction Multiplication Division Remainder + * / % • If either or both operands used by an arithmetic operator are floating point, then the result is a floating point © 2007 Pearson Addison-Wesley. All rights reserved 2 -8

Division and Remainder • If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded) 14 / 3 equals 4 8 / 12 equals 0 • The remainder operator (%) returns the remainder after dividing the second operand into the first 14 % 3 equals 2 8 % 12 equals 8 © 2007 Pearson Addison-Wesley. All rights reserved 2 -9

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: © 2004 Pearson Addison-Wesley. All rights reserved 4 -10

More Objects • Here is another example of a class: § class Window {. . . } • Here are some examples of Windows: © 2004 Pearson Addison-Wesley. All rights reserved 4 -11

Classes contain data definitions • Classes describe the data held by each of its objects • Example: Data usually goes first in a class § class Dog { String name; int age; . . . rest of the 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 © 2004 Pearson Addison-Wesley. All rights reserved 4 -12

Classes contain methods • A class may contain methods that describe the behavior of objects • Example: § class Dog { Methods usually go after the data. . . void bark() { System. out. println("Woof!"); } } • When we ask a particular Dog to bark, it says “Woof!” • Only Dog objects can bark; the class Dog cannot bark © 2004 Pearson Addison-Wesley. All rights reserved 4 -13

Arrays • An array is an ordered list of values Each value has a numeric index The entire array has a single name 0 scores 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 This array holds 10 values that are indexed from 0 to 9 © 2004 Pearson Addison-Wesley. All rights reserved 7 -14

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]); © 2004 Pearson Addison-Wesley. All rights reserved 7 -15

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 Basic. Array. java © 2004 Pearson Addison-Wesley. All rights reserved 7 -16

//********************************** // Basic. Array. java // Demonstrates basic array declaration and use. //********************************** public class Basic. Array { //--------------------------------// 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 + " "); } } © 2004 Pearson Addison-Wesley. All rights reserved 7 -17
- Slides: 17