Pertemuan 10 - Java String
Pertemuan 10 - Java String
Pertemuan 10 - Java String
Objek
JAVA STRING
Java String
In Java, a string is a sequence of characters. For example, "hello" is a string containing a
sequence of characters 'h', 'e', 'l', 'l', and 'o’.
Here, we have created a string variable named type. The variable is initialized with the string
Java Programming.
Java String
Java String
class Main {
// create strings
// print strings
}
Java String
There are two ways to create a string in Java:
String Literal
Using new Keyword
Java String
1. String literal
To make Java more memory efficient (because no new objects are created if it exists already in
the string constant pool).
Example:
String s = “PTI”;
Java String
2. Using new keyword
Example:
Java String class provides a lot of methods to perform operations on strings such as compare(),
concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc.
// create a string
}
Java String - Join Two Java Strings
We can join two strings in Java using the concat() method.
Java String - Join Two Java Strings
class Main {
// create second
}
Java String - Compare two Strings
In Java, we can make comparisons between two strings using the equals() method.
Java String - Compare two Strings
class Main {
// create 3 strings
}
Java String - Escape character
The escape character is used to escape some of the characters present inside a string.
If you print any object, Java compiler internally invokes the toString() method on the object. So
overriding the toString() method, returns the desired output, it can be the state of an object etc.
depending on your implementation.
Java String - toString() Method
class Student{
this.rollno=rollno;
this.name=name;
this.city=city; }
} }
Java String - toString() Method
class Student{
this.rollno=rollno;
this.name=name;
this.city=city; }
public String toString(){//overriding the toString() method
return rollno+" "+name+" "+city; }
public String toString(){//overriding the toString() method
} }
Terima Kasih
Tugas
Carilah method yang dapat digunakan dalam java string, kemudian buatlah contoh dari
penggunaan method tersebut, minimal 2 method (selain yang sudah ada dalam ppt)