Basic composition of Java---03Java language (original, please do not reprint)

Source: Internet
Author: User

1. Keywords
    • Keywords are given special meanings in Java, such as public void class, and all the keywords in the Java language are lowercase . But this sentence can not be reversed.

2. Identifiers
    • Identifiers are actually some of the names that are customized in the program.
    • Identifiers consist of English letters, numbers, and symbols "_" and "$", and cannot use spaces . Be aware that:
      • Numbers cannot be used as the beginning of identifiers
      • You cannot use keywords to define identifiers
      • The name of any variable is an identifier. such as Main,haha, etc.

3. Notes
    • Comments in Java have single-line comments and multiple lines of comments.
      • Single-line comment: Starting with double slash "//" for use in our
      • Multiline Comment: Starts with/* and ends with */
    • Java-specific document-type annotations can be extracted from the tool Javadoc in Java to generate a manual.
    • Attention:
      • Multiple lines of comments can no longer be nested in a multi-line comment , because at compile time will be extra a "* *"
      • You can use multiline comments to find the range of code errors and ultimately determine the location of the error. That is , the comment can be debugged by the program
      • comments are not compiled into a class file during compilation , which can be seen by comparing the size of the class file before and after compilation
      • A canonical Java file is preceded by a multi-line comment before the code class to illustrate the meaning and thought of the code in the Java file. The specific format is as follows
/* Requirements: (that is, what the code is trying to solve--what is the problem?): (Writing the core of the code, figuring out where the problem begins--how to do It) steps: (Major programming steps, including variables and methods to use) */

Class Demo
{
......
}

4. Constants--data that cannot be changed
    • Categories: integer constants, Decimal constants, Boolean constants, character constants, string constants, null constants
    • Attention
      • "and" are not equal to null, such strings or characters are called null characters (strings). Null cannot have single or double quotation marks

5. Variables
    • a storage area in memory, and the zone has its own name (variable name) and type (data type), and the region's data can be constantly changed within the same type range.
    • After a variable is defined, you cannot define a variable of the same data type and name.
    • Format
      • Variable name of data type = initialization value;
    • Pay ATTENTION!!!
      • In Java, integers are given the default int when assigned to a variable, and if the range of the integer is greater than the range of type int, it is declared by adding the lowercase letter "L" to the integer, that is, the large number is identified by the character "L".
      • Similarly, because the decimal in Java default is a double-precision type, if you want to save a decimal to a single-precision float type, you need to add the letter "F" after the decimal, declaring that the number is a single-precision floating-point data.
6. Data type (base data type and reference data type)
    • Basic data type:
      • Numeric type:
1. Integer Type: Byte (1 bytes, -127~128), short (2 bytes), Int (4 bytes default), Long (8 bytes, an oversized integer to represent the character L) ps:1). In order to facilitate the operation, the integer is divided into different degrees, the memory space can be effectively configured. 2). 1 bytes occupy 8 bit bits such as 0000-0000
The representation of 6 in memory, such as int:                               0000-0000 0000-0000 0000-0000 0000-0110-6 (positive 6 first, plus 1)                         take counter  1111-1111 1111-1111 1111-1111 1111-1001                            + 0000-0000 0000-0000 0000-0000 0000-0001__________________________________________________ ___________________                              1111-1111 1111-1111 1111-1111 1111-1010     (note: The binary highest bit of a negative number is 1)                  
2. Floating-point type: single-precision float (4 bytesA total of 32 bit bits, followed by a declaration of F), the default is double-precision doubles (8 bytes)
      • Character type: char (the value range is within 2 bytes , the number of integers that can be loaded in quotation marks)
      • Boolean Type: Boolean
    • Reference data type:
      • Classes: Class
      • Excuse: interface
      • Array: []

  

7. Type Promotion & Casting
    • The above describes the different types of data and their binary representation in computer memory and the amount of memory they occupy. Automatic type promotion and coercion of type conversions occur when the data types on both ends of the equality assignment operation are inconsistent.
    • Here are some examples to illustrate the two separately:
1). First, start with a simple operation,

  

Class vardemo2{public     static void Main (string[] args)     {          int x = 3;          x = x + 5;   //Take 3 out of the memory space, in the arithmetic area binary operation 3+5  namely 011+101 = 1000, and then assign the result of the operation to X, at this time the data in the X memory space is 8          SYSTEM.OUT.PRINTN (x);     }}

The above code in memory of the specific operation process is like this

 

PS: The operands must be of the same type in order to operate (inconsistent types, inconsistent memory space). However, the number between numeric types can be calculated.

2). Automatic type Promotion
Class vardemo2{public     static void Main (string[] args)     {          int x = 3;          byte B = 5;          x = x + b;   Automatic type Promotion          SYSTEM.OUT.PRINTN (x);}     }
/*
integers of type int consume 4 bytes in memory, or 32 bit bits, while integers of type byte use only 1 bytes in memory, or 8 bit bits.

*/

  

3). Forcing type conversions
Class vardemo2{public     static void Main (string[] args)     {          byte b = 5;          b = (byte) (b + 3);  Coercion type conversion: casts an int to byte type (minus the high-level binary data, leaving only 8 bit bits to participate in the operation), and then copying to byte-type variable          System.out.println (b);     }}
/*
The idea is the same as the automatic type promotion above, except that the 3 of the large int of the right of the equation is forced to remove the high position (the first 3 bytes) in the binary, and then the operation.
*/

7. Constants and variables
Class vardemo2{public     static void Main (string[] args)     {          System.out.println (' a ' + 1);   Char in memory is 2 bits, int in memory is 4 bits, in the case of no cast, Java will automatically convert the number of bits smaller automatically into a large number of types, and then the operation, 1 corresponding binary automatic type conversion           System.out.print (( Char) (' a ' + 1));  After the output type is specified, the result  98 is forced to convert the type to  a smaller char type     }}//run get  >>> 98            >>> b

PS: The correspondence between the binary and the words in life such as letters----> Coding table

01100001---> A 01100002---> B ...---> ...

Class vardemo{public     static void Main (string[] args)     {     byte b = 4;     BYTE B1 = 3;     byte b2 = 7;     B = B1 + b2;   Here is an error, because the right side of the equation is a variable, the variable is indeterminate, the Java compiler cannot check whether the right of the equal sign is worth more than Byte ( -128~127), such as the definition of B1, and 127 is assigned to B1, will lose precision. That is, the right is not sure, is unable to determine the assignment of     //b = 3 + 7;  3  7  is constant, no error     }}

  

Class vardemo{public     static void Main (string[] args)     {     int x;     int x1 = 898;     int x2 = 810;     x = B1 + b2; Because the result of the calculation in Java defaults to int, but when  the value of X1 + x2 is outside the range of int, Java will cast, forcing the binary of the INT 4 8 bits, resulting in the truncation of the highest 1, resulting in a negative number     }}

  

  

Basic composition of Java---03Java language (original, please do not reprint)

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.