0% found this document useful (0 votes)
28 views

Strings

The document discusses strings in programming. It defines a string as a group of characters and explains that in Java a string is an object of the String class. It provides three ways to create strings in Java and lists some common String class methods like length(), charAt(), compareTo() etc. It also explains the difference between String and StringBuffer classes.

Uploaded by

poornasandur18
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)
28 views

Strings

The document discusses strings in programming. It defines a string as a group of characters and explains that in Java a string is an object of the String class. It provides three ways to create strings in Java and lists some common String class methods like length(), charAt(), compareTo() etc. It also explains the difference between String and StringBuffer classes.

Uploaded by

poornasandur18
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/ 3

Strings

1. Most of the data that transmit on internet is in the form of group of


characters.
2. such group of characters are called strings.

For exmple in business order form a person enters details like his name,
credit card number address etc which are nothing but strings

** A string is a group of characters.

In C/C++ a string represents an array of characters,where the last charcter


will be ‘\0’ charcter.
In Java, a string is an object of String class.

String s=”Java”;

Here s is a variable of data type ‘String’

IS String a class or data type?


String is a class in java.lang package. But in Java, all classes are also
considered as data types . So we can take String as a data type also.

Three ways to create strings:


1. String s; //declare a string type variable
s=”Hello”;//asssing a group of characters to it.

2. String s=new String(“HEllo”);

3. char arr[]={‘c’,’h’,’a’,’i’,’r’,’s’}
String s=new String(arr);
String class Methods:
1. String concat(String s)
String s3=s1.concat(s2);

2. int length(): This methdo returns the length or number of characters of a


string.
S1.length();

3. char charAt(int i) : this method returns the cahartacter tat te specified


location.

4. int compareTo(String s) : This method is useful to compare two string


and to know which string is bigger or smaller.

What is the difference between String and StringBuffer Classes?

String class objects are immutable and hence their contents cannot be
modified.

StringBuffer class are mutable and so they can be modified.

You might also like