0% found this document useful (0 votes)
70 views2 pages

Strings

Strings are a sequence of characters that are objects in Java. The String class provides methods to create, manipulate, and work with strings. Strings can be created using string literals like "Hello" or by using the String constructor and passing a character array. The String class has methods to get the length of a string, concatenate strings by adding one to the end of another, and more.

Uploaded by

Praneeth Mutnuru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views2 pages

Strings

Strings are a sequence of characters that are objects in Java. The String class provides methods to create, manipulate, and work with strings. Strings can be created using string literals like "Hello" or by using the String constructor and passing a character array. The String class has methods to get the length of a string, concatenate strings by adding one to the end of another, and more.

Uploaded by

Praneeth Mutnuru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Strings

Strings, which are widely used in Java programming, are a sequence of characters. In the Java
programming language, strings are objects.

The Java platform provides the String class to create and manipulate strings.

Creating Strings:
The most direct way to create a string is to write:

String greeting = "Hello world!";

Whenever it encounters a string literal in your code, the compiler creates a String object with its
value in this case, "Hello world!'.

As with any other object, you can create String objects by using the new keyword and a
constructor. The String class has eleven constructors that allow you to provide the initial value of
the string using different sources, such as an array of characters.

public class StringDemo{

public static void main(String args[]){


char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};
String helloString = new String(helloArray);
System.out.println(helloString );
}
}

String Length:
publicclassStringDemo{

publicstaticvoid main(Stringargs[]){
String palindrome ="Dot saw I was Tod";
intlen=palindrome.length();
System.out.println("String Length is : "+len);
}
}

Concatenating Strings:
The String class includes a method for concatenating two strings:

string1.concat(string2);

This returns a new string that is string1 with string2 added to it at the end.
Let us look at the following example:

publicclassStringDemo{

publicstaticvoid main(Stringargs[]){
String string1 ="saw I was ";
System.out.println("Dot "+ string1 +"Tod");
}
}

You might also like