tutorialcup
String Methods Java
In Java programming language, a string is nothing but a sequence of characters. It is the most widely used object. Java String class has many methods that are used for various manipulations. It is immutable, meaning, its value cannot be changed. A string is equivalent to an array of characters.
Creating a string in Java
We can create a string using 2 different methods:
- Using string literal
- Using new keyword
Create a string using a string literal
This is the most direct way of creating a string in Java. We can create a string by enclosing the value in double-quotes. Here the variable "value" of type String holds the string named as "Java language"
String value = "Java language";
When we create a string literal, JVM first checks in the "string constant pool" if the string exists. If it does not exist, JVM creates a new string instance, else only a reference to the pooled instance will be returned. For example, in the below case, both string objects hold the same value. Hence only 1 object is created (i.e s1) and s2 will have the reference to s1. This means, irrespective of how many ever string variables we create with the same value, only 1 instance will be created in the string constant pool.
"String constant pool" is nothing but the special memory to hold the string objects.
String
String Methods Java
In Java programming language, a string is nothing but a sequence of characters. It is the most widely used object. Java String class has many methods that are used for various manipulations. It is immutable, meaning, its value cannot be changed. A string is equivalent to an array of characters.
Creating a string in Java
We can create a string using 2 different methods:
- Using string literal
- Using new keyword
Create a string using a string literal
This is the most direct way of creating a string in Java. We can create a string by enclosing the value in double-quotes. Here the variable "value" of type String holds the string named as "Java language"
String value = "Java language";
When we create a string literal, JVM first checks in the "string constant pool" if the string exists. If it does not exist, JVM creates a new string instance, else only a reference to the pooled instance will be returned. For example, in the below case, both string objects hold the same value. Hence only 1 object is created (i.e s1) and s2 will have the reference to s1. This means, irrespective of how many ever string variables we create with the same value, only 1 instance will be created in the string constant pool.
"String constant pool" is nothing but the special memory to hold the string objects.
String