Day 18: Strings:

String is a class in the java.lang package and is one of the most commonly used classes. A String in Java represents a sequence of characters. It is an immutable object, meaning once a String object is created, its content cannot be changed.

String Creation in Java

1. Using String Literals

When you create a String using double quotes (" "), it is stored in the String Pool.


Both


What is the String Pool?

The String Pool, also known as the String Constant Pool, is a special memory region inside the Heap memory. It is used to store unique string literals and helps to optimize memory usage by reusing string objects.

  • Strings in the pool are immutable and shared.
  • Java reuses strings from the pool to avoid creating duplicate objects for identical string literals.
  • Managed automatically by the JVM.
  • Created when a string literal is used. The intern() method is called.


2. Using the new Keyword

Creates a new object in the heap memory, even if the same value exists in the String Pool.


Here,

What is Heap Memory?

The Heap Memory is the general-purpose memory area where Java objects are created using the new keyword. Strings created in the heap are not shared and are treated as independent objects.

  • Strings in the heap are also immutable but are not shared by default.
  • Each string created using new String() resides in the heap memory.
  • Managed via garbage collection.

Java String Methods in Java

The String class in Java provides a wide range of methods to perform operations on strings, such as comparison, searching, modifying, and splitting. Let's see few commonly used string methods with examples.


1. length()

Returns the number of characters in the string.



output=13

2. charAt(int index)

Returns the character at the specified index.

Example:


Output: v


3. substring(int beginIndex) and substring(int beginIndex, int endIndex)


substring(int beginIndex):Extracts the string from the mentioned index of the string till the End. Index counts starts from 0.

In below example :

System.out.println("Substring (from index 7): " + str.substring(7));

Extracts from 7th Index till end so

Output: World!

substring(int beginIndex, int endIndex): Extracts the string from the mentioned string till the End string mentioned. Index counts starts from 0.

In below example :

System.out.println("Substring (0 to 5): " + str.substring(0, 5));

Extracts from 0 to 5th Index

Output: Hello


4. indexOf(String str) and lastIndexOf(String str)

  • indexOf: Returns the index of the first occurrence of the specified substring.
  • lastIndexOf: Returns the index of the last occurrence of the specified substring.


5. contains(CharSequence s)

Checks if the string contains the specified sequence of characters.


Output: true


6. equals(Object another) and equalsIgnoreCase(String another)

  • equals: Compares two strings for equality.
  • equalsIgnoreCase: Compares strings ignoring case differences.


7. toUpperCase() and toLowerCase()

Converts the string to uppercase or lowercase.



8. trim()

Removes leading and trailing whitespaces.




StringBuffer and StringBuilder

Why?

Since String is immutable, frequent modifications create multiple objects, which is inefficient. To handle this, StringBuffer and StringBuilder are used. Let's discuss more about StringBuffer and StringBuilder tomorrow. Happy Learning :)



要查看或添加评论,请登录

Steffi Graph Preethi S的更多文章

  • Arrays

    Arrays

    Been so long since I posted, Happy that i'm able to be back with basic concepts again. Let's see what is Arrays: As we…

  • Day 19: StringBuffer and StringBuilder in Java:

    Day 19: StringBuffer and StringBuilder in Java:

    Mutable String: A String that can be modified or changed is known as mutable String. and are classes in Java used for…

  • Day 17:

    Day 17:

    Static Keyword: Static is a keyword in Java. The keyword is used to indicate that a particular member (method…

  • Day 16: Jump Statements

    Day 16: Jump Statements

    Jump Statements These statements alter the normal flow of control by transferring execution. 1.

  • Day 16:

    Day 16:

    Looping Statements Looping statement are the execution of the block of code repeatedly until we break it. for Loop…

  • Day 15:

    Day 15:

    Java Control Statements: Java compiler executes the code from Top to Bottom. However Java provides the statement to…

  • Day 14: Encapsulation

    Day 14: Encapsulation

    Day 14: Encapsulation: Encapsulation in Java is integrating the data (fields) and the methods that operate on the data…

  • DAY 13 Polymorphism:

    DAY 13 Polymorphism:

    What is Polymorphism? In simple terms, polymorphism means "many forms," enabling a single method to work in different…

  • Abstract methods and abstract classes

    Abstract methods and abstract classes

    Day 12: What is Abstraction? Hiding the Implementation details. Interface allows 100% Abstraction, whereas Abstraction…

  • DAY11 Multiple Inheritance and Interfaces:

    DAY11 Multiple Inheritance and Interfaces:

    4. Multiple Inheritance Java does not support multiple inheritance with classes to avoid ambiguity caused by the…

社区洞察

其他会员也浏览了