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

String in Java

The document discusses Strings in Java. Some key points: - String is an immutable sequence of characters. The String class is in the java.lang package. - String objects are stored in the String Constant Pool in the heap. Methods like equals() compare character content rather than references. - Common String methods include length(), concat(), indexOf(), substring(), charAt(), and others. - Constructors include the default, parameterized with a String, array of chars, or bytes. - StringBuilder and StringBuffer are mutable alternatives to String that support modification methods.

Uploaded by

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

String in Java

The document discusses Strings in Java. Some key points: - String is an immutable sequence of characters. The String class is in the java.lang package. - String objects are stored in the String Constant Pool in the heap. Methods like equals() compare character content rather than references. - Common String methods include length(), concat(), indexOf(), substring(), charAt(), and others. - Constructors include the default, parameterized with a String, array of chars, or bytes. - StringBuilder and StringBuffer are mutable alternatives to String that support modification methods.

Uploaded by

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

-----------------------------------------------------------------------------------

----------------------------------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~STRING IN
JAVA~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-----------------------------------------------------------------------------------
----------------------------------------------------------------------

* String is a collection of charaters.


* In java srting is a class and a data type.
* the string class is present inside a {lang} package.
* The String objects are immutables.
* If we create a constant variable or value of string type, that variable store in
SCP (String Constant Pool) area.
* Whenever we are creating object of a string class, it get memory inside a heap
and also mentain a copy of a contain in SCP area.

Case 1-
String s=" "; //we are allocating a memory to the String
type variable
String s1=Null;//We are just declaring a String type
variable

Case 2-
String s ="ABC";
String s1="ABC";
sop(s==s1); //it always compare the
refence address of two variable.
sop(s.equals(s1)); //equals() - it is use to compare
the content of two different objects of a string type.

Case 3-
String s="Java";
s.concat("class");
sop(s)
o/p- Java // Becouse String type variable are
immutables.

Case 4-
String s1="Java";
String s2= new String("Java");
sop(s1==s2); //output- false
*refence is different.
sop(s1.equals(s2)); //output- True
*Content is same.

-----------------------------------------------------------------------------------
----------------------------------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~TYPES OF
CONSTRUCTOR~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-----------------------------------------------------------------------------------
----------------------------------------------------------------------

1. Default Constructor :
public String()
eg:
String obj=new string();

2. Parameterised Constructor :
public String(String)
eg:
String obj=new
String("Aman");

3. Passing array as a arguement :


public String(char[])
eg:
char[]
name={'J','A','V','A','C','L','A','S','S'};
String obj=new String(name);
sop(obj); //output- JAVACLASS

4. To make substring of a string :


public String(char[],int
state_index,int length);
eg:
String obj=new
String(name,4,5)
sop(obj) //output- CLASS

5. To convert a Byte Type array into a String :


public String (byte[])
eg:
byte[] name={65,66,67,68,69};
String obj=new String(name);
sop(obj); //output- ABCD

6. To Convert few byte type array into string :


public String(byte[],int
Start_index,int length)
eg:
String obj =new
String(name,1,3);
sop(obj); //output- BCD

>> STRING METHODS :


* length();
* concat(); //or "+" sysmbol
* startswith("string"); //if you want to check the givent string starts
from a given word or charater.
* endswith("string");
* charAT(<index>); //if you want to print a particular charater
from a string then use "charAT" method.
* trim(); //the trim method is used to remove a white space before a
string or after a string.
* toLowerCase();
* tUpperCase();
* substring(int start_index);
or
substring(int Start_index,int end_index);
* toCharArray(); to convert string into array;
* getChars(): //used to obtain set of characters from the sgring.
syntax : public void getChars(int start_index.int end_index.char[],int
offset);
eg :
String s="KAMAL";
char b[]=new char[10];
b[0]='N';
b[1]='E';
b[2]='E';
b[3]='L';
s.getChars(0,4,b,4);
System.out.print(b);
output : NEELKAMA
* equals() : used to compare two strings for equality Comparison is case-
sensitive.
Syntax : public boolean equals(object str)
* equalsIgnoreCase() : to perform a comparison that ignores case differences.
* compareTo() : it is used to compare two string in lexographical order.
//alphabet order.
//if strings are same o/p - 0
//if s1>s2 o/p - +ve else -ve.
eg :
String s1="ABC";
String s2="ABC";
65-65=0
* indexOf() : searches for the first occurrence of a character or substring.
* lastindexOf();
* replace() : it has 2 forms-
1. First replaces all occurrence of
one character in the invoking string with another character.
2. Bhool gye likhna .
* slpit() : syntex :
split("<Character or any sysmbol>") //jaise
python me krte the.

-----------------------------------------------------------------------------------
----------------------------------------------------------------------
Q :WAP where you have to take a string from User which contain number and character
in upper and lower case.
perform : 1. delete all vowels from string.
2. Insert "$" symbol before each and every consonent value.
3. Remove all the integer from the string.
4. Compare each and every word of the string (sort the
string).
5. Print the output in a lower case.

STRING BUILDER
STRING BUFFER

Both are mutable


String builder is a non sync. String buffer is a
sync.
String builder doesn't provide thread safety String Buffer provide
thread safety.

16+length of string

You might also like