Casting

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

String handling

Java String contains an immutable sequence of Unicode characters. Java String is differ from string
in C or C++, where (in C or C++) string is simply an array of char. String class is encapsulated
under java.lang package.

Immutable class in Java


Immutable class means that once an object is created, we cannot change its content.
In Java String, Integer, Byte, Short, Float, Double and all other wrapper classes are immutable.

The Java String is immutable which means it cannot be changed. Whenever we change any string, a
new instance is created. For mutable strings, you can use StringBuffer and StringBuilder classes
Java String class provides a lot of methods to perform operations on string such as compare(),
concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc.
The java.lang.String class implements Serializable, Comparable and CharSequence interfaces.

CharSequence Interface
The CharSequence interface is used to represent the sequence of characters. String, StringBuffer and
StringBuilder classes implement it. It means, we can create strings in java by using these three
classes.

The Java String is immutable which means it cannot be changed. Whenever we change any string, a
new instance is created. For mutable strings, you can use StringBuffer and StringBuilder classes.
String s="welcome"; // String Literal

String s=new String("Welcome");//creates two objects and one reference variable

String s1="Welcome";
String s2="Welcome";//It doesn't create a new instance

Java String class methods


The java.lang.String class provides many useful methods to perform operations on sequence of char
values.
No. Method Description
returns char value for the particular
1 char charAt(int index)
index
2 int length() returns string length
3 static String format(String format, Object... args) returns a formatted string.
static String format(Locale l, String format, Object... returns formatted string with given
4
args) locale.
returns substring for given begin
5 String substring(int beginIndex)
index.
returns substring for given begin index
6 String substring(int beginIndex, int endIndex)
and end index.
returns true or false after matching the
7 boolean contains(CharSequence s)
sequence of char value.
static String join(CharSequence delimiter,
8 returns a joined string.
CharSequence... elements)
static String join(CharSequence delimiter, Iterable<?
9 returns a joined string.
extends CharSequence> elements)
checks the equality of string with the
10 boolean equals(Object another)
given object.
11 boolean isEmpty() checks if string is empty.
12 String concat(String str) concatenates the specified string.
replaces all occurrences of the
13 String replace(char old, char new)
specified char value.
replaces all occurrences of the
14 String replace(CharSequence old, CharSequence new)
specified CharSequence.
compares another string. It doesn't
15 static String equalsIgnoreCase(String another)
check case.
16 String[] split(String regex) returns a split string matching regex.
returns a split string matching regex
17 String[] split(String regex, int limit)
and limit.
18 String intern() returns an interned string.
19 int indexOf(int ch) returns the specified char value index.
returns the specified char value index
20 int indexOf(int ch, int fromIndex)
starting with given index.
21 int indexOf(String substring) returns the specified substring index.
returns the specified substring index
22 int indexOf(String substring, int fromIndex)
starting with given index.
23 String toLowerCase() returns a string in lowercase.
returns a string in lowercase using
24 String toLowerCase(Locale l)
specified locale.
25 String toUpperCase() returns a string in uppercase.
returns a string in uppercase using
26 String toUpperCase(Locale l)
specified locale.
removes beginning and ending spaces
27 String trim()
of this string.
converts given type into string. It is an
28 static String valueOf(int value)
overloaded method.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy