Basic control flow of Java basic series

Source: Internet
Author: User

Data Type

In Java, char is a 16-bit character. Java provides another data type-byte, which is the same as char in C, to maintain cross-platform (Unicode encoding) settings, second, Java provides a fixed font length for each data type for cross-platform purposes. In addition, compared with the C language, Java provides the boolean data type, the Boolean value occupies one storage space in Java (compared with C ++, it uses one byte storage ). Another data type in Java is short ---- 16 bits. Note that there is no unsigned data type in Java, that is, bye is-128----127, and the rest will be deduced accordingly. String is frequently used in Java, but it is not a basic data type, but an object.

Passing Java references OR values

In Java, only values are passed. The so-called reference transfer is a feature in C ++. The basic data type of Java is directly copied and transferred during parameter passing. Therefore, changing the parameter in the main method will not affect the original value. For objects, because all objects in Java are on the heap, there will only be references (or a restricted pointer) in the stack pointing to the objects in the heap, so, when passing a reference (or object), like passing a pointer to a data in C, any modification will be reflected in the passed object. Sometimes this is really annoying! Therefore, both C and Java have only one parameter transfer method-value transfer.

Operators supported by Java

In addition to the four arithmetic operations, there are also a series of %, --, ++, + =, ternary operators, string connection +, = features, logical operators, bit operators, type conversion.

The priority and value assignment are the same as those in the C language. Of course, the auto-increment and auto-increment operators have the same features as those in the C language. Java supports short circuits -----In the Judgment statement, if the current condition is sufficient to judge whether it is true or false, the subsequent condition judgment will not be executed, and the efficient Java program will be optimized at this point.

The second is the string Join Operation "+". For this operator, I suggest using less in the program! Because the Java String is a constant, every time you execute the + String connection operation, a New String object will be created, so if the program uses a large number of + (for example, in for) to connect strings, it will lead to lower program efficiency, and in some cases, it also causes security problems, such as SQL injection attacks.

In floating-point numbers, when scientific notation is used, a special "e" usually exists. Of course, if we do not usually deal with numeric computing, we will consider this as e in mathematics. However, remember that e here is the representative of 10. That is to say, 1.33e12f represents 1.22*10 ^ 12, so if you encounter it later, never get it.

If you have not learned any related courses, you may feel unfamiliar with the shift operation. However, even the advanced Java language provides the shift operation, it is enough to explain the importance of the shift operation (in fact, you will not use it most of the time )! Here we will introduce several related terms: high position, low position, original code, back code, complement code, and comparison, XOR, reverse retrieval, etc. Recently, the logic and composition principles of the above numbers are described, in fact, this efficient operator can sometimes be used in some algorithms.

Type conversion: Java allows interchange between basic types. When performing a narrow conversion, there will inevitably be a corresponding truncation. For example, if you convert a double to an int, the values following the decimal point will be directly rounded off, you can write your own algorithms or use the round () method in Math (do not duplicate the wheel ).

In addition, in basic type operations, as long as it is smaller than int (such as char and byte), it will be automatically upgraded to int, while in double and float operations, float will also be automatically upgraded to double. Of course, it is better for computation for computers. When performing a numeric operation, the maximum numeric type determines the type of the result. You need to understand this because, sometimes when it involves a numerical operation, you will find many type conversion problems! Therefore, be sure to remind yourself when performing numeric operations-is there any overflow? Is precision lost ?......

Java Control statements

If (the same as else in C), while, break, continue, return, common for, and comma operator (the only use is in the for statement) is not described.

Foreach Syntax: it is a more concise and easy-to-read method introduced by Java SE5 for Traversing arrays and containers. It is said that Foreche is more efficient! In Java programming ideology, the Foreach syntax is used whenever possible ". Only classes that implement the Iterator interface in the container can use the Foreach syntax. The Foreach syntax of the array is supported by Java and does not require any other programming requirements.

Public classForeachTest {

Public static voidMain (String [] args ){

Int[] Test = {1, 2, 3, 4, 5, 6, 7, 8, 9 };

For(IntI: test ){

System.Out. Println (I );

}

}

}

The preceding is the foreach syntax rule. Each element of test is assigned to I in sequence. Is this simple? Of course, if counting is involved, it is natural for foreach of the set to be discussed later.

The switch statement separately states that, in many cases, values not int and char are put into the switch (byte, char, int can be put, but not long ), at this time, the compilation will not pass. Java SE1.5 provides a new type-enumeration, which can also be used as the switch judgment object. switch and enumeration are actually quite good technologies. Another big surprise is that String can also be used in the switch in ipve7, but the technical difficulty behind this is actually quite large.

Note that when I first learned the C language, I used a lot of goto statements in a program, but later I found some problems with the program, but I found that the process of the program is very messy. Even if there is only one thread, I can look at it again and find that the conciseness of the C language I love is completely damaged by me, even if sometimes goto can bring good efficiency or process operations, it also needs to weigh the many disadvantages caused by the use of goto. So many reasons are that Java retains the usage of goto, although goto is not directly used, the syntax of tag is enough to represent goto.

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.