In a + b + c, If variable a is a string : String class « Utility Classes « SCJP






The Java compiler treats the previous code as if it were the following:

new StringBuffer().append(a).append(b).append(c).toString();

class MyClass {
  private int a;

  private int b;

  private int c;

  MyClass(int a, int b, int c) {
    this.a = a;
    this.b = b;
    this.c = c;
  }

  public String toString() {
    return "a = " + a + ", b = " + b + ", c = " + c;
  }
}

public class MainClass {
  public static void main(String[] argv) {
    MyClass t = new MyClass(11, 13, 48);
    System.out.println("Here it is: " + t);

  }
}
Here it is: a = 11, b = 13, c = 48








8.7.String class
8.7.1.String in Java
8.7.2.The + Operator and StringBuffer
8.7.3.The overloaded + and += operators perform functions similar to the concat() method
8.7.4.Working with Strings
8.7.5.The most common string constructor takes another string as its input
8.7.6.An easier abbreviation string construction could be
8.7.7.With strings, the equals() method checks the two contained collections of characters.
8.7.8.Use == to compare String literal
8.7.9.toUpperCase() converts all the characters of a string to uppercase.
8.7.10.Original string is not modified after calling toUpperCase()
8.7.11.char charAt(int index) Returns the indexed character of a string, where the index of the initial character is 0.
8.7.12.String concat(String addThis) adds two strings together
8.7.13.int compareTo(String otherString) Performs a lexical comparison
8.7.14.boolean endsWith(String suffix) Returns true if the current string ends with suffix; otherwise returns false.
8.7.15.boolean equals(Object ob) Returns true if ob instanceof String and the ob and the executing object have the same value.
8.7.16.boolean equalsIgnoreCase(String s) Returns true if s matches the current string, ignoring rcase.
8.7.17.int indexOf(int ch) Returns the index within the current string of the first occurrence of ch.
8.7.18.int lastIndexOf(int ch) Returns the index within the current string of the last occurrence of ch.
8.7.19.int length() Returns the number of characters in the current string.
8.7.20.String replace(char oldChar, char newChar) replaces every occurrence of oldChar with newChar.
8.7.21.boolean startsWith(String prefix) Returns true if the current string begins with prefix;
8.7.22.String substring(int startIndex) Returns the substring, beginning at startIndex of the current string and extending to the end of the current string.
8.7.23.String toLowerCase() Converts the executing object to lowercase and returns a new string.
8.7.24.String toString() Returns the executing object (not a copy).
8.7.25.String trim() Returns the string that results from removing whitespace characters from the beginning and ending of the current string.
8.7.26.At compile time, if either operand of a + operator is a String object, then the compiler recognizes that it is in a string context.
8.7.27.In a + b + c, If variable a is a string