BITS Pilani
Pilani Campus
Object Oriented Programming
Arrays
Introduction to Arrays
• An array is a data structure used to process a
collection of data that is all of the same type
– An array behaves like a numbered list of variables
with a uniform naming mechanism
– It has a part that does not change:
• the name of the array
– It has a part that can change:
• an integer in square brackets
– For example, given five scores:
score[0],score[1],score[2],score[3],score[4]
BITS Pilani, Pilani Campus
Creating and Accessing Arrays
• An array that behaves like a collection of variables, all
of type double, can be created using one statement
double[] score = new double[5];
• Or using two statements:
double[] score;
score = new double[5];
– The first statement declares the variable score to be of
the array type double[] (an array of doubles)
– The second statement creates an array with five numbered
variables of type double and makes the variable score
a name for the array
BITS Pilani, Pilani Campus
Creating and Accessing Arrays
• The individual variables that together make up the
array are called indexed variables
– They can also be called subscripted variables or elements of
the array
– The number in square brackets is called an index or subscript
– The number of indexed variables in an array is called the
length or size of the array
– In Java indices must be numbered starting with 0, and
nothing else
score[0], score[1], score[2], score[3], score[4]
BITS Pilani, Pilani Campus
Creating and Accessing Arrays
• When an array is created, the length of the array is
given in square brackets after the array type
• The indexed variables are then numbered starting
with 0, and ending with the integer that is one less
than the length of the array
• The declaration
double[] score = new double[5];
results in the 5 elements
score[0], score[1], score[2], score[3], score[4]
BITS Pilani, Pilani Campus
Creating and Accessing Arrays
double[] score = new double[5];
• A variable may be used in place of the integer (i.e., in
place of the integer 5 above)
– The value of this variable can then be read from the
keyboard
– This enables the size of the array to be determined
when the program is run
double[] score = new double[count];
• An array can have indexed variables of any type,
including any class type
• All of the indexed variables in a single array must be of
the same type, called the base type of the array
BITS Pilani, Pilani Campus
Declaring and Creating an Array
• An array is declared and created in almost the same way that
objects are declared and created:
BaseType[] ArrayName = new BaseType[size];
– The size may be given as an expression that evaluates
to a nonnegative integer, for example, an int variable
char[] line = new char[80];
double[] reading = new double[count];
Person[] specimen = new Person[100];
BITS Pilani, Pilani Campus
Referring to Arrays and Array Elements
• Each array element can be used just like any other single
variable by referring to it using an indexed expression:
score[0]
• The array itself (i.e., the entire collection of indexed
variables) can be referred to using the array name
(without any square brackets): score
• An array index can be computed when a program is run
– It may be represented by a variable:
score[index]
– It may be represented by an expression that evaluates
to a suitable integer: score[next + 1]
BITS Pilani, Pilani Campus
Using the score Array in a Program
• The for loop is ideally suited for performing array
manipulations:
for (int index = 0; index < 5; index++)
[Link](score[index]
+ " differs from max by "
+ (max - score[index]) );
BITS Pilani, Pilani Campus
Three Ways to Use Square Brackets []
with an Array Name
• Square brackets can be used to create a type name:
double[] score;
• Square brackets can be used with an integer value as
part of the special syntax Java uses to create a new
array:
score = new double[5];
• Square brackets can be used to name an indexed
variable of an array:
max = score[0];
BITS Pilani, Pilani Campus
The length Instance Variable
• An array is considered to be an object
• Since other objects can have instance variables, so can
arrays
• Every array has exactly one instance variable named
length
– When an array is created, the instance variable
length is automatically set equal to its size
– The value of length cannot be changed (other than
by creating an entirely new array using new)
double[] score = new double[5];
– Given score above, [Link] has a value of 5
BITS Pilani, Pilani Campus
Pitfall: Array Index Out of Bounds
• Array indices always start with 0, and always end
with the integer that is one less than the size of the
array
– The most common programming error made
when using arrays is attempting to use a
nonexistent array index
• When an index expression evaluates to some value
other than those allowed by the array declaration,
the index is said to be out of bounds
– An out of bounds index will cause a program to
terminate with a run-time error message
– Array indices get out of bounds most commonly
at the first or last iteration of a loop that
processes the array: Be sure to test for this!
BITS Pilani, Pilani Campus
Initializing Arrays
• An array can be initialized when it is declared
– Values for the indexed variables are enclosed in braces,
and separated by commas
– The array size is automatically set to the number of values
in the braces
int[] age = {2, 12, 1};
– Given age above, [Link] has a value of 3
BITS Pilani, Pilani Campus
Initializing Arrays
• Another way of initializing an array is by using a for loop
double[] reading = new double[100];
for (int index = 0; index < [Link]; index++){
reading[index] = 42.0;
}
• If the elements of an array are not initialized explicitly, they will
automatically be initialized to the default value for their base type
BITS Pilani, Pilani Campus
An Array of Characters Is Not a String
• An array of characters is conceptually a list of
characters, and so is conceptually like a string
• However, an array of characters is not an object of the
class String
char[] a = {'A', 'B', 'C'};
String s = a; //Illegal!
• An array of characters can be converted to an object of
type String, however
(continued)
BITS Pilani, Pilani Campus
An Array of Characters Is Not a String
• The class String has a constructor that has a single
parameter of type char[]
String s = new String(a);
– The object s will have the same sequence of characters as
the entire array a ("ABC"), but is an independent copy
• Another String constructor uses a subrange of a
character array instead
String s2 = new String(a,0,2);
– Given a as before, the new string object is "AB"
(continued)
BITS Pilani, Pilani Campus
An Array of Characters Is Not a String
• An array of characters does have some things in
common with String objects
– For example, an array of characters can be output
using println
[Link](a);
– Given a as before, this would produce the output
ABC
BITS Pilani, Pilani Campus
Arrays and References
• Like class types, a variable of an array type
holds a reference
– Arrays are objects
– A variable of an array type holds the address of
where the array object is stored in memory
– Array types are (usually) considered to be class
types
BITS Pilani, Pilani Campus
Arrays are Objects
• An array can be viewed as a collection of indexed variables
• An array can also be viewed as a single item whose value is a
collection of values of a base type
– An array variable names the array as a single item
double[] a;
– A new expression creates an array object and stores the
object in memory
new double[10]
– An assignment statement places a reference to the
memory address of an array object in the array variable
a = new double[10];
– The previous steps can be combined into one statement
double[] a = new double[10];
(continued)
BITS Pilani, Pilani Campus
Arrays with a Class Base Type
• The base type of an array can be a class type
Date[] holidayList = new Date[20];
• The above example creates 20 indexed reference
variables of type Date. It does not create 20
objects of the class Date
– Each of these indexed variables are automatically
initialized to null
– Any attempt to reference any them at this point would
result in a "null pointer exception" error message
(continued)
BITS Pilani, Pilani Campus
Arrays with a Class Base Type
• Like any other object, each of the indexed variables requires a
separate invocation of a constructor using new (singly, or
perhaps using a for loop) to create an object to reference
holidayList[0] = new Date();
. . .
holidayList[19] = new Date();
OR
for (int i = 0; i < [Link]; i++)
holidayList[i] = new Date();
• Each of the indexed variables can now be referenced since
each holds the memory address of a Date object
BITS Pilani, Pilani Campus
Array Parameters
• Both array indexed variables and entire arrays
can be used as arguments to methods
– An indexed variable can be an argument to a
method in exactly the same way that any variable
of the array base type can be an argument
BITS Pilani, Pilani Campus
Array Parameters
double n = 0.0;
double[] a = {2.3, 4.5, 6.7, 8.9};
int i = 2;
Given the method declaration
public void myMethod (double x)
then all of the following are legal:
myMethod(n); //n evaluates to 0.0
myMethod(a[3]); //a[3] evaluates to 8.9
myMethod(a[i]); //i evaluates to 2,
//a[2] evaluates to 6.7
BITS Pilani, Pilani Campus
Array Parameters
• An argument to a method may be an entire array
• Array arguments behave like objects of a class
– Therefore, a method can change the values stored in the
indexed variables of an array argument
• A method with an array parameter must specify the
base type of the array only
BaseType[]
– It does not specify the length of the array
BITS Pilani, Pilani Campus
Array Parameters
The following method, doubleElements, specifies an array of
double as its single argument:
public class SampleClass{
public static void doubleElements(double[] a){
int i;
for (i = 0; i < [Link]; i++)
a[i] = a[i] * 2;
. . .
}
. . .
}
(continued)
BITS Pilani, Pilani Campus
Array Parameters
• Arrays of double may be defined as follows:
double[] a = new double[10];
double[] b = new double[30];
• Given the arrays above, the method doubleElements
from class SampleClass can be invoked as follows:
[Link](a);
[Link](b);
– Note that no square brackets are used when an entire
array is given as an argument
– Note also that a method that specifies an array for a
parameter can take an array of any length as an
argument
BITS Pilani, Pilani Campus
Use of = and == with Arrays
• Because an array variable contains the memory
address of the array it names (it’s a reference), the
assignment operator (=) only copies this memory
address
– It does not copy the values of each indexed variable
– Using the assignment operator b = a; will make two array
variables be different names for the same array
– The memory address in a is now the same as the memory
address in b: They reference the same array
(continued)
BITS Pilani, Pilani Campus
Use of = and == with Arrays
• A for loop is usually used to make two different arrays
have the same values in each indexed position:
for (int i = 0;(i < [Link]) && (i < [Link]); i++){
b[i] = a[i];
}
– Note that the above code will not make b an exact copy
of a, unless a and b have the same length
(continued)
BITS Pilani, Pilani Campus
Use of = and == with Arrays
• For the same reason, the equality operator (==) only
tests two arrays to see if they are stored in the same
location in memory
– (a == b) does not test two arrays to see if they contain
the same values
– The result of the above boolean expression will be true if
a and b share the same memory address (and, therefore,
reference the same array), and false otherwise
(continued)
BITS Pilani, Pilani Campus
Use of = and == with Arrays
• In the same way that an equals method can
be defined for a class, an equalsArray
method can be defined for a type of array
– This is how two arrays must be tested to see if they
contain the same elements
BITS Pilani, Pilani Campus
Use of = and == with Arrays
public static boolean equalsArray(int[] a,
int[] b){
if ([Link] != [Link]) return false;
else{
int i = 0;
while (i < [Link]){
if (a[i] != b[i])
return false;
i++;
}
}
return true;
}
BITS Pilani, Pilani Campus
Arguments for the Method main
• The heading for the main method of a program has
a parameter for an array of String
– It is usually called args by convention
public static void main(String[] args)
– Note that since args is a parameter, it could be replaced
by any other non-keyword identifier
• If a Java program is run without giving an argument
to main, then a default empty array of strings is
automatically provided
BITS Pilani, Pilani Campus
Arguments for the Method main
• If a program requires that the main method
be provided an array of strings argument,
each element must be provided from the
command line when the program is run
java SomeProgram Hi ! there
– This will set args[0] to "Hi", args[1] to "!",
and args[2] to "there"
– It will also set [Link] to 3
BITS Pilani, Pilani Campus
Methods That Return an Array
• In Java, a method may also return an array
– The return type is specified in the same way that an array
parameter is specified. This method returns an array of int
public static int[] incrementArray(int[] a,
int increment){
int[] temp = new int[[Link]];
for (int i = 0; i < [Link]; i++)
temp[i] = a[i] + increment;
return temp;
}
BITS Pilani, Pilani Campus
Privacy Leaks with Array Instance Variables
• If an accessor method does return the contents of an
array, special care must be taken
– Just as when an accessor returns a reference to any private
object
public double[] getArray(){
return anArray;
}
– The example above will result in a privacy leak.
– Why is this so?
BITS Pilani, Pilani Campus
Privacy Leaks with Array Instance Variables
• The previous accessor method would simply return a reference
to the array anArray itself
• Instead, an accessor method should return a reference to a deep
copy of the private array object
– Below, a is an array which is an instance variable of the class containing
the getArray method
public double[] getArray(){
double[] temp = new double[[Link]];
for (int i = 0; i < [Link]; i++)
temp[i] = a[i];
return temp
}
BITS Pilani, Pilani Campus
Privacy Leaks with Array Instance Variables
• If a private instance variable is an array that has a class as its
base type, then copies must be made of each class object in
the array when the array is copied. Here b is an array of class
types and an instance variable of the class containing the
getArray method
public ClassType[] getArray(){
ClassType[] temp = new ClassType[[Link]];
for (int i = 0; i < [Link]; i++)
temp[i] = new ClassType(b[i]);
return temp;
}
BITS Pilani, Pilani Campus
Multidimensional Arrays
• It is sometimes useful to have an array with more
than one index
• Multidimensional arrays are declared and created in
basically the same way as one-dimensional arrays
– You simply use as many square brackets as there are
indices
– Each index must be enclosed in its own brackets
double[][]table = new double[100][10];
int[][][] figure = new int[10][20][30];
Person[][] = new Person[10][100];
BITS Pilani, Pilani Campus
Multidimensional Arrays
• Multidimensional arrays may have any number of
indices, but perhaps the most common number is
two
– Two-dimensional array can be visualized as a two-
dimensional display with the first index giving the row, and
the second index giving the column
char[][] a = new char[5][12];
– Note that, like a one-dimensional array, each element of a
multidimensional array is just a variable of the base type
(in this case, char)
BITS Pilani, Pilani Campus
Multidimensional Arrays
• In Java, a two-dimensional array, such as a, is
actually an array of arrays
– The array a contains a reference to a one-dimensional
array of size 5 with a base type of char[]
– Each indexed variable (a[0], a[1], etc.) contains a
reference to a one-dimensional array of size 12, also with a
base type of char[]
• A three-dimensional array is an array of arrays of
arrays, and so forth for higher dimensions
BITS Pilani, Pilani Campus
Two-Dimensional Array as an
Array of Arrays (Part 1 of 2)
BITS Pilani, Pilani Campus
Two-Dimensional Array as an
Array of Arrays (Part 2 of 2)
BITS Pilani, Pilani Campus
Using the length Instance Variable
char[][] page = new char[30][100];
• The instance variable length does not give the total
number of indexed variables in a two-dimensional array
– Because a two-dimensional array is actually an array
of arrays, the instance variable length gives the
number of first indices (or "rows") in the array
• [Link] is equal to 30
– For the same reason, the number of second indices
(or "columns") for a given "row" is given by
referencing length for that "row" variable
• page[0].length is equal to 100
BITS Pilani, Pilani Campus
Using the length Instance Variable
• The following program demonstrates how a nested for
loop can be used to process a two-dimensional array
– Note how each length instance variable is used
int row, column;
for (row = 0; row < [Link]; row++)
for (column = 0; column < page[row].length;
column++)
page[row][column] = 'Z';
BITS Pilani, Pilani Campus
Multidimensional Array Parameters
and Returned Values
• Methods may have multidimensional array
parameters
– They are specified in a way similar to one-dimensional
arrays
– They use the same number of sets of square brackets as
they have dimensions
public void myMethod(int[][] a)
{ . . . }
– The parameter a is a two-dimensional array
BITS Pilani, Pilani Campus
Multidimensional Array Parameters
and Returned Values
• Methods may have a multidimensional array
type as their return type
– They use the same kind of type specification as for
a multidimensional array parameter
public double[][] aMethod()
{ . . . }
– The method aMethod returns an array of
double
BITS Pilani, Pilani Campus
A Grade Book Class
• As an example of using arrays in a program, a class
GradeBook is used to process quiz scores
• Objects of this class have three instance variables
– grade: a two-dimensional array that records the grade of
each student on each quiz
– studentAverage: an array used to record the average
quiz score for each student
– quizAverage: an array used to record the average
score for each quiz
BITS Pilani, Pilani Campus
A Grade Book Class
• The score that student 1 received on quiz number 3
is recorded in grade[0][2]
• The average quiz grade for student 2 is recorded in
studentAverage[1]
• The average score for quiz 3 is recorded in
quizAverage[2]
• Note the relationship among the three arrays
BITS Pilani, Pilani Campus
The 2-Dimensional Array grade
BITS Pilani, Pilani Campus
THANK YOU
BITS Pilani, Pilani Campus
[Link]
Class Arrays
This class contains various methods for manipulating arrays (such as sorting and searching).
Method Summary
static type binarySearch(type[] a, type key)
Searches the specified array of type for the specified value using the binary
search algorithm.
type = byte, char, double, float, int, long, short, Object
static boolean equals(type[] a, type[] a2)
Returns true if the two specified arrays of type are equal to one another.
type = byte, char, double, float, int, long, short, Object
static void fill(type[] a, type val)
Assigns the specified type value to each element of the specified array of
type.
type = byte, char, double, float, int, long, short, Object
static void fill(type[] a, int fromIndex, int toIndex,
type val)
Assigns the specified type value to each element of the specified range of the
specified array of types.
type = byte, char, double, float, int, long, short, Object
static void sort(type[] a)
Sorts the specified array of type into ascending numerical order.
type = byte, char, double, float, int, long, short, Object
static void sort(type[] a, int fromIndex, int toIndex)
Sorts the specified range of the specified array of type into ascending
numerical order.
type = byte, char, double, float, int, long, short, Object
BITS Pilani
Pilani Campus
Object Oriented Programming
The String Class
Objectives
• Learn about literal strings
• Learn about String constructors
• Learn about commonly used methods
• Understand immutability of strings
• Learn to format numbers into strings
BITS Pilani, Pilani Campus
String class facts
• An object of the String class represents a string of
characters.
• The String class belongs to the [Link] package,
which does not require an import statement.
• Like other classes, String has constructors and
methods.
• Unlike other classes, String has two operators, + and
+= (used for concatenation).
BITS Pilani, Pilani Campus
Literal Strings
• are anonymous objects of the String class
• are defined by enclosing text in double quotes. “This
is a literal String”
• don’t have to be constructed.
• can be assigned to String variables.
• can be passed to methods and constructors as
parameters.
• have methods you can call.
BITS Pilani, Pilani Campus
Literal String examples
//assign a literal to a String variable
String name = “Robert”;
//calling a method on a literal String
char firstInitial = “Robert”.charAt(0);
//calling a method on a String variable
char firstInitial = [Link](0);
BITS Pilani, Pilani Campus
Immutability
• Once created, a string cannot be changed: none of its
methods changes the string.
• Such objects are called immutable.
• Immutable objects are convenient because several
references can point to the same object safely: there
is no danger of changing an object through one
reference without the others being aware of the
change.
BITS Pilani, Pilani Campus
Advantages Of Immutability
Uses less memory.
String word1 = "Java"; String word1 = “Java";
String word2 = word1; String word2 = new String(word1);
word1 word1 “Java"
“Java" word2 “Java"
word2
Less efficient:
OK wastes memory
BITS Pilani, Pilani Campus
Disadvantages of Immutability
Less efficient — you need to create a new string and throw
away the old one even for small changes.
String word = “java";
char ch = [Link]([Link] (0));
word = ch + [Link] (1);
word “java"
“Java"
BITS Pilani, Pilani Campus
Empty Strings
• An empty String has no characters. It’s length is
0.
String word1 = ""; Empty strings
String word2 = new String();
• Not the same as an uninitialized String.
private String errorMsg; errorMsg
is null
BITS Pilani, Pilani Campus
No Argument Constructors
• No-argument constructor creates an empty
String. Rarely used.
String empty = new String();
• A more common approach is to reassign the
variable to an empty literal String. (Often done to
reinitialize a variable used to store input.)
String empty = “”;//nothing between quotes
BITS Pilani, Pilani Campus
Copy Constructors
• Copy constructor creates a copy of an existing String.
Also rarely used.
• Not the same as an assignment.
Copy Constructor: Each variable points to a different copy of the String.
String word = new String(“Java”); word “Java"
String word2 = new String(word); word2 “Java"
Assignment: Both variables point to the same String.
String word = “Java”; word
String word2 = word;
“Java"
word2
BITS Pilani, Pilani Campus
Other Constructors
Most other constructors take an array as a
parameter to create a String.
char[] letters = {‘J’, ‘a’, ‘v’, ‘a’};
String word = new String(letters);//”Java”
BITS Pilani, Pilani Campus
Methods — length, charAt
int length(); ◼ Returns the number of characters in
the string
char charAt(i); ◼ Returns the char at position i.
Character positions in strings are numbered starting
from 0 – just like arrays.
Returns:
”Problem".length(); 7
”Window".charAt (2); ’n'
BITS Pilani, Pilani Campus
Methods — substring
Returns a new String by copying characters from an existing String.
• String subs = [Link] (i, k); television
– returns the substring of chars in
positions from i to k-1 i k
• String subs = [Link] (i); television
– returns the substring from the i-th
char to the end i
Returns:
”television".substring (2,5); “lev"
“immutable".substring (2); “mutable"
“bob".substring (9); Error
BITS Pilani, Pilani Campus
Methods — Concatenation
String word1 = “re”, word2 = “think”; word3 = “ing”;
int num = 2;
• String result = word1 + word2;
//concatenates word1 and word2 “rethink“
• String result = [Link] (word2);
//the same as word1 + word2 “rethink“
• result += word3;
//concatenates word3 to result “rethinking”
• result += num; //converts num to String
//and concatenates it to result “rethinking2”
BITS Pilani, Pilani Campus
Methods — Find (indexOf)
0 2 6 10 15
String name =“President George Washington";
Returns:
[Link] ('P'); 0
[Link] ('e'); 2
[Link] ("George"); 10
(starts searching
[Link] ('e', 3); 6
at position 3)
[Link] ("Bob"); -1 (not found)
[Link] ('e'); 15
BITS Pilani, Pilani Campus
Methods — Equality
boolean b = [Link](word2);
returns true if the string word1 is equal to word2
boolean b = [Link](word2);
returns true if the string word1 matches word2, case-
blind
b = “Raiders”.equals(“Raiders”);//true
b = “Raiders”.equals(“raiders”);//false
b = “Raiders”.equalsIgnoreCase(“raiders”);//true
if([Link](“raiders”))
[Link](“Go You “ + team);
BITS Pilani, Pilani Campus
Methods — Comparisons
int diff = [Link](word2);
returns the “difference” word1 - word2
int diff = [Link](word2);
returns the “difference” word1 - word2,
case-blind
Usually programmers don’t care what the numerical “difference” of
word1 - word2 is, just whether the difference is negative (word1 comes
before word2), zero (word1 and word2 are equal) or positive (word1
comes after word2). Often used in conditional statements.
if([Link](word2) > 0){
//word1 comes after word2…
}
BITS Pilani, Pilani Campus
Comparison Examples
//negative differences
diff = “apple”.compareTo(“berry”);//a before b
diff = “Zebra”.compareTo(“apple”);//Z before a
diff = “dig”.compareTo(“dug”);//i before u
diff = “dig”.compareTo(“digs”);//dig is shorter
//zero differences
diff = “apple”.compareTo(“apple”);//equal
diff = “dig”.compareToIgnoreCase(“DIG”);//equal
//positive differences
diff = “berry”.compareTo(“apple”);//b after a
diff = “apple”.compareTo(“Apple”);//a after A
diff = “BIT”.compareTo(“BIG”);//T after G
diff = “huge”.compareTo(“hug”);//huge is longer
BITS Pilani, Pilani Campus
Methods — trim
String word2 = [Link] ();
returns a new string formed from word1 by
removing white space at both ends
does not affect whites space in the middle
String word1 = “ Hi Bob “;
String word2 = [Link]();
//word2 is “Hi Bob” – no spaces on either end
//word1 is still “ Hi Bob “ – with spaces
BITS Pilani, Pilani Campus
Methods — replace
String word2 = [Link](oldCh, newCh);
returns a new string formed from word1 by
replacing all occurrences of oldCh with newCh
String word1 = “rare“;
String word2 = “rare“.replace(‘r’, ‘d’);
//word2 is “dade”, but word1 is still “rare“
BITS Pilani, Pilani Campus
Methods — Changing Case
String word2 = [Link]();
String word3 = [Link]();
returns a new string formed from word1 by
converting its characters to upper (lower) case
String word1 = “HeLLo“;
String word2 = [Link]();//”HELLO”
String word3 = [Link]();//”hello”
//word1 is still “HeLLo“
BITS Pilani, Pilani Campus
Replacements
• Example: to “convert” word1 to upper case, replace the
reference with a new reference.
word1 = [Link]();
• A common bug:
word1
[Link](); remains
unchanged
BITS Pilani, Pilani Campus
Numbers to Strings
Three ways to convert a number into a string:
1. String s = "" + num; Integer and Double are
s = “” + 123;//”123” “wrapper” classes from
2. String s = [Link] (i); [Link] that represent
numbers as objects.
String s = [Link] (d); They also provide useful
s = [Link](123);//”123” static methods.
s = [Link](3.14); //”3.14”
3. String s = [Link] (num);
s = [Link](123);//”123”
BITS Pilani, Pilani Campus
Review Questions:
1. The String class is part of what package?
2. What does the String class have that
other classes do not have?
3. “Text enclosed in quotes is called ?”
4. What is the returned value for
“Rumplestiltskin”.length()?
5. Define immutable objects.
BITS Pilani, Pilani Campus
Review (cont’d):
6. How does immutability of Strings make
Java more efficient?
7. How does immutability of Strings make
Java less efficient?
8. How do you declare an empty string?
9. Why are String constructors not used very
often?
10. “Bob” + “ “ + “Smith” is called ____ ?
BITS Pilani, Pilani Campus
Review (cont’d):
11. String city = "Bloomington“;
What is returned by [Link] (2)?
12. By [Link](2, 4)?
13. By [Link](‘o’)?
14. By [Link](3)?
15. What does the trim method do?
BITS Pilani, Pilani Campus
Review (cont’d):
16. “sam”.equals(“Sam”) returns ?
17. What kind of value does
“sam”.compareTo(“Sam”) return?
18. What will be stored in s?
s = “mint”.replace(‘t’, ‘e’);
19. What does [Link]() do to s?
20. Name a simple way to convert a number into
a string.
BITS Pilani, Pilani Campus
THANK YOU
BITS Pilani, Pilani Campus