- In Java, a string literal is an object of type String class.
- It is a kind of wrapper class.
- Hence manipulation of strings will be done through the use of the methods provided by the String class.
Some String class constructors :-
- public String() – creates an empty string
- public String(String value) – creates a new string that is a copy of the given string
- public String(char value, int begin, int count) – this constructs a new string based on the character array starting from the position begin, which is count characters long
- public String(StringBuffer buffer) – creates a new string based on a StringBuffer value
More String Constructor and Methods
- String length() – this method determines the length of a string.
- The == operator and equals() method can be used for string comparison
- The == operator checks if the two operands being used are one and the same object
- The equals() method checks if the contents of the two operands are the same
- Everybody knows about String constant pool which is for efficient memory management in java. Basically most of objects are managed on heap area but String object. In most of ordinary application, programmers use String object quite often and this String object quite frequently need to be changed or it occupies large amounts of memory. Therefore instead of managing String object on heap area, they introduced String constant pool.
- One of important characteristic of String constant pool is that it doesn’t create same String object if there is already String constant in the pool.
For above two String objects, JVM creates only one object in the String constant pool and for the second string reference variable (var2), it points the string object which is created for var1. In this case, (var1 == var2) is true.
But one thing, people make confused is that. It works only when it encounter on String Literal with double quote.
String var1 ="This is String Literal";
String var2 ="This is String Literal";
In this case, a regular object will be created by new keyword on heap area and it will be placed in the String constant pool. Finally it will be assigned to the reference variable, var3. This process is just by passing from String constant pool management. Therefore, (var1 == var3) is false.
String var3 = new String("This is String Literal");
- indexOf() method searches within a string for a given character or String
Syntax : public int indexOf(int ch)
- public char charAt(int index) – used to extract a single character from given position
- public char to CharArray() – used to obtain an entire string in the form of character array
- public String substring(int beginindex, int endindex) – used to extract portions of a string starting from beginindex position to endindex position
- Strings in Java once created cannot be changed directly
- This is known as immutability in Strings
- To overcome this, Java provides StringBuffer class.
- StringBuffer is mutable and Thread Safe since all methods are synchronized.
- A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
- String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.
More StringBuffer Constructor and Methods
StringBuilder is also mutable but not Thread Safe because methods are not synchronized.
- A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.
- The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point.
More StringBuilder Constructor and Methods
- This class defines methods for basic numeric operations as well as geometric functions, All methods of this class are static
- The class is final and hence cannot be subclassed.
- Encapsulates the runtime environment
- Used for memory management and executing additional processes or external processes.
- Every Java program has a single instance of this class.
- We can determine memory allocation details by using totalMemory() and freeMemory() methods.
public static void main(String args) throws Exception
- Provides facilities such as standard input, output and error streams.
- Provides means to access properties associated with the Java runtime system.
- Fields of this class are in, out and err that represent the standard input, output and error respectively.
- The System class cannot be instantiated to create objects.
- Instance of this class encapsulates the run time state of an object in a running Java application.
- This allows us to retrieve information during runtime.
- There is no public constructor for Class.
- Object class is the superclass of all classes..
- Even, if user-defined class does not extend any other class, it extends Object by default.
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.
- Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
- If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
- It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.
- Returns a string representation of the object. In general, the toString method returns a string that “textually represents” this object.
- The result should be a concise but informative representation that is easy for a person to read.
- It is recommended that all subclasses override this method.