Core Java Tutorials


Introduction

Java Hello World

Java Compilation

JDK New Features

JVM Architecture

Java OOPs Concepts

Java Package

Array

Exception Handling

String

Immutable Class

File Handling

Serialization

MultiThreading

Volatile

Concurrent Package

Collection

Collection Internal

Generics

Cloning

Reflection In Java

Annotation

Class Loader

Java Inner classes

Garbage Collector

JDBC

References In Java

Heap Dump & Thread Dump

String in Java

String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). In Java programming language, strings are treated as objects. The Java platform provides the String class to create and manipulate strings.
The String class represents character strings. 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 support mutable strings. Because String objects are immutable they can be shared.

 String representation inside JVM

 String Literal vs. String Object

As mentioned, there are two ways to construct a string: implicit construction by assigning a string literal or explicitly creating a String object via the new operator and constructor. For example,
1. String str1="Agile & Scrum"; //using String literal(inside method area->Intern Pool)
2. String str2=new String("Agile & Scrum"); //using new operator(inside java heap)


 String Pool or Intern Pool

String objects are most used data objects in Java. Hence, java has a special mechanism to store the string objects. String Constant Pool is one such arrangement. String Constant Pool is the memory space in heap memory specially allocated to store the string objects created using string literals. String pool is based on Fly Weight Design pattern.

Whenever you create a string object using string literal, JVM first checks the content of the object to be created. If there exist an object in the string constant pool with the same content, then it returns the reference of that object. It doesn't create a new object. If the content is different from the existing objects then only it creates new object.

 String Use cases:
 String Important methods:

length() - it's return length of string.
String str="Agile";
int len=str.length();
System.out.println("len = "+len); ->> output->> :len = 5


 charAt method:

charAt() - Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.
String str="Agile and Scrum";
for(int x=0;x<str.length();x++) {
         char ch=str.charAt(x);
         System.out.println("x = "+x);
}


 substring:

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1 Thus the length of the substring is endIndex-beginIndex. Example:-


 split method:

Splits this string around matches of the given regular expression. Trailing empty strings are therefore not included in the resulting array.
Example:-


 String contains method:

contains() - Returns true if and only if this string contains the specified sequence of char values. .
String str="java spring hibernate jsp ";
boolean b=str.contains("spring");
if(b) {
      System.out.println("spring is present inside str");
}else {
     System.out.println("spring is not present inside str");
}


 Converts this string to a new character array:

toCharArray() - Returns true if and only if this string contains the specified sequence of char values. .

public char[] toCharArray() {
// Cannot use Arrays.copyOf because of class initialization order issues
	char result[] = new char[value.length];
	System.arraycopy(value, 0, result, 0, value.length);
	return result;
}
						


 valueOf of method of String:

valueOf() -Returns the string representation of the Object argument.

public char[] String valueOf(Object obj) {
   return (obj == null) ? "null" : obj.toString();
}
						


 indexOf methods:

indexOf() - Returns the index within this string of the first occurrence of the specified substring.
String str="java spring hibernate jsp ";
int len=str.indexOf("spring");
System.out.println("index = "+index); ->> output->> :index = 5

  The substring() Method in JDK 6 and JDK 7

substring() in JDK 6 String is supported by a char array. In JDK 6, the String class contains 3 fields: char value[], int offset, int count. They are used to store real character array, the first index of the array, the number of characters in the String.

When the substring() method is called, it creates a new string, but the string's value still points to the same array in the heap. The difference between the two Strings is their count and offset values.