sahar mosleh california state university san marcospage 1 the class string there is no primitive...

33
Sahar Mosleh California State University San Marcos Page 1 The Class String There is no primitive type for strings in Java The class String is a predefined class in Java that is used to store and process strings Objects of type String are made up of strings of characters that are written within double quotes Any quoted string is a constant of type String "Live long and prosper." A variable of type String can be given the value of a String object String blessing = "Live long and prosper.";

Upload: donna-fitzgerald

Post on 18-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 1

The Class String

• There is no primitive type for strings in Java• The class String is a predefined class in Java that is used

to store and process strings• Objects of type String are made up of strings of

characters that are written within double quotes• Any quoted string is a constant of type String

• "Live long and prosper."

• A variable of type String can be given the value of a String object• String blessing = "Live long and prosper.";

Page 2: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 2

• Concatenation: Using the + operator on two strings in order to connect them to form one longer string• If greeting is equal to "Hello ", and javaClass is equal to

"class", then greeting + javaClass is equal to "Hello class"

• Any number of strings can be concatenated together• When a string is combined with almost any other type of

item, the result is a string• "The answer is " + 42 evaluates to • "The answer is 42"

Concatenation of Strings

Page 3: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 3

String Methods

• The String class contains many useful methods for string-processing applications• A String method is called by writing a String object, a dot,

the name of the method, and a pair of parentheses to enclose any arguments

• If a String method returns a value, then it can be placed anywhere that a value of its type can be used

• String greeting = "Hello";• int count = greeting.length();• System.out.println("Length is " + greeting.length());

• Always count from zero when referring to the position or index of a character in a string

Page 4: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 4

Some Methods in the Class String

Page 5: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 5

Page 6: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 6

Page 7: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 7

Page 8: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 8

Page 9: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 9

Page 10: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 10

String Indexes

Page 11: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 11

Escape Sequences

• A backslash (\) immediately preceding a character (i.e., without any space) denotes an escape sequence or an escape character• The character following the backslash does not have its

usual meaning• Although it is formed using two symbols, it is regarded as a

single character

Page 12: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 12

Page 13: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 13

Static Methods

• A static method is one that can be used without a calling object

• A static method still belongs to a class, and its definition is given inside the class definition

• When a static method is defined, the keyword static is placed in the method header• public static returnedType myMethod(parameters) • { . . . }

• Static methods are invoked using the class name in place of a calling object• returnedValue = MyClass.myMethod(arguments);

Page 14: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 14

•Example:

Public static int maximum (intn1,intn2){

if (n1>n2)return n1;

elsereturn n2;

}

If the maximum method were in a classs name SomeClass, then the following is a sample invocation of maximum:

Int budget = SomeClass.maximum(yourmoney, mymoney);

yourmoney and mymoney are variables of type int that contain some value.

Page 15: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 15

The Math Class

• The Math class provides a number of standard mathematical methods• It is found in the java.lang package, so it does not require an

import statement• All of its methods and data are static, therefore they are

invoked with the class name Math instead of a calling object• The Math class has two predefined constants, E (e, the base

of the natural logarithm system) and PI (, 3.1415 . . .)• area = Math.PI * radius * radius;

Page 16: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 16

Some Methods in the Class Math

Page 17: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 17

Page 18: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 18

Page 19: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 19

Page 20: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 20

Page 21: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 21

Variables and Memory

• A computer has two forms of memory• Secondary memory is used to hold files for "permanent"

storage• Main memory is used by a computer when it is running a

program• Values stored in a program's variables are kept in main

memory

Page 22: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 22

• Main memory consists of a long list of numbered locations called bytes• Each byte contains eight bits: eight 0 or 1 digits

• The number that identifies a byte is called its address• A data item can be stored in one (or more) of these bytes• The address of the byte is used to find the data item when

needed

Page 23: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 23

• Values of most data types require more than one byte of storage• Several adjacent bytes are then used to hold the data item• The entire chunk of memory that holds the data is called its

memory location• The address of the first byte of this memory location is used

as the address for the data item

• A computer's main memory can be thought of as a long list of memory locations of varying sizes

Page 24: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 24

Variables in Memory

Page 25: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 25

• Every variable is implemented as a location in computer memory

• When the variable is a primitive type, the value of the variable is stored in the memory location assigned to the variable• Each primitive type always require the same amount of

memory to store its values

References

Page 26: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 26

• When the variable is a class type, only the memory address (or reference) where its object is located is stored in the memory location assigned to the variable• The object named by the variable is stored in some other

location in memory• Like primitives, the value of a class variable is a fixed size• Unlike primitives, the value of a class variable is a memory

address or reference • The object, whose address is stored in the variable, can be of

any size

Page 27: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 27

• Two reference variables can contain the same reference, and therefore name the same object• The assignment operator sets the reference (memory address) of

one class type variable equal to that of another• Any change to the object named by one of theses variables will

produce a change to the object named by the other variable, since they are the same object

• variable2 = variable1;

Page 28: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 28

Public class ToyClass{ private String name;private int number;public ToyClass (String initiaName, int initiaNumber)

{ name = initiaName;

number = initiaNumber;}

public ToyClass (){ name = “No name yet” number = 0;}

public void set (String newName, int newNumber){ name = newName;number = newNumber;}:

::}

Page 29: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 29

Class Type Variables Store a Reference

Page 30: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 30

Page 31: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 31

Assignment Operator with Class Type Variables

Page 32: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 32

Page 33: Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined

Sahar Mosleh California State University San Marcos Page 33