Fecha de registro: 12 abr 2022

String in Java

In java String is an object which presents sequence of characters. String is immutable (cannot make changes in the object). The Java platform provides the String class to create and manipulate strings (java.lang.string).


Creation of string objects :

By Literals

In this method value is assigned to the variables.

For example :

String str = “Student learning point”;

Whenever it encounters a string literal in the code, the compiler creates a String object with its value, in this case, “Student learning point”.

By new keyword :

According to homework answers websites string can also be created using new keyword and a constructor. The String class has eleven constructors which allows you to provide the initial value of the string using different sources, such as an array of characters.

Methods of String handling :

• length( )

According to this method is used to obtain the number of characters in the string object.

• Char charAt(int index) :

This method returns the character present at the index specified.

• String concat(String str) :

This method combines one String to the end of another. The method returns a String with the value of the String passed in to the method appended to the end of the String used to invoke this method.

• boolean equals(Object anObject) :

This method compares the string to the specified object in the argument. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

• String replace(char oldChar, char newChar) :

This method returns a newly formed string resulted from replacing all occurrences of oldChar in this string with te newChar.

• String Buffer :

When the content will change frequently then it is never recommended to use string because for every change we will have to create a new string object. Therefore we should use string buffer. String Buffer is mutable.

StringBuffer sb = new StringBuffer(“Student learning point”);

• Extending the java.lang.Thread Class

Steps of thread creation :

1. Extend the java.lang.Thread Class. 2. Override the run( ) method in the subclass from the Thread class to define the code executed by the thread. 3. Create an instance of this subclass. This subclass may call a Thread class constructor by subclass constructor. 4. Invoke the start( ) method on the instance of the class to make the thread eligible for running

More resources:

Java Tutorial: Abstract Class

Tokens in Java Java Tutorial: Data Types Java Tutorial Exception Handling

Más opciones