Core Java Learning Note--2. Basic data Types & type conversions

Source: Internet
Author: User

  • data Type (8 basic types: Int/short/long/byte/float/double/char/boolean)
    • Integral type
      Int 4 bytes -2 147 483 648~2 147 483 647
      Short 2 bytes -32 768~32 767
      Long 8 bytes -9 223 372 036 854 775 808~9 223 372 036 854 775 807
      Byte 1 bytes -128~127

        • The Long value is appended with the suffix L (e.g). 4000000000L)
        • Binary number (BIN): prefix 0b (e.g. 0b10012=910)
        • Octal number (OCT): prefix 0 (e.g. 0108=810)
        • Hex (hex): prefix 0x (e.g. 0xcafe16=5196610)
        • In Java, the scope of an integer is independent of the machine running Java code, which guarantees the portability of Java. In c, the integer bytes in the 16/32/64 bit system are different: short (2Byte) <=int (2/4byte) <=long (4/8byte)
        • After Java 7, you can add "_" between numbers to add readability (the compiler ignores underscores) (e.g. 2_000_000)
        • Java does not have unsigned type (unsigned)

      • Floating-point types
        Float 4 bytes Valid digital 6~7 bit
        Double 8 bytes Valid digits 15 bits

        • Float type value plus suffix f (e.g.  3.14F) A floating-point value with no suffix f (e.g). 3.14) default to double type

    • Char type
      • The char data type is a unit of code that uses UTF-16 encoding to represent Unicode code points. (Details about UTF-16, Unicode code points, code units are described in the next section)
      • The char type is used to represent a single character and is typically used to denote character constants. (e.g. ' A ' is the character constant encoded as 65, and "A" is a string containing the character ' a ')
      • Escape sequence Character
        \b Backspace
        \ t Tab
        \ n Line break
        \ r Enter
        \” Double quotes
        \’ Single quotation marks
        \\ Back slash
      • I see myself dizzy, the next section in-depth discussion of the previous existence of Char in Java

      • Boolean type
        • The Boolean type (Boolean) has two values: false and True, which are used to determine the logical condition. integer values and Boolean values cannot be converted to and from each other (in general).
        • In C, the value 0 is equal to False in Boolean, or True if the value is not 0. Thus, in the code below, C, because the value of the expression "X=0" is 0, the result is judged to be false. In Java, it is not possible to compile because the value of the expression "x=0" cannot be converted to a Boolean value. (in more than one sentence, in C + + programming, in the judgment statement, the "x==0" is rewritten as "0==x" can be a good solution sometimes missing a ' = ' problem )
          if 0)

    • Data type conversions
      • A solid arrow in the diagram indicates a loss-free conversion, and a dashed arrow indicates a conversion that may have a loss of precision. (e.g. 123 456 789 (int) is a large integer that contains more bits than can be expressed by the float type (a valid number 6~7 bit), and when this integer value is converted to float, the result is the same size, But the loss of a certain degree of accuracy (after the number of decimals can not be accurately expressed))
      • Binary operation (with two operands, e.g. N+F) Automatic type conversion priority: Double>float>long>int(if any), convert to higher priority, or to int at least

    • Forcing data type conversions (CAST)
      • Possible loss of precision
      • In a forced type conversion, a floating-point value is converted to an integer by truncating the decimal part , and the variable NX has a value of 9.
      double x = 9.997; int NX = (int) x;
      • If you want to round up floating-point numbers , use the Math.Round method , at which time the variable NX value is 10. In addition, Math.Round () returns the long type of the result, so it is also necessary to cast the type to int.
      double x = 9.997; int NX = (int) Math.Round (x);
    • When a numeric value is converted from one type to another, and the target type's representation range (Long->int) is exceeded, it is truncated to an entirely different value (e.g. (byte) 300–> 44)

Core Java Learning Note--2. Basic data Types & type conversions

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.