Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS2011 Introduction to Programming I Strings

Similar presentations


Presentation on theme: "CS2011 Introduction to Programming I Strings"— Presentation transcript:

1 CS2011 Introduction to Programming I Strings
Chengyu Sun California State University, Los Angeles

2 Class A class can be considered as a user-defined type For example:
public class Point { double x; double y; }

3 Object Objects are instances of a class Type Instances Primitive Type
Values Class Objects

4 Static Method A class may have methods
Methods declared as static are associated with the class For example: Math.random() Character.isDigit() <class_name>.<method_name>

5 Instance Method Methods without static are associated with each object (i.e. instance) of a class, so they are called instance methods

6 Instance Method Example
Class Object Create an object Scanner in = new Scanner( System.in ); int n = in.nextInt(); in.close(); Instance Methods <object_name>.<method_name>

7 String String is a Java class representing a sequence of character
String values (i.e. objects) are text enclosed in double quotes, e.g. String message = "Welcome to Java"; Text processing is a very common task in programming, so it's very important for us to know how to handle Strings.

8 Some String Instance Methods
length() charAt() concat() toUpperCase() toLowerCase() trim() substring()

9 How To Read API Documentation
Constructors Fields Methods Static, Instance, Concrete, Deprecated Arguments and return value

10 Search Inside a String indexOf() lastIndexOf()
Example: split a name into first name and last name Handle erroneous input (i.e. no whitespace) Handle leading/trailing whitespaces Handle multiple whitespaces

11 Read String from Console
Scanner methods next() nextLine() Example: read name from input in the SplitName example

12 Formatting Output double amount = 12618.98;
double interestRate = ; double interest = amount * interestRate; System.out.printf("Interest is $%4.2f", interest);

13 Format Specifier % 4 .2 f Width, i.e. minimum Number of characters
To be written to output Start of a format specifier % 4 .2 f Conversion Character Precision, i.e. number of Digits after the decimal point for floating-point numbers d: decimal integer f: floating-point number c: character s: String Java printf() Quick Reference

14 String Comparison String s1 = "ab"; String s2 = "abc".substring(0,2);
System.out.println(s1); System.out.println(s2); System.out.println(s1 == s2); // true or false??

15 Value vs. Reference Variables of primitive types hold values
int a = 1; int b = a; a = 2; System.out.print(a); //?? System.out.print(b); //?? Point a = new Point(); a.x = 1; Point b = a; a.x = 2; System.out.print(a.x); //?? System.out.print(b.x); //?? Variables of primitive types hold values Variables of class types hold references

16 Reference An object reference can be thought as the location of the object in memory, or a pointer pointing to the object Create a new object in memory Point a = new Point(); Point b = a; Assign the location of the object to a Copy the location of the object to b

17 Reference Comparison s1 s1 "ab" "ab" s2 s2 "ab" s1 == s2 s1 == s2 true
false

18 String Comparison By Value
equals() equalsIgnoreCase() compareTo() compareToIgnoreCase()

19 More String Comparisons
startsWith() endsWith() contains()

20 Interned Strings String s1 = "abc"; String s2 = "abc";
Sytem.out.println( s1 == s2 ); // true In order to improve memory efficiency, JVM keeps a "pool" of the most recently used String literals If two String literals are the same, they refer to the same String object in the pool But, what if we change s1, e.g. s1 = "xyz" ??

21 String Are Immutable The content of a String cannot be changed – when it seems like a String is changed, it's actually a new String being created "xy" s1 s1 "ab" "ab" String s1 = "ab"; s1 = "xy";

22 String Conversion String s int i String s double d Integer.parseInt(s)
Double.parseDouble(s) String s double d d + ""

23 Example: Hex2Dec2 Convert a 2-digit hexadecimal number to a decimal value Hex Digit Decimal Value 1 - 9 1 – 9 A 10 B 11 C 12 D 13 E 14 F 15

24 Readings Chapter 4 of the textbook (there will be a quiz next week)


Download ppt "CS2011 Introduction to Programming I Strings"

Similar presentations


Ads by Google