Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 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.";

2 Sahar Mosleh California State University San MarcosPage 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

3 Sahar Mosleh California State University San MarcosPage 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

4 Sahar Mosleh California State University San MarcosPage 4 Some Methods in the Class String

5 Sahar Mosleh California State University San MarcosPage 5

6 Sahar Mosleh California State University San MarcosPage 6

7 Sahar Mosleh California State University San MarcosPage 7

8 Sahar Mosleh California State University San MarcosPage 8

9 Sahar Mosleh California State University San MarcosPage 9

10 Sahar Mosleh California State University San MarcosPage 10 String Indexes

11 Sahar Mosleh California State University San MarcosPage 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

12 Sahar Mosleh California State University San MarcosPage 12

13 Sahar Mosleh California State University San MarcosPage 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);

14 Sahar Mosleh California State University San MarcosPage 14 Example: Public static int maximum (intn1,intn2) { if (n1>n2) return n1; else return 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.

15 Sahar Mosleh California State University San MarcosPage 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;

16 Sahar Mosleh California State University San MarcosPage 16 Some Methods in the Class Math

17 Sahar Mosleh California State University San MarcosPage 17

18 Sahar Mosleh California State University San MarcosPage 18

19 Sahar Mosleh California State University San MarcosPage 19

20 Sahar Mosleh California State University San MarcosPage 20

21 Sahar Mosleh California State University San MarcosPage 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

22 Sahar Mosleh California State University San MarcosPage 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

23 Sahar Mosleh California State University San MarcosPage 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

24 Sahar Mosleh California State University San MarcosPage 24 Variables in Memory

25 Sahar Mosleh California State University San MarcosPage 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

26 Sahar Mosleh California State University San MarcosPage 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

27 Sahar Mosleh California State University San MarcosPage 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;

28 Sahar Mosleh California State University San MarcosPage 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; } : }

29 Sahar Mosleh California State University San MarcosPage 29 Class Type Variables Store a Reference

30 Sahar Mosleh California State University San MarcosPage 30

31 Sahar Mosleh California State University San MarcosPage 31 Assignment Operator with Class Type Variables

32 Sahar Mosleh California State University San MarcosPage 32

33 Sahar Mosleh California State University San MarcosPage 33


Download ppt "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."

Similar presentations


Ads by Google