Javase_ Basic Syntax

Source: Internet
Author: User
Tags logical operators throw exception

First, Java overview

1, Java language Features: pure object-oriented (all objects), platform-Independent (JVM shielding the difference between the underlying platform), different platforms have different JVM,JVM to translate the program into the current operating system can execute the program, a compilation run everywhere), robust (pointer, memory application and release impact robust).

2, Java Two core mechanism: JVM,GC, here is the daemon thread execution.

3, the main function can guarantee the independent operation of the class, it is the entry of the program, called by the JVM.

4. Java source file with. Java as the extension, a source file can have only one public class, and the class name must match the file name.

5, the Java Development Environment installation, path configuration, Javac command to compile java files, Java command to the generated class file execution.

Conversion of identifiers, keywords, constants, variables, basic data types

1, identifier: (class name, method name, variable name, etc. can be named place), alphanumeric _$, cannot start with a number, cannot be the same as the keyword.

2, the keyword: lowercase English, the compiler will be marked in a special way.

3, constants: Shaping the constant 123, the implementation of the constant 1.2, character constant ' a ', the logical constant false,true, the string constant "Wangwei", the null constant, The term constant is used in another context to represent the immutable variable final modification.

4, variables: three elements (variable name, type, scope), in fact, is a small area of memory, you must first apply, and then assign the value, in order to use, or compile the exception.

5, the execution process of the program: first. java files are compiled by the Java compiler into a. Class bytecode file, and the runtime ClassLoader loads the. class file from the hard drive into memory, then through the bytecode checker, the last interpreter to the main method to begin execution. The memory management in the process is as follows: The heap puts the new thing (heap has the default value), the stack puts the local variable, the data segment in the static variable and the string constant, the code segment in codes.

6, the class can not have any code, this shows that Java is purely object-oriented.

7. Variable classification: Local variables (method body), member variables (methods in-body, in-Class) or basic data type variables (4 classes 8, Byte, char, short, int, long, float, double, Boolean), reference data type changed (class, interface, arrays).

8. Basic data type: (with fixed expression range and field length, not affected by the platform, to ensure its portability)

1. Boolean: The Boolean value can only be true and false. Cannot be null, cannot use 0 and not 0 for true and false, generally used in process control.

2. Character type: the Java characters are Unicode encoded (globally unified encoding, universal code including global characters), each character occupies 2 bytes (that is, Unicode is represented by 22 bytes, up to 65,525), can be used in the form of 16 binary encoding example: Char c= ' \u0061 '; , UTF-8 is one of the implementations of Unicode, a variable-length character encoding with 1-byte letters and 3 bytes for Chinese characters.

*char c= ' ab ';//error, char c= "a";//Error

* To escape characters, Java uses ' \ ' to convert subsequent characters to other meanings.

Example: String s0= "ad\" as ", s2=" A\RB ";//Carriage return, \t//tab,s4=" A\R\NB ";//Line, window is often used.

3. Integer type, available in 8, 10, 16 binary notation, 8 binary starts with 0, 16 binary starts with 0x or 0X, Java shaping constant defaults to int.

But note: Byte b=12;//correct short s=12;//correct long l =999999999;//99999999 out of int range wrong, need to add L.

4. Floating-point type, floating-point type default is double, example: float f=1.2//compile error float f=1.2f//correct

9, the conversion of the basic data type, multiple types of mixed operations, the capacity of small automatic conversion to large capacity (automatic promotion), the data type by capacity size is as follows:

Byte,short,char->int->long->float->double;byte,short,char do not convert between them, the three of them will first be converted to int when calculating.

10, implicit conversion and casting problems, for example:

1.byte B = 1 + 1; Correct, two constants added in byte range will not error

2.byte B = 1;   b = B + 1; Error, B will automatically promote to int, and then +1 or int, assign a value to byte error

3.byte B = 1;     b + = 1;   Correct, implicit type conversion, equivalent to b= (byte) (b+1); Note b= (byte) b+1; it will be an error.

4.byte B = 1;    b = ++b; Correct, implicit type conversion, equivalent to b= (byte) (b+1);

Third, operator

1. Arithmetic operator (+,-, *,/,++,--,%) 1/100==0;//only the integer part; 1%100=1;//modulo operation

2, the assignment operator (=,+=,-=,*=,/=,%=) a=b=c=5;//can

3, the relational operator (==,>=,<=,!=,<,>) operation result is a Boolean, "= =" by the wrong write "="

4, logical operators (&&,!, | |,& (Short circuit and), | (short circuit OR), ^ (XOR, different sides, true;)), to concatenate an expression of type Boolean

5, String Join operation Method (+) operation on both sides of a string, the result is a string

6, bitwise operator (<<,>>,>>>,|,&,^,~), directly to the binary operation &,|,^, both the logical operator and the bitwise operator

Bit operator instances:

1. The most efficient way to figure out 2 times 8 equals a few?  2<<3; Left n is multiplied by 2 n times, right opposite

2. Exchanging two integers does not require a third-party variable

int n=2; M=1;

N=n^m;m=n^m;n=n^m; Can be used for cryptographic decryption

3. For a number (2 binary or 10 binary) of the 16 binary? (Implementation of the mutual conversion code between the binaries)

int k=num&15;k>>>4; Four-bit loop execution via &15 and then right shift

7. Ternary operator (conditional expression)? Expression 1: Expression 2;

Iv. statements

1, if statement, three formats if;if else;if ElseIf else. Each format is a single statement, and the conditional expression only depends on whether the result of the operation is true or false.

2. if (conditional expression) {.} else{.} structure Shorthand: variable = (conditional expression)? Expression 1: expression 2; You can simplify the notation, but because it is an operator, you must have the result of the operation.

3. Note the difference between the following statements int i=3; 1.if (i>1) {..} else if (i>2) {.. Do not execute} 2.if (i>1) {:} if (i>2) {: Execute}

4. Switch statement. The default in switch can be placed anywhere, but is finally executed, unless break, for Byte,char,short,int, 5.0 can judge the enumeration, 7.0 can judge the string.

* Use if when judging the interval and the result is Boolean.

5. Circular statements

The 1.for and while are interchangeable, the only difference being that the memory dwell time of the loop increment is different.

2.for (;;) while (true)

3.break must be used in switch and loop, for example to write Berak in the IF statement to compile errors.

4.break,continue,return,throw exception object, below cannot write anything, otherwise compile error.

5.break.contiue is used in the inner and outer loops (named loop), q:for (int i=0;i<10;i++) {c:while (i==0) {break q;}}

Javase_ Basic Syntax

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.