Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Pertemuan 10 - Java String

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Pemrograman Berorientasi

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’.

String type = "Java programming";

Here, we have created a string variable named type. The variable is initialized with the string
Java Programming.
Java String
Java String
class Main {

public static void main(String[] args) {

// create strings

String first = "Java";

String second = "Python";

String third = "JavaScript";

// print strings

System.out.println(first); // print ???

System.out.println(second); // print ???

System.out.println(third); // print ???

}
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

String s = new String(“Welcome”);


In such a case, JVM will create a new string object in normal (non-pool) heap memory and the
literal “Welcome” will be placed in the string constant pool. The variable s will refer to the object
in the heap (non-pool)

Example:

String s = new String (“PTI”);


Java String
Strings in Java are not primitive types (like int, char, etc). Instead, all strings are objects of a
predefined class named String.

And, all string variables are instances of the String class.

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.

The java.lang.String class implements Serializable, Comparable and CharSequence interfaces.


Java String - Get length of a String
To find the length of a string, we use the length() method of the String.
Java String - Get length of a String
class Main {

public static void main(String[] args) {

// create a string

String greet = "Hello! World";

System.out.println("String: " + greet);

// get the length of greet

int length = greet.length();

System.out.println("Length: " + length);

}
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 {

public static void main(String[] args) {

// create first string

String first = "Java ";

System.out.println("First String: " + first);

// create second

String second = "Programming";

System.out.println("Second String: " + second);

// join two strings

String joinedString = first.concat(second);

System.out.println("Joined String: " + joinedString);

}
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 {

public static void main(String[] args) {

// create 3 strings

String first = "java programming";

String second = "java programming";

String third = "python programming";

// compare first and second strings

boolean result1 = first.equals(second);

System.out.println("Strings first and second are equal: " + result1);

// compare first and third strings

boolean result2 = first.equals(third);

System.out.println("Strings first and third are equal: " + result2);

}
Java String - Escape character
The escape character is used to escape some of the characters present inside a string.

// include double quote


String example = "This is the "String" class";
Java String - Escape character
To solve this issue, we use the escape character \ in Java.

// use the escape character


String example = "This is the \"String\" class.";
Java String - Immutable
In Java, strings are immutable. This means, once we create a string, we cannot change that
string.
Java String - Immutable
// create a string
String example = "Hello! ";

// add another string "World" to the previous string example


example = example.concat(" World");
Java String - toString() Method
If you want to represent any object as a string, toString() method comes into existence.

The toString() method returns the String representation of the object.

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{

int rollno; String name; String city;

Student(int rollno, String name, String city){

this.rollno=rollno;

this.name=name;

this.city=city; }

public static void main(String args[]){

Student s1=new Student(101,"Raj","lucknow");

Student s2=new Student(102,"Vijay","ghaziabad");

System.out.println(s1);//compiler writes here s1.toString()

System.out.println(s2);//compiler writes here s2.toString()

} }
Java String - toString() Method
class Student{

int rollno; String name; String city;

Student(int rollno, String name, String city){

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

return rollno+" "+name+" "+city; }

public static void main(String args[]){

Student s1=new Student(101,"Raj","lucknow");

Student s2=new Student(102,"Vijay","ghaziabad");

System.out.println(s1);//compiler writes here s1.toString()

System.out.println(s2);//compiler writes here s2.toString()

} }
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)

You might also like