Java Constants and variables

Source: Internet
Author: User

Identifier:

Composition: 26 English letters case, number, 0-9, symbol: $,_

Rules:

1. Number can not start (1AB)

2. Cannot use keyword (class)

Java is strictly case sensitive

Some naming conventions in Java:

1. Package Name: Xxyyzz ...

2. Class Name/Interface name: Xxxyyyzzz ...

3. Variable name/function name: Xxyyyzzz ...

4. Constant Name: Xxx_yyy_zzz ...

Constant:

Several special representations of integer constants:

Octal: 0123 (83 in decimal)

Hex: 0x13 (0x13)

Binary

Attention:

Long shaping must end with L (l lowercase/lowercase): 9l,1238238238l

Single-precision float (float) ends with F: 3.6f,. 4f, 5.022e+23f

Variable:

Basic type:

Note: integer default: int, decimal default: Double

Basic Type conversions:

Example:

Class typeconversion1{public  static void Main (string[] args)  {           byte b=1;//judgment 1 This shaping constant is not within the -128~127 range, and is correct, Otherwise error           //b=b+2;//error!, at this time the data type of B is promoted to int, with 2 (the default int), and is int               //And therefore cannot be placed in a byte variable      b= (byte) (b+2);//a B + A value of 2 is cast to a byte assignment to B, which may lose precision (see below) that the         int i=393;//has already exceeded the byte range      System.out.println ((byte) i);//-119, a truncated detailed turn truncation occurred             

Long i2=1238989001234;//Although defined by long,
However, the default int range ( -2^31~2^31-1) is exceeded at this time, and an error is
Long i2=1238989001234l;

      System.out.println (' a ' + 1);//98, the character type is promoted to int (corresponding to the ASCII code)            int n=123456789;      Float F=n;//1.234567892e8, although the value of n does not exceed the numerical range of F,                //But the valid number of float is 7 bits, which means 92 of these two digits                //is not allowed        System.out.println (f);
     
Float f=1.2;//You can use a single-precision floating-point number 1.2f because the floating-point value defaults to double,1.2 to f at compile time with "possible loss of precision"
    }             }
Need to know: the data in the computer is stored in the form of twos complement, where the positive number of the original code, anti-code, the same complement.
          and the negative number of the inverse code is: The sign bit is unchanged, the remaining bits are reversed. Complement: Anti-code +1.

Conversion diagram between the basic types:
Attention:

A solid arrow indicates a conversion with no precision lost, and a virtual arrow indicates a conversion that may have a loss of precision

Conversion rules:

A numeric type conversion rule
numeric data, if the operand type is different or the operand type is lower than the int type, the
Automatic type conversion processing is performed, with the following specific rules:

1) If one of the two operands is of type double, the system first converts the value of the other operand to double
Type, and then perform the operation, otherwise

2) If one of the two operands is of type float, the system first converts the value of the other operand to float
Type, and then perform the operation, otherwise

3) If one of the two operands is a long type, the system first converts the value of the other operand to a long
Type, and then perform the operation, otherwise

4) The value of two operands will be converted to int type

5) If there is only one operand (for example, "~") and its type is lower than int, the value of the operand is converted to type int

Simply attributed:
1) byte,short,char->int->long->float->double
2) Byte,short,char will not convert to each other, the three of them will first be converted to int type when calculating

Note: In Java, you can assign a numeric constant of type int directly to the corresponding low-level Byte,short,char variable.
You do not need to force type conversions, as long as the value range of the variable is not exceeded.

Java Constants and variables

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.