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

What is a String class in java?

String is 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 object is an immutable. Let’s see an example for String immutable.

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.
  • two objects are created (hadoop and cloud), another new string is created with the value cloudhadoop which is referenced by s, original value “Hadoop” is unreferenced.
  • Since original string object value is lost, hence called immutable.

How to create a string object in java?

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();

How to append the strings in java?

we can use either Concat() method or the plus operator to apped 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?

Sometimes, We need to check whether given string are equal or not. Let’s create a two strings

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

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

There are two ways we can compare strings

  • using reference comparision(==) It compare references of two strings and checks both are pointed to same object or not..

  • Object data comparision(equal method) the equals method compare string object data.

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 in Java

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

How to 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

How to 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.