String in JAVA --------------------------

2003sayanch 28 views 37 slides Sep 06, 2024
Slide 1
Slide 1 of 37
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37

About This Presentation

Educate yourself


Slide Content

String in JAVA

Characters “Building blocks” of non-numeric data ’a’, ’$’, ’4’ String Sequence of characters treated as single unit May include letters, digits, etc. Object of class String String name = “Frank N. Stein”;

Strings are most widely used object in java language. In java there are three classes that can process them and create them with nearly three similar methods are as follows: Class String Class StringBuffer Class StringBuilder All the three class are the part of java.lang package.

Overview of three string classes String str1 = “ abcd ”; (creating a string class object). ---(1) String: class String; str1: S tring object; “ abcd ”: sequence of character. Char s1[]= {‘ a’,’b’,’c’,’d ’};. -----(2) The above two declaration are equivalent. Using new operator also we can create the object of class String. StringBuffer bufstr = new StringBuffer (“ abcd ”);. ----(3) StringBuilder budstr = new StringBuilder (“ abcd ”);

Storage of the string The object of class string have a special storage facility which is not available to the other two classes. We know the memory allocated to the java program is divided into two segments. Stack Heap Variables are stored in heap while programs are stored in stack. Heap have a memory segment called “String constant pool”.

The declaration of type 1 will going to store in string constant pool. Where as declaration of type 2 will be stored in the memory segment of the heap. No duplicate string can be created in the String constant pool . String stry = “ abcd ”; String strx =“ abcd ” .

s trx and stry both referenced to a same string. == (true only if the strings are at the same address, i.e., same string ). Method equals (true if the strings are identical)

Questions 1. 2. 3.

4.

Immutability The string object created by class String are immutable. “Immutable”: Once an object is created its value or content cannot be changed. Implying waste of memory, as the old and new string will reside in the memory. The object created by the class of StringBuffer are mutable and are stored in the heap segment of the memory.

The methods of StringBuffer are synchronized and hence they are thread safe. The overburden of thread safety makes them heavy and lower the program execution speed. The object of class StringBuilder are mutable but not thread safe. The operation are fast as compared to StringBuffer and there will be no memory loss as in the case of String class.

Constructor of class string The string class is final and we cannot extend this class. There are different number of constructor of class string through which the strings can be created. String(): Create a string without any character. String(byte[] barray ): Construct a new string by decoding the specified byte array. String(byte[] barray , Charset specifiedset ): Construct a new string by decoding the specified byte array using specified character set. The supported charsets are UTF8, UTF 16, UTF 32, and ASCII byte[] bray = new byte[]{65,66,67,68}; String str2= new String(bray, “ ascii ”);

String(byte[] barray , int offset, int length, String charsetName ): Constructor construct a string object by decoding the specified part of byte array using specified character set . byte[] bray = new byte[]{65,66,67,68}; String str2= new String(bray, 1,3, “ ascii ”); Throw U nsupportedEncodingException or IndexOutofBoundsException . String (char[] value): Constructor construct a string based on the specified char array. Char cr []= {‘A’,’B’,’C’,’D’}; String str = new String( cr ) String (char [], int offset, int count ): Constructor construct a string with character from a part of character array, starting from the offset up to given length. String str = new String( cr , 0,2) Return AB; Throws IndexOutofBoundsException

String(String “ originalstring ”): Constructor construc t a string with string a s specified in the argument. String str2= new String(“Let us learn java”); String( StringBuffer str ): Constructor construct a string contains character in the string buffer. StringBuffer cr = new StringBuffer {“Hello”}; String str = new String( cr ) String( StringBuilder str ): Constructor construct a string contains character in the string buffer . StringBuilder cr = new StringBuilder {“ Hello”}; String str = new String( cr )

Questions on different functions of String 1.

2.

3.

Some more Functions b yte[] getBytes (): Encode a given string into a sequence of bytes and returns an array of bytes. c har toCharArray (): Converts the string into an array of characters and returns the array. Int compareToIgnoreCase (): Compare the string by the ignoring the case difference. startsWith (String str ): Returns true if a string starts with the given substring. endsWith (String str ): Returns true if a string ends with the given substring.

4.

5.

6.

7 .

8.

9.

10

Exception thrown by string class IndexOutOfBoundException :- UnsupportedEncodingException PatternSyntaxException NullPointerException Class StringBuffer The compiler allocate extra capacity for 16 more characters so that small modification don't involve reallocation of string.

11.

12.
Tags