Wednesday, January 22, 2025
HomeProgrammingWhat are Java Strings?

What are Java Strings?

In Java, Strings are objects that represent sequences of characters. Strings are immutable, meaning once a String object is created, its value cannot be changed.

Creation:
1. Using String literal:
java
String str = “Hello”;

2. Using new keyword:
java
String str = new String(“Hello”);

See also  How to Create a Drop-Down List in Excel? [closed]

Common Methods:
– length(): Returns the length of the string.
java
str.length();

– charAt(int index): Returns the character at a specified index.
java
str.charAt(0); // ‘H’

– substring(int beginIndex, int endIndex): Returns a substring.
java
str.substring(0, 2); // “He”

See also  How to Convert JAVA Object to JSON

– equals(Object obj): Compares two strings for equality.
java
str.equals(“Hello”);

– toUpperCase() / toLowerCase(): Converts to uppercase/lowercase.
java
str.toUpperCase();

Strings are widely used for storing and manipulating text, and Java provides a rich set of methods in the String class for handling them efficiently.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x