Java Strings

Java Strings

A String in Java is a sequence of characters, used for storing text.
Strings in Java are objects, not primitive data types.

Example:



Creating Strings

There are two common ways to create strings:

1. Using String Literal (Recommended)

2. Using new Keyword


🧵 String Output Example


 String Length

Use .length() method:


 String Methods

Uppercase and Lowercase


Finding Characters

Use .indexOf() to find character or word position:


 Concatenation (Joining Strings)

Method 1: Using +

Method 2: Using .concat()


 Special Characters (Escape Characters)

CharacterMeaning
\"Double quote
\\Backslash
\nNew line
\tTab

Example:



 Strings Are Immutable

Once created, a string cannot be changed.
Example:



 Comparing Strings

Use .equals() instead of ==.


 


 String Formatting


 


⭐ Complete Example



📌 Summary

FeatureDetails
TypeNon-Primitive
Mutable?No (Strings are immutable)
Stored inString Pool (for literals)
Important Methodslength(), toUpperCase(), toLowerCase(), indexOf(), concat(), equals(), contains()

You may also like...