String, String Array and its simple operations

String and String Array

  • Strings in Java are Objects.
  • String is a simple char array. 
  • An Arrays are immutable, Strings are immutable as well. 
  • Whenever a change to a String is made, an entirely new String is created.
  • Immutable string which means a constant and cannot be changed once created.


Syntax:
String string_variable= “character_array”;
Example:
String str = "Sarthak";


  • Interfaces and Classes in Strings in Java:
    • CharBuffer: 
      • This class implements the CharSequence interface. 
      • This class is used to allow character buffers to be used in place of CharSequences. 
    • String: 
      • String is a sequence of characters. 
      • In java, objects of String are immutable which means a constant and cannot be changed once created.

Using String:

  • There are two ways to create string in Java:
  • Example: String literal

String s = “Sarthak”;

  • Example: Using new keyword

String s = new String (“Sarthak”);

Using StringBuffer: 

  • StringBuffer is a peer class of String that provides much of the functionality of strings. 
  • String represents fixed-length, immutable character sequences while StringBuffer represents growable and writable character sequences.
  • Syntax:

StringBuffer string_object = new StringBuffer(Charcters);

  • Example:

StringBuffer s = new StringBuffer("Sarthak");

Using StringBuilder

  • The StringBuilder in Java represents a mutable sequence of characters. 
  • Since the String Class in Java creates and immutable sequence of characters, the StringBuilder class provides an alternate to String Class, as it creates a mutable sequence of characters.
  • Syntax:

StringBuilder string_object = new StringBuilder ();

  • Example:

StringBuilder str = new StringBuilder();
str.append("Sarthak");

How to create String array?

  • Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
  • To declare an array, define the variable type with square brackets.
  • Example of declaration:

String[] cars;

  • Example of Initialization:

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

How to access elements?

  • You access an array element by referring to the index number.
  • Example:

System.out.println(cars[0]);

How to Change an Array Element?

  • To change the value of a specific element, refer to the index number.
  • Example:

cars[0] = "Opel";
System.out.println(cars[0]);

How to get array length?

  • To find out how many elements an array has, use the length property.
  • Example:

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);

How to use Loop Through an Array?

  • You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run.
  • Example:

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.length; i++) {
  System.out.println(cars[i]);
}

How to use Loop Through an Array with For-Each?

  • There is also a "for-each" loop, which is used exclusively to loop through elements in arrays.
  • Example:

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (String i : cars) {
  System.out.println(i);
}

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form