What is a String in java ?

A string in java is a sequence of characters treated as a unit

Strings in Java

  • standard objects with built-in language support
    • String – class for immutable (read-only) strings
    • StringBuffer – class for mutable strings
  • can be converted to other types like integers and booleans
  • like other classes, String has constructors and methods
  • unlike other classes, String has two operators, + and += (used for concatenation)
  • Strings, once created, cannot be modified!!
    • However, we can carry out operations on a string and save it as another string

Basic String Operations

  • Constructors
    • public String()
      • construct a new String with the value “ ”
    • public String(String value)
      • construct a new String that is a copy of the specified String object value
  • Basic methods
    • Length() : return the length of string
    • charAt() : return the char at the specified position

Example

for(int i = 0; i < str.length(); i++)
counts[str.charAt(i)]++;

Leave a Comment