Strings in Java

Published on November 2016 | Categories: Documents | Downloads: 33 | Comments: 0 | Views: 192
of 14
Download PDF   Embed   Report

Comments

Content

Strings in Java

String





A string is commonly considered to be a sequence of
characters stored in memory and accessible as a unit.
Strings in Java are represented as objects.
Java considers a series of characters surrounded by
quotation marks to be a string literal or string constant.
For Example:

String s = "This is a string literal in Java "

How to create String objects?
 Two

ways

 String

str1 = new String("String named str2");

 String

str2 = "String named str1";

String Concatenation



Java supports string concatenation using the + operator
For Example:
String cat = “good";
System.out.println(cat + “morning");




In addition, it will convert many different types (int, float etc) to
objects of a String
For Example:

int myVar = 25;
“Var has a value " + myVar + " at this point.“
//So, it will be converted into string as
“Var has a value 25 at this point."

String Concatenation
 string

concatenation can also be achieved
by using the concat method of the String
class

String Object can’t be Modified
 Contents

of a String object cannot be
modified, a reference to a String object
can point to a different String object
 Sometimes this makes it appear that the
original String object is being modified.

For Example
class StringTest
{
public static void main(String[] args)
{
String str1 = "first string";
String str2 = "second string";
System.out.println("Display original string values");
System.out.println(str1);
System.out.println(str2);
System.out.println("Replace str1 with another string");
str1 = str1 + " " + str2;
System.out.println("Display new str1 string");
System.out.println(str1);
}//end main()
}//end class StringTest

Comparing Strings





While comparing strings, never use == operator. The
String class equals method should be used for the
comparison purposes.
== operator compares references only! (shallow
comparison)
It does not compare what is pointed to by the pointers

equals() method
 Default

implementation same as ==
 String class overrides to do a deep
comparison, i.e. comparison of characters.

Constructors and Methods of String class

split() method
 The

split ( ) method allows an application
to break a string into tokens.
 A token is a substring of the string.
 This method takes a delimiter (the
character that separate tokens) as an
argument.
 The delimiter is not included in the token
string.

Example

Converting Strings to Numeric primitive
Data Types

Array of String References
 For

Example:

String[ ] str1= new String[3];
str1[0]=new String(“this is first array element”);
str1[1]=“this is second array element”;
str1[2]=“this is third array element”;

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close