{

Best 15 examples of a String class| string class in java


String class or object in java

java String class Object Examples

Strings are a sequence of characters in any language. For Java, the sequence of characters is stored in the form of java.lang.String object. Strings are immutable objects, meaning once created, the contents of the string, can not be modified in memory which means string objects are read-only.

String class is a final class, which means we can’t able to override the behavior of the methods defined in strings.

Features:

  • String is an immutable class

  • It is a thread-safe and the value of the string will never change

  • It is the final class

  • Store the string objects in heap memory, not in reading only memory

  • The string class is defined in java.lang package

  • It provides various operations and methods to manipulate strings

why are String objects immutable?

The string is an immutable and here is an example

String s="hadoop";  
s="cloud"+s;  

In the above example,

  • string ’s' reference is created with value as Hadoop.
  • Next line of code, appending the string with the cloud which means string “cloud” object is created.
  • new string is created with the value ‘cloudhadoop’ which is referenced bys, original value “Hadoop” is unreferenced

How to create a string object:-**

There are many ways to create a string object in java

  • using overloaded constructors
  • Inline initialization
String cloudString=new String(); string object created without value  
  
String cloudString=new String("cloudhadoop"); string object created with cloudhadoop value  
String cloudChars="{'c','l','o','u','d'}"  
String cloudString=new String(cloudChars);  
String cloudString="cloudhadoop"  
String cloudString=new String(cloudChars);

How to find the length of the string?

String class has a length method that returns the number of characters in the string.

string.length();

Concat strings or add the strings in java

we can use either Concat() method or the plus operator to add the strings. Both will be used to add the strings at the end.

String sObj="cloud";  
String addedString="hadoop+sObj; or  
  
String sObj="cloud";  
String addedString="s.concat("hadoop");  

How to compare two strings of data and references in java?

String sObj="cloud";  
String addedString="cloud";  

In the above how many objects are created? the answer is only one object but the references are created two which are pointing to the cloud object.

In Java, We can use the == operator to compare references to the two strings if we want to compare two string objects' data, we can use the equals method.
In the above example, == returns true and equal method returns true.

we can also use the equalsIgnoreCase method to ignore the string case sensitivity.

How to Split a string into substrings?

strings are separated into a list of substrings with delimiter specified using the String.split(String) method

String s="Best String examples";  
String delimeter=" ";  
String array[]=s.split(delimeter);  

output:- Best String examples

format String with an example in java:-**

format method is provided in String object which is similar to c language println format. To format one of the strings, we use codes to represent the different value types.

String.format("Hi %s, today is %d day of month","Kiran",29);  

and the output is Hi Kiran, today is the 29 day of the month.

  • %s represents strings type
  • %d represents integer type -%f represents float type

capital codes represent capital types types

Convert String to Integer in java

This is a common requirement encountered in programmer life The string should contain numbers enclosed in double-quotes.
We can convert in two ways. One way is to use Integer.parseInt() method which returns primitive int value.
Other way is using Integer.valueOf() method which returns Integer Object.

It will convert to Integer. You can check my previous post about full tutorial

String numberString = "90";  
int number = Integer.parseInt(numberString);  
System.out.println(number); // returns 90  
String numberString1 = "94";  
int number1 = Integer.valueOf(numberString1);  
System.out.println(number1); // returns 94  
String numberString2 = "94abc";  
int number2 = Integer.valueOf(numberString2);  
System.out.println(number2); // returns java.lang.NumberFormatException: For input string: "94abc"  
}  

if the number in string format is not valid, It throws NumberFormatException

Reverse a String example in java

The StringBuilder.reverse() method is used to reverse the string. First Convert String to StringBuilder using append() method.

String str = "CloudHadoop";  
StringBuilder strBuilder = new StringBuilder();  
strBuilder.append(str);  
strBuilder = strBuilder.reverse();  
System.out.println(strBuilder);  

Output is

poodaHduolC

Conclusion

To Summarize, You learned the basics about string objects in java and all examples about string conversion and creation.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.