Strings in Java

A String is a chain of characters. String Class is provided by Java and it implements strings as object of class String. In this lesson we will learn about Strings in Java.

Create strings in Java using any of the following constructors,

The following creates an empty string,

String student = new String();

The following creates a string object. Initialized in the form of array of characters,

String message = new String(“Studyopedia provides free tutorials!”);

The following is another usage of Strings,

String result = “New York tops the list of most expensive cities for Business”;

Let us see an example of strings,

public class StudyopediaDemo {

   public static void main(String args[]) {
	 
     String message = new String("Studyopedia provides free tutorials!");
     System.out.println("Dear Learners, "+message);	 	 
  } 
}

The following is the output,

Java Strings

Let us see how to concatenate two strings,

String Concatenation in Java

To concat a string in Java, use the String concat() method,

public class StudyopediaDemo {

   public static void main(String args[]) {
	 
     String str1 = new String("Studyopedia provides free tutorials ");
     String str2 = str1;
	 
     str1 = str1.concat("for all");
    
     System.out.println("Dear Learners, "+str1);	 	 
  } 
}

The following is the output,

Java string concatenation
Let us now see why strings are mutable in Java,

Why Strings are Immutable?

Strings in Java cannot be changed, once created. Therefore, String objects are immutable. Remember that this does not apply to reference variables.

An example would make it easier to understand.

String str1 = “Example”;

Now, referred to str2,

String str2 = str1;

The following displays the text Example,

System.out.println(str1);
System.out.println(str2);

Now let us use the concat() method,

str1 = str1.concat(“Question”);

Printing str1, displays, Example Question, i.e. concatenation,

System.out.println(str1);

Printing str2, displays, Example,

System.out.println(str2);

Since concatenation did not refer to string str2, therefore the concatenation result is not visible.

Let us now see the methods in String class that allows you to manipulate strings.

Methods

String class methods allow easier manipulation of strings in Java. Below the methods are explained with suitable examples.

  • byte getBytes()
    Encodes this String into a sequence of bytes using the platform’s default charset, storing the result into a new byte array.
  • int indexOf(int ch)
    Returns the index within this string of the first occurrence of the specified character.
  • String intern()
    Returns a canonical representation for the string object.
  • String toLowerCase()
    Converts all of the characters in this String to lower case using the rules of the default locale.
  • String toUpperCase()
    Converts all of the characters in this String to upper case using the rules of the default locale.
  • String trim()
    Returns a copy of the string, with leading and trailing whitespace omitted.

Reference: Oracle Java String Class

In this lesson we learned how to work with Strings in Java.

Multidimensional Arrays in Java
String chartAt() method in Java
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment