Java Unit 3 (2025) PDF

Summary

This document appears to be part of a Java programming course focusing on strings and exceptions. It includes theoretical concepts and practical examples. The unit covers basic string operations, comparison methods, and the StringBuffer class along with introductory exception handling in Java.

Full Transcript

Unit 3 – Basic Concepts of Strings and Exceptions 3.1 Strings 3.1.1 Basic String operations, String Comparsion 3.1.2 String methods : (charAt(), concat(), equals(), indexOf(), isEmpty(), join(), lastIndexOf(), length(),split(), substring(),trim()) 3.1.3 StringBuffer class and its con...

Unit 3 – Basic Concepts of Strings and Exceptions 3.1 Strings 3.1.1 Basic String operations, String Comparsion 3.1.2 String methods : (charAt(), concat(), equals(), indexOf(), isEmpty(), join(), lastIndexOf(), length(),split(), substring(),trim()) 3.1.3 StringBuffer class and its constructors. 3.1.4 StringBuffer methods : (append(),insert(), delete(), reverse(),capacity()) 3.2 Introduction to Exceptions: 3.2.1 Exception Types, User defined Exception 3.2.2 Throw, Throws 3.2.3 Try, Catch and Finally String class. (Ch : 8 → P – 200) Strings are a sequence of characters. Java implements strings as objects of type string. When you create a String object, you are creating a string that can not be changed. Here, each time you need an altered version of an existing string, a new string object is created that contains the modifications. The original string is left unchanged. Using this approach fixed, immutable string object is implemented. The most direct way to create a string is to write: String greeting = “Hello World!”; There are different other ways to create string object. For that different constructors are available in the String class. o To create empty string i.e. string with no characters: String s=new String( ); o To create a string with range of characters: char c[ ]={‘a’,’b’,’c’,’d’,’e’,’f’}; String s=new String(c); → abcdef o To create a string with range of characters: char c[ ]={‘a’,’b’,’c’,’d’,’e’,’f’}; String s=new String(c,2,3); → cde o To create a string by using another string object: String s1=”Hello”; String s2=new String(s1); → Hello Dr. Darshna Rajput Page 1 Unit 3 – Basic Concepts of Strings and Exceptions o To create a string with array of bytes: byte b[ ]={65,66,67,68,69,70}; String s=new String(b); → abcdef o To create a string with range of byte array: byte b[ ]={65,66,67,68,69,70}; String s=new String(b,2,3); → cde CharSequence. CharSequence is an interface that represents a sequence of characters. Mutability is not enforced by this interface. Therefore, both mutable and immutable classes implement this interface. Of course, an interface can't be instantiated directly; it needs an implementation to instantiate a variable: CharSequence rcf = "Hello"; Here, rcf is instantiated with a String. Instantiating other implementations: 1) CharSequence c1 = new StringBuffer("Hello"); OR StringBuffer sb=new StringBuffer("Hello"); CharSequence c1=sb; 2) CharSequence c2 = new StringBuilder("Hello"); Methods of String class. (Ch : 8 → P – 203) SN Methods with Description Example 1 int length( ) String s=”Hello”; int l; Returns the length of this string. l=s.length( ); →5 2 char charAt(int index) String s1=”Hello”; Returns the character at the char ch; specified index. ch=s1.charAt(1); → e Dr. Darshna Rajput Page 2 Unit 3 – Basic Concepts of Strings and Exceptions 3 String concat(String str) String s1=”Darshna “; String s2=”Rajput”; Concatenates the specified string to String s3; the end of this string. s3=s1.concat(s2); → Darshna Rajput 4 boolean equals(String str) String s1=”ABC”; String s2=”abc”; Compares this string to another boolean b; string object. It considers the case of b=s1.equals(s2); → false strings. 5 int indexOf(int ch/char ch/ String s1=”Kabhi Khushi Kabhi Gam”; String str) System.out.println(s1.indexOf(‘K’)); → 0 Returns the index within this string System.out.println(s1.indexOf(97)) ; →1 of the first occurrence of the System.out.println(s1.indexOf(“Khushi”));→6 specified character. 6 int indexOf(int ch/char String s1=”Kabhi Khushi Kabhi Gam”; ch/String str, int startIndex) System.out.println(s1.indexOf(‘K’,5)); → 6 Returns the index within this string System.out.println(s1.indexOf(97,5)) ;→ 14 of the first occurrence of the System.out.println(s1.indexOf(“Kabhi”,5)); specified character, starting the → 13 search at the specified index. 7 int lastIndexOf(int ch/char ch/ String s1=”Kabhi Khushi Kabhi Gam”; String str) System.out.println(s1.lastIndexOf(‘K’)); → 13 Returns the index within this string System.out.println(s1.lastIndexOf(97));→ 20 of the last occurrence of the System.out.println(s1.lastIndexOf(“Khushi”)); specified character. → 6 8 int lastIndexOf(int ch/char ch/ String s1=”Kabhi Khushi Kabhi Gam”; String str, int startIndex) System.out.println(s1.lastIndexOf(‘K’,10)); →6 Dr. Darshna Rajput Page 3 Unit 3 – Basic Concepts of Strings and Exceptions Returns the index within this string System.out.println(s1.lastIndexOf(97,15)); of the last occurrence of the →14 specified character, searching System.out.println(s1.lastIndexOf(“Kabhi”,5); backward starting at the specified → 0 index. 9 String substring(int beginIndex) String s1=”This is a Test”; System.out.println(s1.substring(10)); → Test Returns a new string that is a substring of this string. 10 String substring(int beginIndex, String s1=”This is a Test”; int endIndex) System.out.println(s1.substring(5,9)); → is a Returns a new string that is a substring of this string. 11 String trim( ) String s1=” Hello “; Returns a copy of the string, with System.out.println(s1.trim( )); → Hello leading and trailing whitespace omitted. 12 boolean isEmpty() // non-empty string String s1 = "Hello"; Checks whether a String is empty or // empty string not. This method returns true if the String s2 = ""; given string is empty, else it returns false. System.out.println(s1.isEmpty()); → false System.out.println(s2.isEmpty()); → true The isEmpty() method of String class is included in java string since JDK 1.6. Dr. Darshna Rajput Page 4 Unit 3 – Basic Concepts of Strings and Exceptions 13 static String join String s1=String.join (CharSequence delimiter, ("-","Welcome","to","DUIAS"); CharSequence... elements) System.out.println(s1); Returns a string joined with a given ➔ Welcome-to-DUIAS delimiter. In the String join() method, the delimiter is copied for each element. The join() method is included in the Java string since JDK 1.8. 14 String[] split(String sep) String s1="I Love India"; String words[]=s1.split(" "); Splits this string against given //splits the string based on whitespace regular expression and returns a for(int i=0;i

Use Quizgecko on...
Browser
Browser