Documente Academic
Documente Profesional
Documente Cultură
Programming - 2
Umair Javed
BTE
Strings
Please note that this topic is not discussed in detail in the lecture. Please cover it by
yourself. Any queries are warmly welcomed.
What is a string?
A string is commonly considered to be a sequence of characters stored in memory and
accessible as a unit.
The first approach uses the new operator to instantiate an object while the shorter version
doesn't use the new operator.
-1-
Handout 33
Programming - 2
Umair Javed
BTE
String Concatenation
String concatenation in Java
Java supports string concatenation using the + operator as shown in the following
example
String cat = "cat";
System.out.println("con" + cat + "enation");
In addition, it will convert many different types (int, float etc) to objects of a String as
we have been using this form of code in many examples since the beginning of the
course.
int myVar = 25;
Var has a value " + myVar + " at this point."
//So, it will be converted into string as
Var has a value 25 at this point."
Note: string concatenation can also be achieved by using the concat method of the
string class. We see the example of using concat shortly.
class StringTest{
public static void main(String[] args){
String str1 = "first string";
String str2 = "second string";
System.out.println("Display original string values");
System.out.println(str1);
System.out.println(str2);
System.out.println("Replace str1 with another string");
-2-
Handout 33
Programming - 2
Umair Javed
BTE
Output
File StringTest.java
This application illustrates the fact that while a
String object cannot be modified, the reference variable
can be modified to point to a new String object which
can have the appearance of modifying the original String
object.
The output from this program is
Display original string values
first string
second string
Replace str1 with another string
Display new str1 string
first string second string
It is important to note that the following statement does not modify the original object
pointed to, by the reference variable named str1.
str1 =
Rather, this statement creates a new object which is concatenation of two existing objects
and causes the reference variable named str1 to point to the new object instead of the
original object.
The original object then becomes eligible for garbage collection. See handout on Memory
Management & Garbage Collection for more info.
The above discussion is summarized in the diagram below
-3-
Handout 33
Programming - 2
Umair Javed
BTE
str1
Fist string
str2
second string
str1
str2
second string
Fist string
-4-
Handout 33
Programming - 2
Umair Javed
BTE
Comparing Strings
While comparing strings, never use == operator. The String class equals method should
be used for the comparison purposes. The example of comparing strings is given in the
coming next program.
Remember
everything is a reference (except primitives)
==
equals() method
Default implementation same as ==
String class overrides to do a deep comparison, i.e. comparison of
characters.
String Class
// Constructors
public String();
// Methods
public char charAt(int
public String
index);
concat(String
str);
substring(int
substring(int
public String
public String
toLowerCase();
toUpperCase();
beginIndex);
beginIndex, int endIndex);
-5-
Handout 33
Programming - 2
Umair Javed
BTE
-6-
Handout 33
Programming - 2
Umair Javed
BTE
Output
File StringMethodsTest.java
This application illustrates the usage of some basic
methods of the string class
The output from this program is
char of str1 at 0 index: H
concatenate by method: Hello WorldPakistan
concatenate by operator: Hello WorldPakistan
comparing Strings
strings equals using equals method
substring of str1: World
str1 in lower case: hello world
using space);
tokens[0]);
tokens[1]);
tokens[2]);
System.out.println();
System.out.println(tokenizing string using comma);
String tokens1[] = str1.split(,);
System.out.println(first token: + tokens[0]);
System.out.println(second token: + tokens[1]);
}
}
-7-
Handout 33
Programming - 2
Umair Javed
BTE
Output
File TokensTest.java
This application illustrates how to tokenize string
using delimiter
The output from this program is
tokenizing string using space
first token: this
second token: is
third token: test
tokenizing string using comma
first token: web design
second token: development
-8-
Handout 33
Programming - 2
Umair Javed
BTE
The following program illustrates the conversion of string into integer and double. You
can follow the same pattern / procedure for other conversions
class ConvertStringTest{
public static void main(String[] args){
String intString = "20";
String doubleString = "35.573";
-9-
Handout 33
Programming - 2
Umair Javed
BTE
System.out.println(double1);
Output
File ConvertTest.java
This application demonstrates how to convert string into
an integer and double.
The output from this program is
converting string into int
20
20
converting string into double
35.573
35.573
- 10 -
Handout 33
Programming - 2
Umair Javed
BTE
- 11 -
Handout 33
Programming - 2
Umair Javed
BTE
References:
- 12 -