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

String

Uploaded by

kewatkuldeep558
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

String

Uploaded by

kewatkuldeep558
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

String

String is Not a Datatype but String is a class defined in


java.lang package which stores multiple characters of
any type with in double quotes " ".

String is a special type of class for which there is not


need to create String Constructor as any data with in
double quotes is treated as String Object.
(Creating Constructor unnecessary doubles memory
space)

String is not compared using == as == operator does not


compare String content but compare memory space as
under:-
In above diagram it is showing true because a and b refer
to same memory.

Now if we use boolean equals(Object) method of String


class instead of == it will return true in all 3 cases as it will
now compare String Contents, hence it is preferable to
use String -> equals method to compare two Strings as
under:-
Case 1:
String x = new String(“amit”);
String y = new String(“amit”);
If(x.equals(y))
{
True
}
else{
False
}
Now it will be true.

Case 2:
String a = “amit”;
String b = “amit”;
If(a.equals(b))
{
True // it will go to true
}
else{
False }
Case 3:
String m = “amit”;
String n = new String(“amit”);
If(m.equals(n))
{
True // go to true
}
else{
False
}

In java, string objects are immutable. Immutable simply


means unmodifiable or unchangeable. Once string object
is created its data or state can't be changed but a new
string object is created.

Default input of Java is in Format of String and We use


Wrapper classes to Convert String to Primitive Type of
Data.

import java.io.*;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“enter..”); // message to take input
String x = br.readLine(); // input from user in format of String
Wrapper classes are helper classes for each Data type
as under
Datatype Wrapper class method to convert String
byte Byte byte parseByte(String)
short Short short parseShort(String)
int Integer int parseInt(String)
long Long long parseLong(String)
char Character Can’t convert String to char
float Float float parseFloat(String)
double Double double parseDouble(String)
boolean Boolean boolean parseBoolean(String)

Example:

String x = “123”; // only numeric data can be converted


int i = Integer.parseInt(x);
float f = Float.parseFloat(x);
StringTokenizer

StringTokenizer is a class defined in java.util package


which is used to break String into multiple tokens using
identifiers. Identifiers can be multiple, space is also used
as identifier.

import java.util.*;
String names = “ram, shyam. Sita, Geeta”;
StringTokenizer str = new StringTokenizer(names,”,.”);
while(str.hasMoreTokens())
{
s.o.p(str.nextToken());
}

You might also like