0% found this document useful (0 votes)
47 views6 pages

Java Strings

String defines and supports character strings in Java. Unlike other languages, strings are objects in Java rather than character arrays. The String class provides functionality for constructing strings, operating on strings through methods like length(), comparing strings, concatenating strings, and more. Strings are immutable in Java, so their contents cannot be changed after creation.

Uploaded by

Mahalakshmi
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)
47 views6 pages

Java Strings

String defines and supports character strings in Java. Unlike other languages, strings are objects in Java rather than character arrays. The String class provides functionality for constructing strings, operating on strings through methods like length(), comparing strings, concatenating strings, and more. Strings are immutable in Java, so their contents cannot be changed after creation.

Uploaded by

Mahalakshmi
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/ 6

Strings

String defines and supports character strings

In many other programming languages a string is an array of characters.

This is not the case with Java. In Java, strings are objects.

Constructing Strings

String str = new String("Hello");

String str = new String("Hello");


String str2 = new String(str);

String str = "Java strings are powerful.";

Operating on Strings

boolean equals(str)
int length( )
char charAt(index)
int compareTo(str)
int indexOf(str)
int lastIndexOf(str)

pgm

concatenate

String str1 = "One";


String str2 = "Two";
String str3 = "Three";
String str4 = str1 + str2 + str3;

Str4=”OneTwoThree”

Arrays of Strings 10000


A 250 hello
Strings Are Immutable

String A= “hello”
250
A =”HI”
HI
Using Command-Line Arguments
Reference means address of object or variable. Object is instance of class and instance means representative of
class i.e object

Instance is the actual object created at run time.

String supports a wide range of functionality


The String class is packaged in java.lang
it is automatically available to all programs.

String Fundamentals

Java a string is a sequence of characters,


Java implements strings as objects of type String
Implementing strings as objects allows Java to provide a full complement of features that make string handling
convenient.
The String class implements the following interfaces:
Comparable<String>,
CharSequence, and
Serializable.

The Comparable interface specifies how objects are compared.


CharSequence defines a set of methods that are applicable to a character sequence.
Serializable simply indicates that the state of a String can be saved and restored using Java’s serialization
mechanism.
Immutable
This approach is used because fixed, immutable strings can be implemented more efficiently than changeable
ones

the strings within objects of type String are immutable, we mean that the contents of a String instance cannot
be altered after it has been created. However, a variable declared as a String reference can be changed to refer
to a different String object.

The String Constructors.

To create an empty String, call the default constructor.

String str = new String();

The String class provides a variety of constructors to handle this task. For example, to create a
String initialized by an array of characters, use the constructor shown here:

String(char[ ] chrs)
You can specify a subrange of a character array as an initializer using the following constructor:

String(char[ ] chrs, int startIndex, int numChrs)

startIndex specifies the index at which the subrange begins, and numChrs specifies the number of characters to
use.

You can construct a String object that contains the same character sequence as another String object using this
constructor:

String(String strObj)

Pgm

String also provides several constructors that initialize a string when given a byte
array.

String(byte[ ] chrs)
String(byte[ ] chrs, int startIndex, int numChrs)

chrs specifies the array of bytes. The second form allows you to specify a subrange

The byte-to-character conversion is done by using the default character mapping.

The byte-array constructors can be especially useful when building a string from input provided by a byte
stream.

you can construct a String from a StringBuilder or a StringBuffer object

Three String-Related Language Features

Java supports three useful string features directly within the syntax of the language
These are the automatic creation of new String instances from string literals,
concatenation of multiple String objects by use of the + operator, and
the conversion of other data types to a string representation.

explicit methods available to perform all of these functions, but Java’s built-in support makes them more
convenient

String Literals

a string literal is created by specifying a quoted string

Java automatically constructs a String object. Thus, you can use a string literal to initialize a String object

String str = "this is an example string literal";


a String object is created for every string literal, you can use a string literal any place you can use a String
instance

void myMethod(String arg) { ...

myMethod("a string literal");


String Concatenation
Java does not allow operations on String objects through operators

One exception to this rule is the + operator, which concatenates two strings, producing a String object as the
result

String age = "19";


String str = "He is " + age + " years old.";
System.out.println(str);

Instead of letting long strings wrap around within your source code, you can
break them into smaller pieces, using the + to concatenate them.

String Concatenation with Other Data Types

You can concatenate strings with other types of data

int age = 19;


String str = "He is " + age + " years old.";
System.out.println(str);

This is because the int value in age is automatically converted into its string representation as a String object

This string is then concatenated as before. The compiler will convert an operand to its string equivalent
whenever the other operand of the + is an instance of String

String str = "four: " + 2 + 2;


System.out.println(str);

careful when you mix other types of operations with string concatenation expressions

String str = "four: " + (2 + 2);

Overriding toString( )

When Java converts an object into its string representation during concatenation, it does so by calling the
object’s toString( ) method
every class implements toString( ) because it is defined by Object. It has this general form

String toString( )

the Object class provides a default implementation of toString( ),


Pgm
Box’s toString( ) method is automatically invoked when a Box
object is used in a concatenation expression or in a call to println( ).

The length() Method


Strings aren’t arrays, they don’t have a length field. However, String does have a method called length( ) that
returns the length of a string.

int length( )

For example,
String str = "Theta";
System.out.println(str.length());

Obtaining the Characters within a String


The String class provides three ways in which characters can be obtained from a String object.
charAt( )
To obtain a single character from a String, you can use the charAt( ) method. It has this general form:

char charAt(int where)


where is the index of the character that you want to obtain. The value of where must be nonnegative and
specify a location within the string.
The charAt( ) method returns the character at the specified location

Pgm
getChars( )
If you need to obtain more than one character at a time, you can use the getChars( ) method. It has this general
form:

void getChars(int sourceStart, int sourceEnd, char[ ] target, int targetStart)

pgm
toCharArray( )

If you want to convert all the characters in a String object into a character array, the easiest way is to call
toCharArray( ). It returns an array of characters for the entire string. It has this general form:

char[ ] toCharArray( )

String str = "Programming is both art and science.";


char[] chrs = str.toCharArray();
System.out.println(chrs);

String Comparison

The String class includes a number of methods that compare strings or substrings within strings
equals( ) and equalsIgnoreCase( )

You might also like