What Is A Java String? - The Server Side
Maybe your like
Java string object example
In Java, a string is an object that represents a number of character values. Each letter in the string is a separate character value that makes up the Java string object. Characters in Java are represented by the char class. Users can write an array of char values that will mean the same thing as a string.
The following is an example of how character values make up the char array ch.
char[] ch = {'t','e','c','h','t','a','r','g','e','t'}; String s2 = new String (ch);This means the same thing as the example below.
String s2 = "techtarget";In the example, each letter of the string is represented by a character. Although strings created using the string class are immutable, strings created using combinations of characters can be changed. There are several utility string methods that can be used to alter strings, including split(), toLowerCase(), toUpperCase(), length() and valueOf(). These string methods can do the following:
- split strings at a specified point, such as white spaces;
- make the string lowercase or uppercase;
- give the string length; and
- provide the value of a given character in a string.
The following shows the use of valueOf()for determining the value of a letter in the above char array ch.
public class StringValueOfExample5 { public static void main(String[] args) { char[] ch = {'t','e','c','h','t','a','r','g','e','t'}; String s2 = new String (ch); String v = String.valueOf(ch[1]); System.out.println(v); } }The example above prints the value of the first position in the character array ch. The exact print statement is: e.
Utility methods like valueOf() and toLowerCase() are only used with strings that are immutable. They perform operations on strings, but they do not change the string itself. To create mutable strings that can be changed, the StringBuilder() and StringBuffer() classes are used.
Below are examples of string mutability using these classes.
String concatenation, which is the process of linking strings together, can be done using the append() method to print the sentence "Ben eats vegetables."
class Example { public static void main(String args[]) { StringBuilder x = new StringBuilder("Ben "); x.append("eats vegetables"); System.out.println(x); } }Reverse a string using the reverse() method to print "selbategev stae neB."
class Example{ public static void main (String args[]) { StringBuilder x = new StringBuilder ("Ben eats vegetables"); x.reverse(); System.out.println(x); } }Delete a specified portion of a string using the delete() method. This prints just the word "vegetables."
class Example { public static void main (String args[]) { StringBuilder x = new StringBuilder ("Ben eats vegetables"); sb.delete(1, 9); System.out.println(x) } }The classes and StringBuffer() are similar, however StringBuilder() is newer and not thread-safe, meaning it can't be used by multiple threads at once.
Java strings are a core component of the Java programming language and of software development in general. Learn about the Jenkins development platform and some possible alternatives for Java developers.
Tag » What Does This Mean In Java
-
What Is The Meaning Of "this" In Java? - Stack Overflow
-
Java This Keyword - W3Schools
-
This Keyword In Java: What Is & How To Use With Example - Guru99
-
What Does This Mean In Java? - Linux Hint
-
What Does % Mean In Java? - Quora
-
What Does :: Mean In Java? - Tech With Maddy
-
Java - Basic Operators - Tutorialspoint
-
Using The This Keyword (The Java™ Tutorials > Learning The Java ...
-
This Keyword In Java: Meaning & Use
-
What Does ! Mean In Java With Examples
-
What Is += Addition Assignment Operator In Java? - DigitalOcean
-
What Is A Java Object? - Definition From Techopedia
-
This Keyword In Java - Javatpoint