Java | String :
~~ CORE-JAVA ~~
1. INTORDUCTION
1.1 Java Intorduction
1.2 Java history
1.3 Java features
1.4 Diff b/w Java & C++
1.5 Java Variables
1.6 Java Data Types
1.7 Typecasting
1.8 Arrays
2. OOPS
2.1 Introduction
2.2 Class
2.3 Object
2.4 Static Keywords
2.5 Constructors
2.6 This KeyWord
3. INHERITANCE
3.1 Inheritance (IS-A)
3.2 Aggregation (HAS-A)
4. POLYMORPHISM
4.1 Polymorphism & Its Type
4.2 Method Overloading
4.3 Method Overriding
4.4 Super Keyword
4.5 Final Keyword
4.6 Dynamic Binding
5. ABSTRACTION
5.1 Abstract Classes & Methods
5.2 Interfaces
5.3 Abstract vs Interface
6. ENCAPSULATION
7. STRING MANIPULATIONS
7.1 String
7.2 String Buffer
7.3 String Tokenizer
8. PACKAGES
8.1 Predefined packages
8.2 Userdefined Packages
8.3 Access Specifiers
9. EXCEPTION HANDLING
9.1 Introduction
9.2 Pre Defined Exceptions
9.3 Try-Catch-Finally
9.4 Throws, throw
9.5 User Defined Exception
10. MULTITHREADING
10.1 Introduction
10.2 Thread Creations
10.3 Thread Life Cycle
10.4 Life Cycle Methods
10.5 Synchronization
10.6 Wait() notify() notify all()
11. WRAPPERCLASSES
11.1 Introduction
11.2 Wrapper Classes
12. COLLECTION FRAME WORK
12.1 Introduction
12.2 List interface
12.3 Set interface 12.4 Map interface
13. INNER CLASSES
13.1 Introduction
13.2 Member inner class
13.3 Static inner class
13.4 Local inner class
13.5 Anonymous inner class
14. CoreJava Interview Questions
Introduction :
If we talk about normally What is “String”, then we can say It’s group of Character. But in Java, String is a class which is available in java.lang package, which provides all necessary methods to work with String.
Creating String :
In Java there are 3 ways to create String in, these are below :
By String literal :
- we can create String, just by assigning to group of character as below :
String s = "Hello";
- In this case, JVM creates an object and stores the string “Hello” in that object. This object will be refer by variable “s”.
- we can create String, just by assigning to group of character as below :
2. By using new Keyword :
- We can create an object of String Class, by using new keyword also, as below :
String s = new String("Hello");
- Here we are doing, 2 things. First we are creating an object using new keyword. Then we are storing “Hello” into that Object.
- We can create an object of String Class, by using new keyword also, as below :
3. By converting char type array into String :
- Third way is to creating String is, to converting an char type array.
char arr[] = {'h','e','l','l','o'}; String s = new String(arr);
- Third way is to creating String is, to converting an char type array.
String Class Methods :
Now here, we will look what are the methods available in String Class and what is use of these methods :
Sr | Method | Description |
1 | String concat(String str) | concatenates the specified string. |
2 | int length() | returns string length |
3 | char charAt(int index) | returns char value for the particular index |
4 | int compareTo(String s) | |
5 | int compareToIgnoreCase(String s) | |
6 | boolean equals(String s) | |
7 | boolean equalsIgnoreCase(String s) | |
8 | boolean startsWith(String s) | |
9 | boolean endsWith(String s) | |
10 | int indexOf(String s) | |
11 | int lastIndexOf(String s) | |
12 | String replace(char c1, char c2) | |
13 | String subString(int i) | |
14 | String subString(int i1, int i2) | |
15 | String toUpperCase() | |
16 | String toLowerCase() | |
17 | String trim() | |
18 | String[] split(delimiter ) |
String Comparison :
In Java, we can compare String based on it’s content or it’s reference. We have below 3 methods to compare :
- By equeals() Method
- By == Operator
- By compareTo() Method
1. By equals() Method :
2. By == Operator :
3. By compareTo() Method :
String Constant Pool :
String Constant Pool, is a separate block of memory where the String objects are hold or store by JVM. If a String object is created directly, using assignment operator (String s = “hello”), then it is stored in String Constant pool.
- Each time you create a string literal using assignment operator, the JVM checks the “String Constant Pool” first.
- If the string already exists in the pool, a reference to the pooled instance is returned.
- If the string doesn’t exist in the pool, a new string instance is created and placed in the pool.
Example :