Java Foundation--java Language Composition 01

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise naming convention

2. Identifier 2.1. Defined:

is the symbol used to play the role of logo;

(That is, the programmer's name for what he defines)

2.2. Naming rules (grammatical provisions, which must be followed):

1. can be uppercase and lowercase letters, numbers, underscores (_ ), dollar sign ($ ) composition;

2. numbers cannot begin;

3. keywords cannot be used;

4. strictly case-sensitive

Valid identifiers: Name; _abc; A, A;; Clasz;

Illegal identifier: 0nae;1name; class;

2.3. Writing Specifications:

Hump Method:

1, class name: The first letter capital, multiple words, each word capitalized, such as: Demo; xxxxyyyyzzzz;

2. Method name (function name): First letter lowercase, multiple words, starting with the second word, each word capitalized; for example: xxxxyyyyzzzz

3. Note 3.1. Defined:

Comments in life: Classical Chinese, English words;

The comment in Java is the text that interprets the code;

(for developers to see; the JVM does not look, so the content of the comment is not compiled when compiled)

3.2. Wording:

single-line comment ://annotated content until the end of the line

Multiline Comment :/* The contents of the comment */

Document Comment :/** The Annotated content */

The function is to allow the annotated content to be extracted by the Javadoc tool to generate a help document;

3.3. Function:

1, explain the code, improve the readability of the code, reduce the difficulty of development and maintenance;

2, the annotated content will not be compiled, can be used to troubleshoot errors, or adjust the function;

For beginners,

1, the thinking and analysis of the process with comments written to help clarify ideas;

2, develop a good habit of commenting, in the interview when useful;

4. Constant 4.1. Defined:

Is the data that is not changed after the definition;

4.2. Classification of constants in Java:

Numeric type:

Integer type: Positive, negative, 0;

Floating point type (number with decimal point): -3.4,0.0,7.8;

Text type:

Character type: a single character enclosed in English single quotation marks; ' 2 '; ' A ';

String type: 0 or more characters enclosed in English double quotation marks; “ ”; “ A ";" 123 ";" Hey! Beauty! ”;

Boolean: Only two values: true (True, yes), False (false, wrong);

Null value constant: null;

4.3. Representation of computer values (know):

The computer only knows the binary data;

4.3.1. Introduction to the System

Life is generally used in decimal counting, every ten into a, a digit, the maximum is 9;

The system is the rule of digital carry;

X into the system, is every x into a, a number for the largest number is x-1;

4.3.2. The origin of binary

The use of electrical signals in the computer to work, can accurately represent only high potential and low potential, equivalent to switch on and off;

A digit can only represent two digits, in the computer, the amount of data is called 1bit;

Typically 8 data bits are used to represent a number, called a byte:

8bit = 1byte; (B)

1024x768 byte = 1kb;

1024KB = 1MB;

1024MB = 1Gb;

1024Gb = 1Tb;

1024Tb = 1 Pb;

4.3.3. Eight binary and Hex

Octal and hex, solves the problem that binary digit writing is too long;

100:

Binary: Add prefix: 0b; 0b100;

Octal: Plus prefix: 0; 0100;

Decimal: Default no prefix;

Hex: plus prefix: 0x; 0x100;

4.3.4. Conversion to Binary

Arbitrary binary conversion to decimal: bit right expansion method;

Decimal conversion to any binary: short Division;

Quick Conversion Method:

4.3.5. Binary representation of negative numbers

4.4. Representation of computer characters (encoding table):

Only binary digits are recognized in the computer;

A

0

B

1

C

2

D

3

bbc:112

Abcd:0123

Establish a one by one correspondence between the characters and numbers to be represented, either at the bottom or through a digital representation;

The table that holds this correspondence is the encoding table;

Common Coding tables: ASCII;UNICODE;GB2312;BGK

......

5. Variable 5.1. The concept of variables

Data that can be changed;

In Java, a variable refers to a piece of space in memory that can hold constant data;

5.2. Data types for Java

Basic data type:

Numeric type:

Integer type:

byte: one byte;-128 ~ 127;

Short: two bytes: -2^15 ~ 2^15-1;

Int: four bytes: -2^31 ~ 2^31-1;

Long: Eight bytes: -2^63 ~ 2^63-1;

in the Java , integers are int by default type of;

Floating point type:

float: four bytes; -2^127 ~ 2^127;

Double: eight bytes; -2^1023 ~ 2^1023;

in the Java , floating-point numbers are double by default type of;

Character type (char): two bytes; 0 ~65535;

Boolean (Boolean): true false;

Reference data type: When the array is followed by; String is not a basic data type;

5.3. The writing format of the variable

Data type variable name;

Variable name = value;

Data type variable name = value;

Use variables, which are used by variable names;

5.4. Naming of variables

The variable name is the identifier; The naming convention is the same as the naming convention for identifiers;

The writing specification, in the Hump method, is the same as the writing specification for the function name:

The first letter is lowercase, and multiple words start with the second word and capitalize the first letter;

The naming of general identifiers has the best meaning;

5.5. Precautions for use of variables 5.5.1. Variables have a range of use

5.5.2. Before variables are used, assign values

5.5.3. Assigning a value to a variable needs to be consistent with the data type of the variable

5.6. Data type conversion 5.6.1. Automatic type conversion:

The data of the type with small value range will be placed in the variable of the type with large value range, and can be put in directly without loss of precision.

This is called the automatic type conversion of Java;

BYTE---------long------double;

Char-->int

5.6.2. Forcing type conversions:

The data of the type with large range will be placed in the variable of the type with small value range, which may not be put in, and the precision loss will occur.

If you have to put it inside, you need to use a method:

Small range of data type variable name = (small range of data type) variable name;

This is called a forced type conversion of Java;

Reason:

An interview question:

In Java, integers by default are int, to declare a long constant, you need to add a letter after the number: L (uppercase and lowercase, recommended capitalization, easy to identify)

In Java, floating-point numbers are double by default, and to declare a constant of type float, you need to add a letter after the number: F (can be uppercase and lowercase)

6. Operator 6.1. Arithmetic operators

is the symbol used to perform arithmetic operations on the data;

+ (plus);-(minus); * (multiply);/(except);% (modulo operation: equivalent to take-rest operation);

The two data involved in the operation, if the data type is inconsistent, the result is a large range of values of the type;

+ +: increment operator; unary operator, that is, only one data participates in the operation;

int a = 100;

Operator on the left side of the data of the operation, called Zuzi: ++a;

Zuzi, is to participate in the operation of the data itself on the addition of an operation;

operator to the right of the data of the operation, called Right self-increment: a++;

--: self-subtraction operator;

The self-decrement operator, like the principle and the increment operator, differs in that it is a subtraction operation;

The self-increment decrement operator exercises:

+: When there is a string on either side of the operator, it is a string connector that connects the data on both sides into a large string;

Practice:

Summary: The character + character is the corresponding ASCII on the numeric character + string string concatenation

6.2. Assignment operators

=: The variable that assigns the value to the right of the symbol to the left of the symbol;

+=:-=;*=;/=;%=;

The compound assignment operator, in fact, is a shorthand form;

A + = b <=> a = a+b;

You can automate coercion of type conversions:

6.3. Comparison operators

The result of operation is Boolean constant;

>;

>=;

<;

<=;

= =:

!=;

All basic types of data can be verified by the = = symbol for equality;

Note: Interval judgment, can not even continue to write;

1.1. Logical operators

& : With, equal to and meaning, indicates that the data on both sides of the operator is true, and the result is true;

&& : Short circuit and, calculation results and & is the same;

| : Or, the system of the or meaning, representing the data on both sides of the operator, one is true, and the result is true;

|| : Short circuit or, result of operation and | is the same;

^ : XOR, indicating that the data on both sides of the operator is not the same, the result is true, otherwise it is false;

The above operators are two-tuple operators, and the results of the two data and operations involved in the operation are Boolean data;

! : non; means the opposite; is a unary operator;

Logical operator Exercises:

Logical operation table:!true = false;!false = true;

Operator left

operator right

& (&& )

| (| | )

^

True

True

True

True

False

True

False

False

True

True

False

True

False

True

True

False

False

False

False

False

& and && Similarities and differences:

The same point: the result of the operation is the same, the logical operation relation of the expression is the same;

Different points:

&: Regardless of the value of the left and right sides of the symbol, both sides of the operation;

&&: If the left side of the symbol is false, the right side is not calculated, otherwise the right side is counted;

| and | | Similarities and differences:

The same point: the arithmetic rules are the same; the result of the operation is the same;

Different points:

|: regardless of the left and right sides, both sides have to calculate;

|| : If the left is true, the result must be true, do not calculate to the right, otherwise, the right will be calculated;

question: When to use & and | ?

If you must calculate on the right, use & and | ;

1.2. Ternary operators

Format:

Variable name = boolean expression? expression One : expression two;

Operation Process:

Note: Ternary operators must have a variable to receive the result of the operation;

1.3 Flow control statements

<<: Left-shift operator:

>>: Signed Right shift (arithmetic right shift), before and after the operation, the number of the sign does not change;

Summary: Signed right-shift operation, the left empty out of the original sign digits to be filled;

>>>: Unsigned Right shift (logical right SHIFT), right after the left empty out of all with 0;

&: bitwise AND;

^: Bitwise XOR OR

Application of the bitwise ^

Summarize

The above code first AB XOR or get to the value of C, and then when C with a XOR or get B then assign B to a and then C and B to a different or get a and then assign a value to B so that you can get a two value exchange.

Then there is a drawback is that you have to initialize the C, then in order to save memory space directly between the AB two values to calculate, less C operation steps

1. A and B XOR or acquire a C assigns C to a 2. A and B xor the equivalent of C and B or get the value of a and then assign a value to B 3.a and B is different or equivalent to C and a XOR or get B then assign a value to a

~: Bitwise Inverse:

Java Foundation--java Language Composition 01

Related Article

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.