23
NovBlack Friday Deal : Up to 40% OFF! + 2 free self-paced courses + Free Ebook - SCHEDULE CALL
Java String library is one of the most used Java libraries. In Java, the String class is encapsulated under java lang package. All the strings created in Java are of String type. Java string functions objects are immutable which means that the objects created once cannot be altered. This article discusses:
The state of the immutable objects cannot be changed and that’s why they are known as immutable objects. In Java language, the immutable classes are String, Byte, Short, Double, Integer, Float, and wrapper classes. Following example shows the way to create an immutable object of String Class: [code lang="js"]Public final class TestString { Final String str; TestString(String s) this.str=s; public String get() return str; } [/code] In the above example, the TestString class is called the immutable class as the state of the class cannot be changed, once it will be created. Read More: Java Tutorial Guide For Beginners
In Java a String object can be created by a number of ways, following are a few of them:
[code lang="js"]String str=new String ("Java").[/code]
Read: Top 51+ Simple Java Project Ideas for Beginners in 2023(Updated)
[code lang="js"]String str=new String(str1);[/code]
Literal means a simple string, written within the double quotes "" and a String literal itself is a String object: [code lang="js"]String str="Hello";[/code]
[code lang="js"]String str=str0+str1; or String str="hello" + "world";[/code] String objects inJava are stored in a string constant pool inside the heap memory. Whenever a String literal is created, JVM checks for its existence in the pool and if it already exists then a reference to the pool is created and returned.
As listed above that a String object in Java can be created with and without using the new keyword. In the below example the literals are assigned to the String class instances: [code lang="js"]String str1="Rest"; String str2="Rest";[/code] Above you can see, no String class object is created and so the new keyword is not used. Here the compiler will create only a single String object which will store the assigned literal or it will assign the literal to the provided string instance. If the object already exists in the memory then the new object of String class will not be created instead the value will be assigned to the existing object. So, whenever we need to create two different objects with same string then we should use the new keyword. Like in the following example the String objects are created with the help of new keyword. In this approach, the compiler will not create the single instance of the string object instead two objects will be created with the same string. [code lang="js"]String str1=new String ("Welcome"); String str2=new String ("Welcome");[/code] Here the two different String objects str1 and str2 will have the same value as “Welcome" and it will be stored in the memory.
Read: How to Create Object in Java with Examples?
In String class of Java, there are a number of methods or functions defined which can be directly used by the user. A few functions are an argument based and return values as well. These functions are called pre-defined functions and the user can use them directly. Following are a few of those functions with example and the concepts associated with them. You can check the Java string methods library to check all possible pre-defined functions of the String class
It is one of the most used pre-defined String function. The function is used to join or concatenate the two Java strings. Like if you have two strings say str1 and str2 then you can add these two strings using concatenation function and as a result, you will get str3. Let us suppose that we have two strings say str1="welcome" and str2="back" to add these two strings we can use the string concatenation function as a result of which you will get the third string str3=welcomeback can be created by concatenating the two strings str1 and str2. [code lang="js"] Public class Sample_Concate{ Public static void main (String args[]) { String str1="welcome" String str2="back"; String str3=str1.concat(str2); System.out.println(str3); }} [/code] The output of the above program will be: "WelcomeBack"
String Length method is used to determine the length of the given string. Whenever you have to find the length of any string you can use this method. An example is given below: [code lang="js"]Public class sample_length { Public static void main (String args[]) String str="Welcome"; System.out.println("Length of the given string is" + str.length()); }[/code] The output of the above program will be: Length of the given string is 7 Read More: Java Spring Tutorial Guide for Beginner
To compare the value of the two strings the comparison method can be used directly. For this, the two strings can be passed to the method and the result of the comparison will be displayed. In this method, the result can be case sensitive, while if you don’t want to get the case-sensitive comparison result then you can use “compareToIgnoreCase" method. [code lang="js"]Public class sample_comparison {public static void main(String args[]) String str="Welcome" System.out.println(“Compare to Welcome" + str.compareTo(welcome)); System.out.println(“Compare to welcome where case is ignored" + str.compareToIgnoreCase(“WELCOME))); }[/code] The output of the above program will be: Compare to Welcome –ve value Compare to welcome where the case is ignored 0
Read: Free Jquery SLider Carousal Plugin
If you want any of the lowercase string to be shown in uppercase then you can use the pre-defined method of conversion. For this, the methods used in Java are "ToLowerCase" and "ToUpperCase". The example is listed below: [code lang="js"]Public static void main(String args[]) { String str=welcome; System.out.println("Convert to Lowercase:" + str.toLowerCase"()); System.out.println("Convert to Uppercase" + str.toUpperCase()); }[/code] The output of the above program will be: Convert to Lowercase: welcome Convert to Uppercase: WELCOME
To know that whether a string is ending with a particular suffix you can use this method. You can use this method directly and pass the suffix which you want to check that whether a string is having that particular suffix at the end or not. The method returns a TRUE value if the suffix is found at the end of string otherwise it returns FALSE. Following example shows the use of this method: [code lang="js"]Public class Sample_Suffix { Public static void main (String args[]) { String str="welcome"; System.out.println(“Ends with character ‘e’:" + str,endsWith(“e")); } }[/code] The output of the above program will be Ends with character ‘e’: true You May Like:
Apart from the above-listed characteristics and methods there are some other facts associated with the String class like it is a final class and that’s why the String class objects are called immutable objects. For String objects, a special memory area is reserved by JVM, which is called String constant pool. You can find the String class of Java in java.lang. In the String library, you can override the String references but the content or literals are not copied. Any number closed in double quotes also becomes a string and multiple references can be created for the same string. The String is one of the most used and dynamic classes of Java. There are many functions in the String class which is beneficial for the developers of Java.
Read: Java ArrayList: What It Is & How To Create An Arraylist In Java
A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.
Cyber Security
QA
Salesforce
Business Analyst
MS SQL Server
Data Science
DevOps
Hadoop
Python
Artificial Intelligence
Machine Learning
Tableau
Search Posts
Related Posts
Top Java Developer skills To Have For Better Employment Opportunities 996.1k
Difference Between Angular 5, React JS, & Vue 302.4k
Java Collections Framework in Depth with Example for Beginners 507.1k
What is the Difference between JavaScript and JQuery? 296.9k
Java ArrayList: What It Is & How To Create An Arraylist In Java 990.5k
Receive Latest Materials and Offers on Java Course
Interviews