String and String Buffer Classes Assignment Help

Assignment Help: >> Built-in Classes - String and String Buffer Classes

String & String Buffer Classes

String is possibly the most generally used class in java's class library. An obvious reason for this is that strings are an extremely important category of programming.

The first thing to known about strings is in that every string you create is really an object of type String.  Even string constants are really string objects.  For instance, in the statement System.out.println("That is a string, too");

The string "This is a String, too" is a String constant. Although, Java handles String constants in the similar way in which other computer languages handle "normal" strings, so you do not have to worry about this.

The second thing to know about strings is which objects of type String are immutable; once a String object is created, its contents cannot be altered. Although this might seem just like a serious restriction, it is not to two reasons:

1.  If you require modifying a string, you could always create a new one which holds the modifications.

2.  Java declares a peer class of string, called String buffer that permits strings to be altered, then all of the normal string manipulations are yet available in Java.

A Strings can be constructed a variety of ways. The simplest is to use a statement like this:

String mystring = "this is a test"

At one you have created a string object; you could use it anywhere in which a string is allowed. For instance, this statement shows mystring:

System.out.println(mystring);

Java declares one operator for String objects: +. It is used to merge two strings. For instance, this statement

String myString = "I"+"like" +"Java."

Results in myString holding "I like Java"

The given program demonstrates the preceding concepts:

Demonstrating Systems

class StringDemo {

public static void main(String args[ ])

{

String s1 = "First String";

String s2 = "Second String";

String s3 = s1+ " and " + s2;

System.out.println(s1);

System.out.println(s2);

System.out.println(s3);

}

}

The output produced through this program is displays here:

First string

Second String

First string and Second String

The String class holds various functions which you can use. Here are a few. You could test two strings for equality through using equals (). You could obtain the length of a string through calling the length () method. You could obtain the character at a specified index inside a string through calling charAt(). The common forms of these three methods are displays here:

Boolean equals (String object)

int length()

char charAt(int index)

the given program that demonstrates these methods:

//Demonstrating some String methods.

class StringDemo2 {

public static void main(String a[])

{

String s1 = "First string";

String s2 = "Second string";

String s3 = s1;

System.out.println("Length of s9 : " + s9.length());

System.out.println("char at index 3 in s1 :" +s2.charAt(3));

if(s1.equals(s2))

System.out.println("s1 == s2");

else

System.out.println("s1 != s2");

if(s1.equals(s3))

System.out.println("s1 == s3");

Else

System.out.println("s1 != s3");

}

}

The output produced through this program is displays here:

Length of s1: 12

Char at index 3 in s1: s s1!=  s2

s1 == s3

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd