JAVA Programming Practice
Basic Grammar2
▪ String
▪ String Methods
▪ Type Casting
1
Prof. Hwansoo Han
T.A. Hwiwon Kim
T.A. Minseop Jeong
String
2
▪ String is a sequence of characters placed in
double quote(“ ”).
▪ Strings are constant, their values cannot be
changed in the same memory after they are
created.
String s = “abcdefg”variable value of
variable “s”
How to create String
3
▪ By String literal
▪ By new keyword
String s = “abcdefg”
String s = new String(“abcdefg”)
Non-Primitive type
4
▪ String is not a primitive type variable.
• Primitive type: int, long, float, double…
• Non-Primitive type: Integer, Double, String…
▪ Primitive type example
Name of Variable data
a 24
int a = 24;
Non-Primitive type
5
▪ String is not a primitive type variable.
• Primitive type: int, long, float, double…
• Non-Primitive type: Integer, Double, String…
▪ Non-Primitive type example
Name of Variable data
s
String s = “24”;
Heap
“24”Object
We will learn “object” soon…
Immutability
6
▪ In JAVA, string objects are immutable.
Immutable simply means unmodifiable or
unchangeable.
▪ Once string object is created its data or state
can’t be changed but a new string object is
created.
Advantage of Immutability
7
▪ Use less memory
Name of Variable data
s1
String s1 = “24”;
String s2 = s1;
Heap
“24”Object
Name of Variable data
s2
Disadvantage of Immutability
8
▪ Less efficient
• You need to create a new string and throw away
the old one even for small changes.
Name of Variable data
s1
String s1 = “abc”;
s1 = s1.concat(“def”);
Heap
“abc”Object
Disadvantage of Immutability
9
▪ Less efficient
• You need to create a new string and throw away
the old one even for small changes.
Name of Variable data
s1
String s1 = “abc”;
s1 = s1.concat(“def”);
Heap
“abc”Object
“abcdef”Object
Garbage
String Methods
10
▪ length():
• Returns length of string.
String s1 = “abcdef”;
System.out.println(s1.length());
6
String Methods
11
▪ charAt(int index):
• Returns character at position ‘index’.
String s1 = “abcdef”;
System.out.println(s1.charAt(3));
d
String Methods
12
▪ substring(int begin):
• Returns substring from begin index to end of the
String.
String s1 = “abcdef”;
System.out.println(s1.substring(2));
cdef
String Methods
13
▪ substring(int begin, int end):
• Returns substring from begin index to end index of
the String.
String s1 = “abcdef”;
System.out.println(2,s1.length());
cdef
String Methods
14
▪ concat(string s2):
• Returns a string that concatenated two strings
String s1 = “abc”;
String s2 = s1.concat(“def”);
System.out.println(s2);
abcdef
String Methods
15
▪ equals(string s):
• To peform content comparision.
String s1 = “abc”;
String s2 = new String(“abc”);
System.out.println(s1 == s2);
false // different object
String Methods
16
▪ equals(string s):
• To peform content comparision.
String s1 = “abc”;
String s2 = new String(“abc”);
System.out.println(s1.equals(s2));
true
String Methods
17
▪ split(string s):
• Returns an array of sub-strings splited with input
parameters.
String s1 = “abc!def”;
System.out.println(s1.split(“!”)[0]);
System.out.println(s1.split(“!”)[1]);
abc
def
Type Casting
18
▪ String to Int
▪ String to Double
String s1 = “123”;
int n = Integer.valueOf(s1);
System.out.println(n);
123
String s1 = “10.1”;
double n = Double.valueOf(s1);
System.out.println(n);
10.1
Type Casting
19
▪ Int to String
▪ Double to String
int n = 123;
String s1 = Integer.toString(n);
System.out.println(s1);
123
double n = 10.1;
String s1 = Double.toString(n);
System.out.println(s1);
10.1
[Lab – Practice #3]
▪ New String Methods
• String addString(String s1, int index, String s2)
• Returns the string created by adding 's2' after position
'index' of 's1’.
• String reverse(String s)
• Returns reversed string of ‘s’
• String removeString (String s1, String s2)
• Returns a string with all 's2's removed from 's1'
20
[Lab – Practice #3]
▪ New String Methods
• You must follow the format of skeleton code.
• Otherwise, you may get 0 points.
• Upload StringMethod.java on icampus.
• Input/Output Example
21
System.out.println(addString("0123456",3,"-"));System.out.println(reverse("abc"));System.out.println(removeString("01001000","00"));
0123-456
cba
0110
[Lab – Practice #3]
public class StringMethod {
// Returns the string created by adding 's2' after position 'index' of 's1'.
static String addString(String s1, int index, String s2) {
}
// Returns reversed string of 's'
static String reverse(String s) {
}
// Returns a string with all 's2's removed from 's1'
static String removeString (String s1, String s2) {
}
public static void main(String[] args) {
}
}
22
▪ Skeleton Code
Top Related