String Class Simplified in Java

1
294
Spread the love
  • 113
  • 34
  • 56
  • 149
  •  
  •  
  •  
  • 12
  •  
  •  
    364
    Shares

String Class Simplified in Java:

Java string represents character String. A quoted string is a value of type String.  All string literals (example -“Java”) is an instance of this class. Strings are constant, their values can not be changed after they are created. This is why String is an immutable class. In another way every String class is final and it can not be subclassed. Java String is a very important class to carry out day to day coding.

The String class represents sequences of characters. Once a String object is created, it is immutable. In other words, the sequence of characters that a String represents cannot be changed after it is created. The StringBuffer class, on the other hand, represents a sequence of characters that can be changed. StringBuffer objects are used to perform computations on String objects.

The String class includes a number of utility methods, such as methods for fetching individual characters or ranges of contiguous characters, for translating characters to upper- or lowercase, for searching strings, and for parsing numeric values in strings.

String literals are compiled into String objects. Where a String literal appears in an expression, the compiled code contains a String object. If s is declared as String, the following two expressions are identical:

a.equals("ABC");
"ABC".equals(a);

The string concatenation operator implicitly creates String objects.

These values of type String are known as String but underlying they are objects. The class is a predefined class that is automatically available in our java code.

The class structure of class String is given as

public final class java.lang.String extends java.lang.Object {
public String();
public String(byte[] bytes); 
public String(byte[] bytes, String enc);
public String(byte[] bytes, int offset, int length); 
public String(byte[] bytes, int offset,int length, String enc);
public String(byte[] lowbytes, int hibyte);
public String(byte[] lowbytes, int hibyte,int offset, int count);
public String(char[] value);
public String(char[] value, int offset, int;
public String(String value);
public String(StringBuffer buffer);
public static String copyValueOf(char data[]);
public static String copyValueOf(char data[], int offset, int count);
public static String valueOf(boolean b);
public static String valueOf(char c);
public static String valueOf(char[] data);
public static String valueOf(char[] data, int offset, int count);
public static String valueOf(double d);
public static String valueOf(float f);
public static String valueOf(int i);
public static String valueOf(long l);
public static String valueOf(Object obj);
public char charAt(int index);
public int compareTo(String anotherString);
public String concat(String str);
public boolean endsWith(String suffix);
public boolean equals(Object anObject);
public boolean equalsIgnoreCase(String anotherString);
public byte[] getBytes(); 
public byte[] getBytes(String enc); 
public void getBytes(int srcBegin, int srcEnd,byte[] dst, int dstBegin); 
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
public int hashCode();
public int indexOf(int ch);
public int indexOf(int ch, int fromIndex);
public int indexOf(String str);
public int indexOf(String str, int fromIndex);
public native String intern();
public int lastIndexOf(int ch);
public int lastIndexOf(int ch, int fromIndex);
public int lastIndexOf(String str);
public int lastIndexOf(String str, int fromIndex;
public int length();
public boolean regionMatches(boolean ignoreCase, int toffset,String other, int ooffset, int len);
public boolean regionMatches(int toffset, String other,int ooffset, int len);
public String replace(char oldChar, char newChar);
public boolean startsWith(String prefix);
public boolean startsWith(String prefix, int toffset);
public String substring(int beginIndex);
public String substring(int beginIndex, int endIndex);
public char[] toCharArray();
public String toLowerCase();
public String toLowerCase(Locale locale);
public String toString();
public String toUpperCase();
public String toUpperCase(Locale locale); 
public String trim();
}

The details of the class structure are given as follows:

public String();

public String() constructor creates a new String object that represents the empty string (i.e., a string with zero characters).

public String(byte[] bytes); 

public String(byte[] bytes); constructor creates a new String object that represents the sequence of characters stored in the given bytes array.

The bytes in the array are converted to characters using the system’s default character encoding scheme.

Parameter
bytes – An array of byte values.

public String(byte[] bytes, String enc);

public String(byte[] bytes, String enc) constructor creates a new String object that represents the sequence of characters stored in the given bytes array.

The bytes in the array are converted to characters using the specified character encoding scheme.

Parameter
bytes – An array of byte values.
enc – The name of an encoding scheme.

public String(byte[] bytes, int offset, int length); 

public String(byte[] bytes, int offset, int length); constructor creates a new String object that represents the sequence of characters stored in the specified portion of the given bytes array.

The bytes in the array are converted to characters using the system’s default character
encoding scheme.

Parameter
bytes – An array of byte values.
offset – An offset into the array.
length – The number of bytes to be included.

public String(byte[] bytes, int offset,int length, String enc);

public String(byte[] bytes, int offset, int length, String enc) constructor creates a new String object that represents the sequence of characters stored in the specified portion of the given bytes array.

The bytes in the array are converted to characters using the specified character encoding scheme.

Parameter
bytes – An array of byte values.
offset – An offset into the array.
length – The number of bytes to be included.
enc – The name of an encoding scheme.

public String(byte[] lowbytes, int hibyte);

public String(byte[] lowbytes, int hibyte) constructor creates a new String object that represents the sequence of characters stored in the given lowbytes array.

The type of the array elements is a byte, which is an 8-bit data type, so each element must be converted to a char, which is a 16-bit data type. The value of the hibyte argument is used to provide the value of the high-order byte when the byte values in the array are converted to char values.

More specifically, for each element i in the array lowbytes, the character at position i in the created String object is:

((hibyte & 0xff)<<8) | lowbytes[i]

This method is deprecated because it does not convert bytes into characters properly. We should instead use one of the constructors that takes a specific character encoding argument or that uses the default encoding.

Parameter
lowbytes – An array of byte values.
hibyte – The value to be put in the high-order byte of each 16-bit character.

public String(byte[] lowbytes, int hibyte,int offset, int count);

public String(byte[] lowbytes, int hibyte, int offset, int count) constructor creates a new String object that represents the sequence of characters stored in the specified portion of the lowbytes array. That is, the portion of the array that starts at offset elements from the beginning of the array and is count elements long.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 113
  • 34
  • 56
  • 149
  •  
  •  
  •  
  • 12
  •  
  •  
    364
    Shares

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here