Date post: | 18-Jan-2016 |
Category: |
Documents |
Upload: | donna-fitzgerald |
View: | 219 times |
Download: | 0 times |
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.";
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
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
Sahar Mosleh California State University San Marcos Page 4
Some Methods in the Class String
Sahar Mosleh California State University San Marcos Page 5
Sahar Mosleh California State University San Marcos Page 6
Sahar Mosleh California State University San Marcos Page 7
Sahar Mosleh California State University San Marcos Page 8
Sahar Mosleh California State University San Marcos Page 9
Sahar Mosleh California State University San Marcos Page 10
String Indexes
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
Sahar Mosleh California State University San Marcos Page 12
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);
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.
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;
Sahar Mosleh California State University San Marcos Page 16
Some Methods in the Class Math
Sahar Mosleh California State University San Marcos Page 17
Sahar Mosleh California State University San Marcos Page 18
Sahar Mosleh California State University San Marcos Page 19
Sahar Mosleh California State University San Marcos Page 20
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
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
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
Sahar Mosleh California State University San Marcos Page 24
Variables in Memory
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
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
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;
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;}:
::}
Sahar Mosleh California State University San Marcos Page 29
Class Type Variables Store a Reference
Sahar Mosleh California State University San Marcos Page 30
Sahar Mosleh California State University San Marcos Page 31
Assignment Operator with Class Type Variables
Sahar Mosleh California State University San Marcos Page 32
Sahar Mosleh California State University San Marcos Page 33