Chapter 9- Strings

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Is the following syntax acceptable? "Welcome to Java".charAt(0);

Yes, this is correct. The return value is W.

How can you construct a string?

You can create a string object from a string literal or from an array of characters. To create a string from a string literal, use the syntax: String newString = new String(stringLiteral); stringLiteral is in double quotes (i.e. "Welcome to Java")

How can you retrieve the length of the content of a string object?

length() : int

How do you compare two strings without case sensitivity?

string1.equalsIsIgnoreCase(string2);

What classes are used to process strings?

String, StringBuilder, and StringBuffer

What does the "==" operator do with strings when used for comparison?

The "==" operator checks only whether string1 and string2 refer to the same object; it does not tell you whether they have the same content (i.e. "Welcome to Java").

What's the use of String comparison methods in the String class?

To compare the contents of two strings rather than determining whether or not they reference the same object as with the "==" operator.

What method should you use to determine if two strings have the same content?

Use the equals method. For example: boolean example = string1.equals(string2);

What does it mean to say a String object is immutable?

A String object is immutable meaning that its contents CANNOT be changed.

What is the key property of a String object?

A String object is immutable: Its content cannot be changed once the string is created.

Accurately describe what a String variable is.

A String variable holds a reference to a String object that store a string value. Strictly speaking, the terms String variable, String object, and string value are different, but most of the time the distinctions between them can be ignored.

What is a string?

A string is a sequence of characters. Strings are frequently used in programming. In many languages strings are treated as an array of characters, but in Java a string is treated as an object.

What is an interned string?

Because strings are immutable and are ubiquitous in programming, the JVM uses a unique instance for string literals with the same character sequence in order to improve efficiency and save memory. Such an instance is called an interned string.

Does the following code change the contents of the string? String s = "Java"; s = "HTML";

No. The first statement creates a String object with the content "Java" and assigns its reference to s. The second statement creates a new String object with the content "HTML" and assigns its reference to s. The first String object still exists after the assignment, but it can no longer be accessed, because variable s now points to the new object.

Describe the method: concat(s1: String): String

Returns a new String that concatenates this string with string s1

What does the String method "compareTo(s1 : String)" do?

Returns an integer greater than 0, equal to 0, or less than 0 to indicate whether this string is greater than, equal to, or less than s1.

Describe the method: charAt(index: int): char

Returns the character at the specified index from this string

Describe the method: regionMatches(index : int, s1: String, s1Index: int, len: int) : boolean

Returns true if the specified subregion of this string exactly matches the specified subregion in string s1.

Describe the method: endsWith(suffix: String) : boolean

Returns true if this string ends with the specified suffix.

Describe the method: equals(s1: Object): boolean

Returns true if this string is equal to string s1.

Describe the method: startsWith(prefix: String): boolean

Returns true if this string starts with the specified prefix.

What does the String method (compareToIgnoreCase(s1 : String) do?

Same as compareTo except that the comparison is case insensitive

Describe the method: regionMatches(ignoreCase: boolean, index int, s1: String, s1Index: int, len: int): boolean

Same as regionMatches except that you can specify whether the match is case sensitive.


Ensembles d'études connexes

Chapter 7: Nutrition During Lactation Conditions and Intervention

View Set

Body Systems Cells make up tissues, and tissues make up organs. A group of organs that serves a specific function in the body is called a system. The human body has numerous body systems, and each system has the purpose of maintaining homeostasis

View Set

Maternity Chap 28, Maternity and Women's Health Nursing - Newborn, Nur 106- Module G2, Pediatric Growth & Development EAQ, Nursing Sciences EAQ, Theory Communication, Nursing SBU

View Set

Chapter 7: Health Insurance Underwriting

View Set

Population Health disease management questions from courses

View Set

wordly wise 3000 book 6 lesson 17

View Set

CISM - Risk management & Response

View Set