String Length, Characters, and Combining Strings

The String class provides the methods for obtaining length, retrieving individual characters, and concatenating strings.
java.lang.String

The String class contains the methods for getting string length, individual characters and combing strings.

You can get the length of a string by invoking its length() method.

You can retrieve a specific character from a string by using charAt(index :  int) method.

Note : -   A string value is represented using a private array variable internally. The array cannot be accessed outside of the String class. The String class provides many public methods, such as length() and charAt(index), to retrieve the array information. This is a good example of encapsulation: the data field of the class is hidden from the user through the private modifier and thus the user cannot directly manipulate it. If the array were not private the user would be able change the string content by modifying the array. Thus would violate the tenet that the String class is immutable.

You can use the concat method to concatenate two strings. The statement below, for example click here, concatenates strings str1 and str2 into str3:
String str3=str1.concat(str2);

Popular posts from this blog

Shutdown Pc

Ellipse using OpenGl

String Comparisons