Skip to main content

String Class

String Class

String is a group of Characters. Thus, String class used to represent these group of characters.
String is a Final class i.e. no subclass will be created for this.
It implements interfaces like - Comparable, Serializable and charSequence.
From Comparable interface it implements compareTo() method and implements it.
Objects of String class are immutable, i.e. content inside them cannot be modified.
We can create string with new operator or with double quotes i.e. without new.
Eg.:
         String str = new String ("Hello");             //With new
         String str1 = "Java";                                 //Without new

1)   Creating object with new operator :-
  • A new String Object will be created every-time i.e. we can create multiple objects with same string class.
  • JVM will verify that String Literal is present in String Constant Pool or not.
  • If not,  then it creates new object inside the String Constant Pool.
  • If String Literal is available, then ignores that.
  • It also creates another new object inside the pool with new operator and assign its address to the String Reference.
               String str   = new String ("Hello");
               String str1 = new String ("Hello");
Here, str =! str1 as they have different addresses.

2)   Creating string without new operator :-
  • JVM will create an object of string class with content present in double quotes.
  • For that, JVM will verify that String Literal is present in String Constant Pool or not.
  • If not,  then it creates new object inside the String Constant Pool and newly created object address will be assigned to it.
  • If String Literal is available in pool, then address of existing object will be assigned to it.
  • With this, we can create only one object with same String Class.
           String s1 = "Java";
           String s2 = "Java";
Here, s1 == s2, as they both points to the same address.


Program to demonstrate String Class concept.


   class Strings{
          public static void main(String[] args) {
                   String s = "JAVA";
                   String str = new String("Hello");
                   String str1 = new String("Hello");
                   String s1 = "JAVA";
                   if(str==str1)
                             System.out.println("Strings are equal");
                   else
                             System.out.println("Strings are not equal");
                   if(s==s1)
                             System.out.println("Strings are equal");
                   else
                             System.out.println("Strings are not equal");
          }
}

Important Points - 
  • Object created with new operator will create in HEAP memory.
  • Object created without new operator will create in String Constant Pool inside the HEAP memory.
  • String Constant Pool is used to maintain the objects having common string values.
  • String methods can be applied on String Objects.


Comments

  1. It will clear the confusion about String concept, about what is actually happening internally.
    Contents presented beautifully.

    ReplyDelete

Post a Comment

Popular posts from this blog

Data Types

 Data Types     There are different types of data used by programmers in java like Numbers, Alphabets, Images etc. Computer does not know the difference between values like number or alphabets.Each type of data will be categorized into different types which are known as Datatypes in java. Thus, Datatype is a classification of data according to its type that an object or a variable can hold. It is a kind of data storage that contains specific type or range of value. Whenever variable is created a storage is assigned to it according to the datatype. Two main concepts of Datatypes are - Type of data. Size allocated to it. It is classified into two main categories i.e Primitives and Referenced .  There are 8 Primitive Type Datatypes and 4 Referenced Datatypes i.e Primitives Datatypes - These are built-in or predefined by the language and their definition cannot be changed. byte short char int long float double boolean Referenced Dat...

Literals

Literals Literal is the value that we pass in the variables. In other words, Literals are syntactic representation of boolean,numeric,character and string type. These values can be fixed or constant and are stored in the variable. Generally literal are categorized into two types i.e. Numeric Literals and Non Numeric Literals. Integer Literal : They includes - int, short, long, byte. These are the value without any decimal. Int is default type of integer. if any value is out of range in integer literal then compiler will consider that value as int and generates an error. To represent any value in long L/l will be used at the end. There are 4 integer Literals type i.e. Decimal, Octal, Hexadecimal, Binary(added in java 7)  Decimal Literals has base 10 i.e from 0 to 9.          They must not be started with 0. Example :          int a = 111;          it will print 111 Octal...

Operators

Operators Operators are used to perform some operations. Operators are special symbols that works with variables and constants in java. These symbols performs specific operation and gave result accordingly. Operators work with Operands. For Eg: a + b; Here a, b are Operands to which operation is to be performed,          + is Operator symbol. To work with operators 3 things must be noticed before using them i.e. Use of operator. Type of operand you are using. Result of operation. Unary Operators   : These type of operators required only one operand. For e:. +, -, ++, -- Binary Operators  : These type of operators required two operands to perform some operation. For eg: +, -, *, =, >, <, && etc. Ternary Operators :  These type of operators required three operands to perform some operation. For eg: :? Arithmetic Operators :           Arithmetic operato...