Java introduction String

The String class represents character strings in UTF-16 encoding. All string literals in Java programs, such as "abc", are implemented as instances of this class.

Strings are constant; their values cannot be changed after they are created. String buffers ( StringBuffer and StringBuilder ) support mutable strings. Because String objects are immutable they can be shared. After creating string “abc” it will be added to pool of string in the runtime and if another variable has the same value “abc” then this variable just will point to the initial string so there will be only one imitable representation of that string “abc” . This way very big strings can be reused many times without big memory footprint

Example of declaration of string:

String str = "abc";

Here are some more examples of how strings can be used directly without to be declared in variable first:

String b = "abc".substring(2,3);

The class String includes many methods, some of which:

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings:

System.out.println("abc" + "def");

Inside Object class there is method toString which is used for string conversion. All Java classes have this method but in many cases this method must be overridden to show more sensible information. By default toString implementation of Object will return:

this.getClass().getName() + "@" + Integer.toHexString(this.hashCode());

Which in most cases will not help us much. Example of override of Class Person with fields name and age:

class Person {
  String name;
  Integer age;

  public String toString() {
    return "Person{ name=" + name +", age=" + age +"}";

Example program using some of the string most common methods:

public class MainClassStringTest {
    public static void main(String[] args) {

        String a = "This is a simple string";

        // find if word simple is inside of string and get first index occurrence
        int indexOfSimple = a.indexOf("simple");
        System.out.println("Index of simple inside string 'a': " + indexOfSimple);

        // substring from indexOfSimple until the end of the string
        String simpleString = a.substring(indexOfSimple);
        System.out.println("simpleString value: " + simpleString);

        // split the substring by empty spaces
        String[] words = simpleString.split(" ");
        for(String word: words)
            System.out.println("Word inside simpleString: " + word);

        String simpleWord = words[0]; // This should give us value 'simple'
            System.out.println("Equal 'SIMPLE' and '" + simpleWord + "' with equalsIgnoreCase");


        // Get byte array and print the byres
        byte[] simpleAsByteArray = simpleWord.getBytes();
        for(byte b : simpleAsByteArray)
        // convert back from byte array to string
        String simpleWordFromByteArray = new String(simpleAsByteArray);
        System.out.println("simpleWordFromByteArray: " + simpleWordFromByteArray);

        for(int i = 0; i < simpleWord.length(); i++)
            char c = simpleWord.charAt(i);
            System.out.println("Char: " + c);


        String s = "This is a simple string";
        // check if string is contained in string
            System.out.println("'" + s + "' contains string 'is'" );


Index of simple inside string 'a': 10
simpleString value: simple string
Word inside simpleString: simple
Word inside simpleString: string
Equal 'SIMPLE' and 'simple' with equalsIgnoreCase
simpleWordFromByteArray: simple
Char: s
Char: i
Char: m
Char: p
Char: l
Char: e
'This is a simple string' contains string 'is'

Leave a Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.