String in JAVA


String in Java


String is a final class in java extends Object class. A string is something like char array but it exactly is an Object.
String class implements Comparable, Serializable, CharSequence Interfaces. We can create String using two ways in java.

i)                 String Literal.
ii)                String new keyword.

If we talk about String literal then it will be created something like that String str=”Some words”;
And on the other way if we talk about new keyword it will like String str=new String(“Some words”); both will create String str variable but the difference is String literal will create only one object in String Constant Pool if we create more than one String variable with same values all the variables will be created but they will indicate to only one object reference. This is a very memory efficient technique to create String variables.

Using new keyword we will create some variable having the same values in it but they will have separate memory location and they will take their own memory (Heap)  space every time they will be created.

Example:
String literal
String str=”suraj pratap singh”;
String str1=”suraj pratap singh”;
Variable str and str1 will share the same memory location and only one object will be created in memory area, remaining all will share the same location.

String new keyword
String str=new String (“suraj pratap singh”);
String str1=new String(“suraj pratap singh”);
Variable str and str1 will create two object having same values. This is not memory efficient.

All the String methods and constructor we can see using javap tool provided JVM.

javap java.lang.String

Compiled from "String.java"
public final class java.lang.String implements java.io.Serializable, java.lang.Comparable<java.lang.String>, java.lang.CharSequence {
  public static final java.util.Comparator<java.lang.String> CASE_INSENSITIVE_ORDER;
  public java.lang.String();
  public java.lang.String(java.lang.String);
  public java.lang.String(char[]);
  public java.lang.String(char[], int, int);
  public java.lang.String(int[], int, int);
  public java.lang.String(byte[], int, int, int);
  public java.lang.String(byte[], int);
  public java.lang.String(byte[], int, int, java.lang.String) throws java.io.UnsupportedEncodingException;
  public java.lang.String(byte[], int, int, java.nio.charset.Charset);
  public java.lang.String(byte[], java.lang.String) throws java.io.UnsupportedEncodingException;
  public java.lang.String(byte[], java.nio.charset.Charset);
  public java.lang.String(byte[], int, int);
  public java.lang.String(byte[]);
  public java.lang.String(java.lang.StringBuffer);
  public java.lang.String(java.lang.StringBuilder);
  java.lang.String(char[], boolean);
  public int length();
  public boolean isEmpty();
  public char charAt(int);
  public int codePointAt(int);
  public int codePointBefore(int);
  public int codePointCount(int, int);
  public int offsetByCodePoints(int, int);
  void getChars(char[], int);
  public void getChars(int, int, char[], int);
  public void getBytes(int, int, byte[], int);
  public byte[] getBytes(java.lang.String) throws java.io.UnsupportedEncodingException;
  public byte[] getBytes(java.nio.charset.Charset);
  public byte[] getBytes();
  public boolean equals(java.lang.Object);
  public boolean contentEquals(java.lang.StringBuffer);
  public boolean contentEquals(java.lang.CharSequence);
  public boolean equalsIgnoreCase(java.lang.String);
  public int compareTo(java.lang.String);
  public int compareToIgnoreCase(java.lang.String);
  public boolean regionMatches(int, java.lang.String, int, int);
  public boolean regionMatches(boolean, int, java.lang.String, int, int);
  public boolean startsWith(java.lang.String, int);
  public boolean startsWith(java.lang.String);
  public boolean endsWith(java.lang.String);
  public int hashCode();
  public int indexOf(int);
  public int indexOf(int, int);
  public int lastIndexOf(int);
  public int lastIndexOf(int, int);
  public int indexOf(java.lang.String);
  public int indexOf(java.lang.String, int);
  static int indexOf(char[], int, int, java.lang.String, int);
  static int indexOf(char[], int, int, char[], int, int, int);
  public int lastIndexOf(java.lang.String);
  public int lastIndexOf(java.lang.String, int);
  static int lastIndexOf(char[], int, int, java.lang.String, int);
  static int lastIndexOf(char[], int, int, char[], int, int, int);
  public java.lang.String substring(int);
  public java.lang.String substring(int, int);
  public java.lang.CharSequence subSequence(int, int);
  public java.lang.String concat(java.lang.String);
  public java.lang.String replace(char, char);
  public boolean matches(java.lang.String);
  public boolean contains(java.lang.CharSequence);
  public java.lang.String replaceFirst(java.lang.String, java.lang.String);
  public java.lang.String replaceAll(java.lang.String, java.lang.String);
  public java.lang.String replace(java.lang.CharSequence, java.lang.CharSequence);
  public java.lang.String[] split(java.lang.String, int);
  public java.lang.String[] split(java.lang.String);
  public static java.lang.String join(java.lang.CharSequence, java.lang.CharSequence...);
  public static java.lang.String join(java.lang.CharSequence, java.lang.Iterable<? extends java.lang.CharSequence>);
  public java.lang.String toLowerCase(java.util.Locale);
  public java.lang.String toLowerCase();
  public java.lang.String toUpperCase(java.util.Locale);
  public java.lang.String toUpperCase();
  public java.lang.String trim();
  public java.lang.String toString();
  public char[] toCharArray();
  public static java.lang.String format(java.lang.String, java.lang.Object...);
  public static java.lang.String format(java.util.Locale, java.lang.String, java.lang.Object...);
  public static java.lang.String valueOf(java.lang.Object);
  public static java.lang.String valueOf(char[]);
  public static java.lang.String valueOf(char[], int, int);
  public static java.lang.String copyValueOf(char[], int, int);
  public static java.lang.String copyValueOf(char[]);
  public static java.lang.String valueOf(boolean);
  public static java.lang.String valueOf(char);
  public static java.lang.String valueOf(int);
  public static java.lang.String valueOf(long);
  public static java.lang.String valueOf(float);
  public static java.lang.String valueOf(double);
  public native java.lang.String intern();
  public int compareTo(java.lang.Object);
  


Example Program
public class StringExample {
public static void main(String[] args) {
String str = "String Example Program";
String s1= "JAVA";
String s2= "Java";
String s3 = "  Hello Java  ";

System.out.println("Char at index 2(third position): " + str.charAt(2));
System.out.println("After Concat: "+ str.concat("-Enjoy-"));
System.out.println("Checking equals ignoring case: " +s2.equalsIgnoreCase(s1));
System.out.println("Checking equals with case: " +s2.equals(s1));
System.out.println("Checking Length: "+ str.length());
System.out.println("Replace function: "+ str.replace("fun", "easy"));
System.out.println("SubString of str: "+ str.substring(8));
System.out.println("SubString of str: "+ str.substring(8, 12));
System.out.println("Converting to lower case: "+ str.toLowerCase());
System.out.println("Converting to upper case: "+ str.toUpperCase());
System.out.println("Triming string: " + s3.trim());
System.out.println("searching s1 in str: " + str.contains(s1));
System.out.println("searching s2 in str: " + str.contains(s2));

char [] charArray = s2.toCharArray();
System.out.println("Size of char array: " + charArray.length);
System.out.println("Printing last element of array: " + charArray[3]);

}

}






String Immutable

We call String as Immutable just because it is not modifiable whenever we change or assign a new value to a String variable there will be created new object every time and the reference variable would be the same but the reference will be changed every time. That’s why it is advisable to careful when using strings. 

Read below posts.
Most Frequently Asked Java Interview Programs. PART-1
Most Frequently Asked Java Interview Programs PART-2

Suraj Pratap Singh(India)

Comments

Popular Posts