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

     6.1 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 :

  1. 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”.

          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.

          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);

 

String Class Methods :

Now here, we will look what are the methods available in String Class and what is use of these methods :

SrMethodDescription
1String concat(String str)concatenates the specified string.
2int length()returns string length
3char charAt(int index)returns char value for the particular index
4int compareTo(String s) 
5int compareToIgnoreCase(String s) 
6boolean equals(String s) 
7boolean equalsIgnoreCase(String s) 
8boolean startsWith(String s) 
9boolean endsWith(String s) 
10int indexOf(String s) 
11int lastIndexOf(String s) 
12String replace(char c1, char c2) 
13String subString(int i) 
14String subString(int i1, int i2) 
15String toUpperCase() 
16String toLowerCase() 
17String trim() 
18String[] 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 :

  1. By equeals() Method
  2. By == Operator
  3. 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 :

 

Immutability of Strings :

Help Others, Please Share