0% found this document useful (0 votes)
20 views3 pages

Java String Class Overview and Methods

The document provides an overview of the String class in Java, highlighting its definition, creation methods, and common functionalities. It covers various methods such as length(), charAt(), concat(), and others, along with example outputs for each. Additionally, it emphasizes the immutability of strings in Java, demonstrating that operations do not alter the original string.

Uploaded by

johnbhai7765
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Java String Class Overview and Methods

The document provides an overview of the String class in Java, highlighting its definition, creation methods, and common functionalities. It covers various methods such as length(), charAt(), concat(), and others, along with example outputs for each. Additionally, it emphasizes the immutability of strings in Java, demonstrating that operations do not alter the original string.

Uploaded by

johnbhai7765
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

■ JAVA PRINT WALL — STRING CLASS

(All-in-One)

■ 1. Basic Definition
- String is a class in [Link] package.
- Represents a sequence of characters.
- It is immutable (cannot be changed after creation).
String s = "Hello";
String t = new String("World");

■ 2. String Creation
String s1 = "Java";
String s2 = new String("Java");

■ 3. Common Methods
■ length()
String s = "Hello";
[Link]([Link]());
Output:
5

■ charAt()
[Link]("Java".charAt(2));
Output:
v

■ concat()
String s1 = "Hello ";
String s2 = "World";
[Link]([Link](s2));
Output:
Hello World

■ equals() and equalsIgnoreCase()


[Link]("JAVA".equals("java"));
[Link]("JAVA".equalsIgnoreCase("java"));
Output:
false
true

■ compareTo()
[Link]("A".compareTo("B"));
[Link]("B".compareTo("A"));
[Link]("A".compareTo("A"));
Output:
-1
1
0

■ substring()
String s = "Programming";
[Link]([Link](3));
[Link]([Link](0, 6));
Output:
gramming
Progra

■ toUpperCase() / toLowerCase()
String s = "Java";
[Link]([Link]());
[Link]([Link]());
Output:
JAVA
java

■ trim()
String s = " Hello ";
[Link]([Link]());
Output:
Hello

■ replace()
[Link]("banana".replace('a', 'o'));
Output:
bonono

■ contains()
[Link]("JavaScript".contains("Java"));
Output:
true

■ indexOf() / lastIndexOf()
String s = "banana";
[Link]([Link]('a'));
[Link]([Link]('a'));
Output:
1
5

■ startsWith() / endsWith()
[Link]("Hello".startsWith("He"));
[Link]("Hello".endsWith("lo"));
Output:
true
true

■ split()
String s = "A,B,C";
String[] arr = [Link](",");
[Link](arr[0]);
Output:
A

■ valueOf()
int x = 10;
String s = [Link](x);
[Link](s + 5);
Output:
105
■ join()
String s = [Link]("-", "Java", "Python", "C++");
[Link](s);
Output:
Java-Python-C++

■ toCharArray()
char[] arr = "Hi".toCharArray();
[Link](arr[0]);
Output:
H

■ intern()
String s1 = new String("Java").intern();
String s2 = "Java";
[Link](s1 == s2);
Output:
true

■ isEmpty() / isBlank() (Java 11)


[Link]("".isEmpty());
[Link](" ".isBlank());
Output:
true
true

■ 4. String Immutability
String s = "Java";
[Link]("World");
[Link](s);
Output:
Java
(Original string doesn’t change — creates new one.)

You might also like