No.5 traps in an expression

Source: Internet
Author: User

1. Traps About strings

  • JVM handling of strings
    • string java = new string (" Java ");   How many objects are created?
      • of 2. A string object corresponding to the "Java" direct amount; The string object returned by the new string () constructor
      • java.intern ()   Can be a direct volume object in a string pool. The
    • string and the basic type of wrapper class, Java allows the creation of a corresponding Java object by a direct amount, in addition to a simple arithmetic expression, a connection calculation (which can be determined at compile time) or a corresponding Java object can be created directly
    • string literals, the JVM will be saved with a string pool, the first time the direct amount is used, it will be placed in the string pool for caching (the cache meaning is to be used later, directly from the pool, but not new), in general, the string in the cache pool will not be garbage collected
    • as long as it is compile to determine the string of its specific value, the JVM will be at compile time to determine its specific value, and let it point to the string pool corresponding to the string, otherwise, the JVM's string pool can not be exploited
      • What is the specific value at compile time?
        • no method call; no variable reference
        • of course, "macro substitution" except for the variable
    • string str = "Hello" + "world";   created a few string objects
      • 1. Since the computed value can be determined at compile time, it is good to put the computed value directly into the pool, and the rest is not necessary.
    • When using string, basic type wrapper class object instance, try to create an instance by   direct amount. You can create fewer objects to improve performance.
  • String is a typical immutable class. Variable string recommended use StringBuilder
  • string comparison
    • The underlying is a string array
    • Two strings are left-aligned, and each character of the two is compared in turn

2. Traps for expression types

An expression is also a data type that has a set

    • Automatic elevation of an expression type
      • When an expression contains more than one base data type, the data type of the entire expression is automatically promoted to the same type as the highest-level operand in the expression.
      • The rank of operands is listed

    • Traps for compound assignment operators (+ =,-=,/= 、... )
      • E1 op= E2 equivalent to E1 = (type of E1) (E1 op E2)
      • Eg:a + = 5 equivalent to a = (type of a) (A + 5)
      • Trap: Its potential implicit conversions may result in high "truncation" of the computed result (when the result cast overflows)

No.5 traps in an expression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.