Home >>Java Tutorial >Java Strings

Java Strings

Java Strings

The Java strings are just like any other strings; these are just the sequence of characters that are basically used on a vast level in the Java programming. The strings are generally treated as the objects, in the Java programming language. One of the main uses of the strings that they are used to create and manipulate strings. The text is generally stored by the use of strings. And the variable of the strings or a string variable can be defines as the collection of characters that are surrounded by double quotes.

Creating the Strings in Java

The most phenomenal and undeviating way in order to create a string is to just write:

String greeting = "Hello phptpoint!".

Whenever the string generally gets an encounter with a string literal in the code that is written by the programmer then the compiler creates a String object that is with its own value in this case, "Hello phptpoint!'. The strings can also be created with any other object; programmers can generally create the string objects just by the use of the new keyword and a constructor. 11 constructors are contained by the string class that basically permits the programmer to provide the initial value of the string using different sources, like an array of the characters.

Here is an example that will demonstrate the creating of a string in Java:


public class StringDemoEx 
{
   public static void main(String args[]) {
      char[] helloArr = { 'h', 'e', 'l', 'l', 'o', '.' };
      String helloStr = new String(helloArr);  
      System.out.println( helloStr );
   }
}
Output :hello

Note : The String class is generally unchangeable in order to make sure that once it has been created then a String object cannot be changed. In case, there is a necessity to do a lot of modifications to strings of characters then only the programmer should use the string Buffer & the String Builder Classes in Java langauge.

String Length in Java

The accessor methods are basically the methods that are generally used to acquire information about an object. One that you can use with strings is the length() method, There is one accessor method that generally returns the number of characters that are contained in the string object and the fun fact is that one of the accessor method can be used by the programmer with the strings and that is length ().

Here is an example

public class StringDemoEx2 {
   public static void main(String args[]) {
      String str = "Welcome to the world of Java Tutorial";
      int length = str.length();
      System.out.println("Total String Length = " + length );
   }
}
Output :Total String Length = 7

Concatenating Strings

There is a method in the string class that is included in it just for linking the two strings or we can say concatenating the two strings.

Syntax

string1.concat(string2);

Writing the above code will generally return a new string that is string1 with string2 added to it at the end. There is another way of doing it like programmers can use the concat() method with string literals alternatively as depicted below:

"My name is ".concat("phptpoint");

In Java programming the strings are mostly linked (concatenated) with the + operator as depicted below:

"Hello," + " PHPTPOINT" + "!"

That will simply turn out to be like the result that has been depicted below:

"Hello, PHPTPOINT!"

Here is an example

public class StringDemoEx3
{
   public static void main(String args[]) 
   {
      String str1 = " Java  ";
      System.out.println("Welcome to the world of " + str1 + "Tutorial");
   }
}
Output :Welcome to the world of Java Tutorial

Creating Format Strings in Java

Creating Format Strings in Java in very easy task as the programmer have printf() and format() methods just to print the output with the formatted numbers. The String class also possesses an equivalent class method, format() that generally do the work of returning a String object rather than a PrintStream object. In order to create a formatted string that can be reused by the programmer instead of one-time print statement, String's static format() method is generally used in Java .

Here is an example of creating formats in strings that will explain you the working aspect of it:

public class StringDemoEx4{  
public static void main(String args[])
{  
String name="Phptpoint";  
String s1=String.format("Name = %s",name);  
String s2=String.format("value = %f",32.33434);  
String s3=String.format("value = %32.12f",32.33434);
  
System.out.println(s1);  
System.out.println(s2);  
System.out.println(s3);  
}}  
Output :
Name = Phptpoint
value=32.334340
value = 32.334340000000

Popular Tutorials