II. Basics of Java syntax

Source: Internet
Author: User

Here to mention a few personal feelings need to pay attention to the details, specific content recommended to read this Java book

First, identifiers

1. Naming rules for identifiers:
1) letters, underscores, dollar signs or numbers
2) letters, underscores, dollar signs (not numbers) (The interview likes to test these two points)
3) Case sensitive, unlimited length
4) cannot be duplicate with keyword or reserved word
2. The Convention generally uses the hump rules, and is known as the name
3. Note that the name can not have keywords all keywords, are lowercase letters

II. Basic data types

Byte,short, Int,long,char; boolean; float,double

It is important to note that:

1) integer type by default using int, float type default double

For example, to write a long length of the number, you need to explicitly write 1000L (after adding an L, although the lowercase l can also write, but the recommended capital L)

To write a floating-point number of type float, explicitly written as 1.23F

2) about the financial industry for data accuracy requirements, it is common to use BigInteger and BigDecimal to represent integer and floating-point

3) Char type

In Java, the bottom of the char is Unicode, English is two bytes 16 bits, a Chinese character is also two bytes, that is, ' a ', ' Medium ', for Char in Java is the same length
(although some encodings say a Chinese character is two English characters long, they are the same length and are two bytes below Unicode)

Three, operator

1. Assignment =, note that the reference type passes the address, and the base type passes the value.

2. Arithmetic operators: + 、-、 *,/,%, etc.

Note that an int is used at the bottom of the integer operation, such as

Short s1,s2 = 1;

s2++;

s1=s2;//here will be an error, because when s++, the short type will be automatically converted to int type, and then assigned to the left of the short type, the right to actually become an int type

3. Relational operators

Pay attention to the object comparison, can not use "=", and said "=" have to mention "equals", here to mention two points

1)

1  Public classA {}2 3  Public classTest {4 5      Public Static voidMain (string[] args) {6 7A A1 =NewA ();8 9A A2 =NewA ();Ten   OneSYSTEM.OUT.PRINTLN (a1 = = A2);//false A  -System.out.println (A1.equals (A2));//false -  the     } -  -}

The first one is false, because the comparison is actually an address, two objects, different addresses

The second is also false, you may already know the object comparison to use the Equals method, but here the A object does not override the Equals method, then the Equals method of object is called, and the Equals method of object is actually comparing the address  

2)

1  Public classTest {2 3      Public Static voidMain (string[] args) {4 5String S1 = "Hello";6 7String s2 = "Hello";8 9System.out.println (S1 = = s2);//trueTen  OneSystem.out.println (s1.equals (S2));//true A  -     } -  the}

Although string is an object type, it is very special, and Java has a string pool, When you create the first Hello, put in the string pool, the second time you create the S2, the JVM first goes to the string pool, found that there is already "hello", and directly let S2 also point to S1 this object.

If you want to create another "hello", you need to write this: new String ("Hello");

(about designing the string pool, one of the reasons is that Java is the first version, string is a constant, this is the JVM for efficiency considerations of an optimization, the later detailed introduction of string will also talk about Stringbuilder,stringbuffer, They are Java's second optimizations for string manipulation)

4. Logical operator &, | ,! Note two points

1)A&&b If the previous one is false, then the latter one will not execute; if a| | B, the first one is true, the second one is not executed;
A & B is false and the second one executes; a|b the first is true, and the second one executes. (This interview is very fond of exam)

2) If you are familiar with the binary operation

For example, the & 11 result is 01, 01 | 11 result is 11, here is calculated according to the principle of and or not, each position. In reality, there are few cases in which to do so. This is a bitwise operation.

3) Index Calculation

1.39e9f = 1.39*10^9 is used relatively little

4) Shift operation (requires a bit of binary knowledge to understand)

Shift left <<
Shift Right >>
Unsigned right shift >>> less, generally only in a multiple of 2 operation time with
And before the shift operation, Short,byte, etc., the system will automatically convert to int and then shift
One of the more recommended uses is when you zoom in to 2 of the N-side, you can write this t<<n, the left one is equal to 2, the right one is the equivalent of dividing by 2, this operation is faster (this is a weapon of b).

5) Ternary operator

A?b:c

If A is true then execution B,a is false to execute C

6) interception
Math.random () rounding
Math.ceil () L-section
Math.floor () cut down

7) automatic conversion between basic data types

Automatic conversion of small capacity into large capacity
Boolean and other types cannot be converted to each other (this differs from C + +, such as if (1) in C + +, not in Java)
Byte,short,char>int>long>float>double
Byte,short,char do not convert to each other, the three of them occur, first of all into int
(integer operation, the bottom is an int, so the small capacity of these three operations, before the operation, the system automatically into an int)

Iv. Control of flow statements

1. Cycle

While,do-while,for, note that the Do-while cycle is performed more often than while (this interview is a frequent test)

Like what:

1  Public classTest {2      Public Static voidMain (string[] args) {3         intI=0;4          while(I&LT;2) {//performed 2 times5i++;6System.out.println ("DD");7         }8         9         intJ=0;Ten          Do{//performed 3 times OneJ + +; ASystem.out.println ("DD"); -} while(j<2); -     } the}

2. Remember a principle, do not use if, else, more than 5 comments (for better maintenance)

3.switch statement, this remembers plus break control

Although the JDK version comes out fast version 9, but the use of switch and enumeration with the seemingly use of not common, see the following example:

1 enumSpe {2 Not,yes3 }4 5  Public classTest {6      Public Static voidMain (String[]args) {7Spe s = spe.not;//The value of the enumeration type, which is itself the enumeration type8         Switch(s) {//The position of S is put into an enumeration type9              CaseNOT:System.out.println ("not"); Break;Ten              CaseYES:SYSTEM.OUT.PRINTLN ("yes"); Break; One         } A     } -}

II. Basics of Java 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.