Jav Study Notes 2 built-in data types and three loops

Source: Internet
Author: User
1. built-in data type Learning

In Java, there are two types of data: one is the basic data type (also called the built-in data type) and the other is the reference type. There are eight built-in data types in Java: byte, short, Int, long, float, double, Char, and Boolean.

Byte: byte type, occupies the storage space of one byte. The data range is-128 ~ 127.

Short: Short integer, dual-byte, which occupies two bytes of storage space. The data range is [-327678, 32767].

INT: an integer. It is the most basic data type and occupies 4 bytes of storage space. The data range is [-2 ^ 31,2 ^ 31-1].

Long: long integer, which occupies 8 bytes of storage space. It is the most memory occupied by integer data.

Any of the above four data types has a unique feature. The data accessed through integer variables is accurate and there is no error. Another point is that the result of any operation between integers is still an integer.

There are two types of real numbers: float with single precision and double with double precision. The access to these types of data is inaccurate. In Java, the precision (that is, the number of decimal places) of the real number is controlled in the following format:

System. Out. printf ("% 10.5f", value); --> retain 5 decimal places.

CHAR: character type. The character type in Java belongs to the double byte type. That is, a character is saved by two bytes. Its Encoding table is unicode encoded and belongs to the Double Byte encoding. For a character, the encoding value must be considered. The encoding value ranges from 0 to 65535, that is, 65536 characters, including a large number of Chinese characters. The Unicode encoding range of Chinese characters is '\ u4e00'-'\ u9fa5 '. You can assign values to character types by encoding values. You can forcibly convert a character to the int type to know its unicode encoded value.

Eg: Char collate = 65; char CH2 = 'B'; char CH3 = 'hang'; syso (INT) ','); // The result is 44.

Application of escape characters:

Press Enter: '\ R' (I didn't know how to use this character. Test it)

The role is '\ R' to make the cursor return to the beginning of this line. If there is content to be output next to it, the content of the previous line will be overwritten.

Line feed: '\ n'; single quotation marks:' \ ''; double quotation marks: '\"'; bit table: '\ T) the string indicates the carriage return line break: "\ r \ n ".

Boolean: boolean type, which has two values: false and true, respectively, to indicate the true and false states.

Eg:

Int x = 10; int y = 10; Boolean B = x> Y & (x = y ++)> 5; system. out. println (x + ',' + Y); // The result is 65 system. out. println ("X, Y:" + x + ',' + Y); // The result is X, Y: 10, 11 // compare Boolean B = x> Y & (x = y ++)> 5; // The second expression does not calculate system. out. println (x + ',' + Y); // The result is 64system. out. println ("X, Y:" + x + ',' + Y); // The result is X, Y: 10, 10.

Comparison & differences:

& And & can both be used as operators of logic and, indicating logic and (and). When the expressions on both sides of the operator are true, the entire result is true, otherwise, if one of them is false, the result is false.

& It also has a short circuit function, that is, if the first expression is false, the second expression is no longer calculated. Just like the above example. When the expressions on both sides of the & operator are not of the boolean type, & indicates bitwise AND operation.

System. Out. println (x + ',' + Y); ', the' character is equivalent to converting it into a unicode value and addition operations are performed on X and Y. ',' Character Unicode value is 44.

2. Three cycles

1) while loop

In the while loop, the counter is assigned a value, the cycle condition is judged, and the counter is increased or decreased. The three expressions are not declared at the same position. The loop characteristic related to while is that the number of cycles is uncertain (the number of times is fixed but unknown ).

The while loop exits in two ways: Exit from the parentheses after the while; exit from the loop body.

Exit from the loop body, which is usually designed as a true loop.

Eg:

While (true) {sum + = start; Start ++; If (Start> end) {break; // The Break keyword is mainly used in the loop, used to exit the current layer loop} system. out. println ("1 + 2 + 3 + .... "+ end +" = "+ sum );

2) Do-while loop

Int sum = 0; int start = 1; do {// execute the loop body sum + = start ++;} while (start <= 100); system. out. println ("sum:" + sum );

There is only one difference between while and do-while. a while loop may not be executed once, while a do-while loop may be executed at least once. For the do-while loop, it must be executed at least once before judgment. Even if the first condition is false, a loop is executed.

3) For Loop

In a for loop, the counter is assigned a value, the cycle condition is judged, the counter is increased or decreased, and the three expressions are declared and defined at the same position. The characteristics of the for loop are basically determined by the number of cycles.

Eg: For (INT I = 0; I <= N; I ++)

General for loop execution process:

After the counter is initialized (only once), use the loop condition to determine the expression. If it is true, run the code in the loop body. After the execution, enter the added part of the counter, go to the judgment Expression to make a judgment. If it is true, the execution continues. If it is false, the loop ends. It repeats in sequence.

3. Several Typical Examples

1) define two integers to indicate the number of rows and the number of columns. Use the while loop to print out a rectangle with the character.

*****************

*****************

*****************

*****************

Use two while loops to control the outer and inner layers

Private Static void printrectangle (int m, int N) {int I = 0; // control row Int J = 0; // control column while (I <= m) {J = 0; // key: after each row of a loop, J starts from scratch while (j <= N) {system. out. print ("*"); j ++;} system. out. println (); I ++ ;}}

2) use the while loop to solve the "Fibonacci series" problem.

It is known that there are two values, f0 = 0, F1 = 1; then each value is equal to the sum of the first two values. The preceding numbers are:

0, 1, 2, 3, 5, 8, 13, 21 ,..... The first 40 items must be output.

Private Static void printfun2 () {// Fibonacci sequence problem int f0 = 0; int F1 = 1; int times = 19; int time = 1; system. out. printf ("%-10D", f0); // call the output format of C language to control the spacing between systems. out. printf ("%-10D", F1); While (time <= times) {system. out. printf ("%-10D", f0); system. out. printf ("%-10D", F1); f0 = f0 + F1; F1 = f0 + F1; time ++ ;}}

3) print the multiplication tips.

A) print the original multiplication tips

B) print the multiplication rules without repeated values

Private Static void printallmultiplication () {// complete multiplication formula {int x = 1, y = 1; while (x <= 9) {y = 1; while (Y <= 9) {system. out. print (x + "*" + Y + "=" + x * Y + "\ t"); // you must use the \ t hexadecimal character to control the line spacing. Y ++;} system. out. print ("\ n"); X ++ ;}} Private Static void printmultiplication () {// non-repeated multiplication table {int x = 1; // int y = 1; visible range of the variable (lifecycle) while (x <= 9) {int y = 1; while (Y <= x) {system. out. print (x + "*" + Y + "=" + x * Y + ""); y ++;} system. out. print ("\ n"); X ++ ;}}}

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.