10. String and Operators.

 String






















Strings in Java are a crucial aspect of programming, as they allow for the manipulation and handling of text. Here’s a detailed explanation of strings, their characteristics, and how they can be created and used in Java.

Characteristics of Strings in Java

  1. String Class:

    • String is a predefined class in Java, derived from the java.Lang package.
    • It is a non-primitive data type because it is a class.
    • A String is a collection of characters enclosed within double quotes (" ").

  2. Immutability:

    • Strings in Java are immutable, meaning once a String object is created, its value cannot be changed.

Creating Strings in Java

There are two main ways to create a string in Java:

       1. Using String Literals:

  • When a string is created using string literals, it is stored in the String Constant Pool (SCP)                                                                                                                        
  • Example - String a = "Hello" ;
      2. Using the new Keyword:
  • When a string is created using the new keyword, it is stored in the heap area            
  • Example - String b = new String("Hello") ;

Different Classes for Creating Strings

  • The String class is used to create immutable strings.

In Java, we can create and manipulate strings using three different classes:

  1. String:

  2. String Buffer:

    • The String Buffer class is used to create mutable strings (strings that can be changed).
    • It is synchronized, meaning it is thread safe.

  3. StringBuilder:

    • The StringBuilder class is also used to create mutable strings.
    • It is not synchronized, meaning it is not thread-safe but offers better performance in a single-threaded environment.

Example of string 
           class Example
                  {
                          public static void main(String[] args)
                                  {
                                          String a = "Hello";          // Will print Hello
                                          String b = "H";              // H
                                          String c = 10;            // Incompatible datatype, will cause an error
                                          String d = true;          // Incompatible datatype, will cause an error
                                          String e = 'A';           // Incompatible datatype, will cause an error
                                          String f = 1.1;           // Incompatible datatype, will cause an error

                                          String g = "Hello" + 1;      // This will concatenate

                                          System.out.println(a);       // Output: Hello
                                          System.out.println(b);       // Output: H
                                          System.out.println(g);       // Output: Hello1
                                  }
                    }

Operators  

Operators are symbols that are used to perform operations on variables and values (operands). They are a fundamental part of any programming language.

Operators are of two types -

1) Based on type of Operation it Perform 

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Conditional Operator
  • Compound Assignment Operator
  • Increment and Decrement Operator
  • Miscellaneous Operator
2) Based on Number Of Operands

  • Unary Operators
  • Binary Operators
  • Ternary Operators

Comments