Java Beginners - Java For Beginners Java String Rating: 5.0/5 (1 vote cast) | Level : Beginners Author : Arunkumar S Download Source : Not Avaliable
Java String - Creating StringsManipulating text is a common computer programming task. Several languages (perl, for instance) were purposely designed to include powerful text processing capabilities. Early Java versions were not designed with this in mind, so developers had to rely on third-party libraries to add decent string handling capabilities. But, with recent releases of the JDK, Java now has solid, built-in support for manipulating text through both regular expressions and enhanced string-related classes. In this tutorial, we'll take a look at Java's basic text processing class java.lang.String
Strings are Objects
Java uses primitives to represent basic data types, such as integers, floats, characters, and bytes, because primitives are very efficient for a computer to manipulate internally. For a programmer, though, primitives don't provide any benefit. Object-oriented programming emphasizes putting related data and behavior together into objects, but primitives can only hold data. In order to provide both an efficient and object-oriented string data type, the Java designers came up with a specially-handled class called java.lang.String.
Creating Strings
String enjoys a special shortcut syntax for instantiating new String objects from string literals: String myName = "Arunkumar Subramaniam "; The text between double quotes is a string literal. By assigning a string literal to myName, you can avoid using the new keyword. In fact, this special shortcut syntax was designed to improve String performance: Each JVM only keeps one copy of each string literal. The following code creates a single string literal that both String references point to: String literalOne = "I am a literal."; //points to the same object as literalOne String literalTwo = "I am a literal."; But, you can still use new to create a String object: String myName = new String("Arunkumar Subramaniam"); Note: You almost always want to initialize String references with literals to avoid creating unnecessary objects in the JVM. This can really add up if you are creating hundreds of identical Strings. You can also instantiate an empty String object by passing in just a pair of empty double quotes: If you don't know what a String object's value should be, but you need to create a reference, you can indicate this by assigning the null value; Java has a char primitive for representing single characters, and Strings are simply one or more characters. Not surprisingly, the language designers have made it is easy to convert a char[] to a String and vice versa. In this example, firstName, myName, and name all end up holding the text "Kevin": char[] firstName = {'A', 'r', 'u', 'n' }; String myName = new String(firstName); char[] name = myName.toCharArray(); Now that you know what a String is and how to create one, in Part 2, we'll take a look at common String operations. 1 | 2 | Next >
Discussion about this tutorial
 Start a new Discussion | Read All Discussion
|