0% found this document useful (0 votes)
88 views6 pages

Understanding Java String Basics

Java String Notes

Uploaded by

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

Understanding Java String Basics

Java String Notes

Uploaded by

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

Java String

In Java, string is basically an object that represents sequence of char values. An


array of characters works same as Java string. For example:

char[] ch={'a','b','c','d','e','f','g','h','i','j'};
String s=new String(ch);
is same as:

String s="abcdefghij";

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.

What is String in Java?


Generally, String is a sequence of characters. But in Java, string is an object
that represents a sequence of characters. The [Link] class is used to
create a string object.

How to create a string object?


There are two ways to create String object:

By string literal
By new keyword

Java String literal is created by using double quotes. For Example:

String s="welcome";
2) By new keyword
String s=new String("Welcome");

In such 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 a heap (non-pool).

Java String Example

public class StringExample{


public static void main(String args[]){
String s1="java";//creating string by Java string literal
char ch[]={'s','t','r','i','n','g','s'};
String s2=new String(ch);//converting char array to string
String s3=new String("example");//creating Java string by new keyword
[Link](s1);
[Link](s2);
[Link](s3);
}}

java
strings
example

The above code, converts a char array into a String object. And displays the
String objects s1, s2, and s3 on console using println() method.
Java String class methods
The [Link] class provides many useful methods to perform operations
on sequence of char values.

Java String length()


The Java String class length() method finds the length of a string.

public class LengthExample{


public static void main(String args[]){
String s1="abcdefghi";
String s2="python";
[Link]("string length is: "+[Link]());//9 is the length of
abcdefghi string
[Link]("string length is: "+[Link]());//6 is the length of python
string
}}

Output:

string length is: 9


string length is: 6

Java String substring()


The Java String class substring() method returns a part of the string.

public class SubstringExample{


public static void main(String args[]){
String s1="abcdefghi";
[Link]([Link](2,4));
[Link]([Link](2));
}}

Output:

cd
cdefghi

Java String concat


The Java String class concat() method combines specified string at the end of
this string. It returns a combined string. It is like appending another string.

public class ConcatExample{


public static void main(String args[]){
String s1="java string";
[Link]("is immutable");
[Link](s1);
s1=[Link](" is immutable so assign it explicitly");
[Link](s1);
}}

Output:

java string
java string is immutable so assign it explicitly

Java String indexOf()


The Java String class indexOf() method returns the position of the first
occurrence of the specified character or string in a specified string.

public class IndexOfExample{


public static void main(String args[]){
String s1="this is index of example";
//passing substring
int index1=[Link]("is");//returns the index of is substring
int index2=[Link]("index");//returns the index of index substring
[Link](index1+" "+index2);//2 8

//passing substring with from index


int index3=[Link]("is",4);//returns the index of is substring after 4th index
[Link](index3);//5 i.e. the index of another is

//passing char value


int index4=[Link]('s');//returns the index of s char value
[Link](index4);//3
}}

Output:

2 8
5
3

Common questions

Powered by AI

The 'substring()' method creates a new String object that shares the original character array used by the string object being sliced. This can lead to more efficient memory usage compared to creating a completely new string instance that duplicates the relevant characters. However, if a large string is sliced using 'substring()' methods and the original is no longer needed, the shared character array might unnecessarily remain in memory, causing higher memory usage; thus, re-evaluating memory management for very large strings and frequent substring operations might be necessary .

The 'indexOf()' method in Java is useful for finding the position of a substring or a character within a string. It can be used to determine if a substring exists in a string or to locate multiple occurrences by providing a starting index. This is particularly beneficial in parsing strings, such as when extracting data from a formatted string or checking the presence of substrings before string operations .

In Java, when a string is created using a string literal (e.g., String s = "welcome";), it is stored in the string constant pool, and if a string with the same content already exists, a new object is not created. However, when a string is created using the 'new' keyword (e.g., String s = new String("Welcome");), it creates a new object in the heap memory, regardless if an equivalent string exists in the constant pool .

In Java, strings are represented internally as sequences of characters (char arrays) and are encapsulated as objects of the 'java.lang.String' class. This internal representation means that each character is stored in a fixed size, allowing operations like length and substring that manipulate indices to be very efficient. However, due to immutability, operations that modify strings, such as concatenation, result in the creation of new string objects, which can affect performance if done repeatedly in loops without optimization through, for example, StringBuilder .

The immutability of Java strings means that operations requiring string modifications, like concatenation or slicing, necessitate creating new string objects. In large-scale applications where strings are concatenated or modified frequently, this can lead to excessive memory usage and increased garbage collection overhead. To mitigate such issues, alternatives like StringBuilder or StringBuffer, which allow mutable strings, are often employed to optimize performance during extensive modifications .

The 'length()' method provides an efficient means to determine the number of characters in a string, which is essential for validation, partitioning, and optimizing algorithms that depend on string size. This method operates in constant time due to the internal character array representation of strings, allowing developers to perform size checks and loop operations efficiently. However, relying solely on length without considering other performance aspects like immutability and string object creation might not fully optimize string processing tasks in memory-intensive applications .

Java uses UTF-16 encoding for its strings, represented internally as sequences of characters. This choice supports a wide range of characters and symbols, catering to most written languages and ensuring compatibility with internationalization requirements. However, dealing with code points that use supplementary characters (above the basic multilingual plane) may require additional handling, as certain methods operate on code units rather than code points, requiring developers to use methods like codePointCount() for accurate operations with multilingual strings .

The string constant pool is a space in memory that stores unique strings created from string literals. When a new string literal is created in Java, the JVM checks if it already exists in the pool; if so, it reuses the reference instead of creating a new object. This approach saves memory and speeds up string instantiation time for literals. However, it only applies to literals and not to strings created with the 'new' keyword, which can cause potential memory inefficiencies if overlooked .

The 'concat()' method is explicitly used to append a specified string to the end of another string, returning the combined string. It requires assigning the result to a variable, as strings are immutable. The '+' operator can also concatenate strings, is often more readable, and does not need an explicit method call, but, like 'concat()', the result must be assigned to apply. Both create new string objects since strings are immutable .

Immutability in Java strings ensures that a string object cannot be changed after it's created, which enhances performance by reusing immutable objects and saving memory through the string constant pool. Security is improved since immutable objects are stable and can't have their content changed unpredictably, preventing unauthorized alteration during execution. This design also simplifies concurrent programming, as strings can be shared between threads without the need for synchronization .

You might also like