20145326 Java programming Second week of study summary

Source: Internet
Author: User

20145326 "Java program design" the second week to summarize the learning content of learning materials

This week in the third chapter of the study, this chapter mainly describes the Java language of some basic syntax, Java is a support object-oriented programming language, but before formally entering the object-oriented support syntax, for the type, variables, operators, process Control, and so on, the various programming languages have basic syntax elements, Still need to have a certain foundation. Although the various programming languages have these basic grammatical elements, do not neglect them because the various programming languages have their own purpose and evolutionary process, and the basic grammatical elements will have their own unique characteristics.

1. Type

In the world of Java, not everything is abstracted into objects, you still have to face some of the features of the system. For example, you still have to be aware of the problem of limited memory length, so the program executes when encountering 123 of this integer, or to think about how much length of memory to store it relatively economical. Basically, Java can be differentiated into two types of systems, the base type and the class type, where class types are also referred to as reference types.

Basic types (each type has its storage range, such as out-of-range, i.e. overflow):

The so-called basic type, is in use, you have to consider how much memory length of the data to save the economy, using program syntax to tell the JVM, and then by the JVM automatically for you in memory configuration and management. The basic types in Java can be distinguished mainly by integers, bytes, floating-point numbers, characters, and Booleans.

    1. Integers: can be divided into short integers (accounting for 2 bytes), int integers (4 bytes), long integers (8 bytes), three types, integers of different lengths, and the range of integers that can be stored is also different. Long integers account for more memory lengths than int integers, and can represent integers larger than int. For example, a decimal 2 of type int is actually stored in memory in 000000000 00000000 00000000 00000000 00000010 form. The greater the number of integers, the larger the integer range that can be represented by the integer type.
    2. BYTE: The byte type, as the name implies, is a byte in length, a byte type is used when the data is processed in bytes, such as processing, encoding, and so on, and byte can be used to represent integers, and the range of integers that can be represented is -128~127.
    3. Floating point number: Mainly used to store fractional values, can be divided into float floating point (4 bytes), double floating point (8 bytes). Double floating point numbers use more memory space than float floats, and can represent greater accuracy.
    4. Character: Char type is used to store character symbols such as ' A ', ' B ', ' Forest '. Each character type is 2 bytes, and Chinese characters are also double-byte stored in Java with the English character.
    5. Boolean: The Boolean type can represent True and false, respectively, representing the logical "true" and "false". In Java, you do not have to care about the length of the Boolean type, because you cannot do the Boolean type with other types.
    6. Note: Each type occupies a different length of memory, and the range of values that can be stored is different. For example, the memory space of the int type is four bytes, so the integer range that can be stored is -2147483648~2147483647, if the stored value exceeds the type range is called overflow value, it can cause the program unpredictable consequences, and we do not have to remember the various types of stored range of values, Can be implemented through the API. System.out.printf () is a standard API, meaning that the output text is formatted and then displayed in the text mode, some commonly used format control symbols to memorize! (the book has a table) class type: See Chapter Fourth

2. Variables

The staging location in the programming language we call variables, we can declare variables, that is, to tell the JVM, I want a location, this position is called XXX name, what type of data can be put. 1. To declare the variable type before use, that is, use keywords such as the Short,byte,char mentioned above to declare 2. Variables are named with some rules, it can not use numbers as the beginning, not special characters, and variable names can not be the same name as Java keywords, commonly used "camel" nomenclature, such as "int ageofstudent". So far, our program paradigm has been written in main (), and the variable declared in the method is called a local variable. 3. You cannot declare a local variable after you have specified no value to it before you use the variable. To initialize a variable before calling it, assign a value to the variable. PS: If, after assigning a variable, you do not intend to change can be added to final, such as final double pi=3.1415926, to prevent yourself or others inadvertently modified. 4. Tips: When representing numeric constants, 0b represents a binary number, and 0x represents a hexadecimal number, such as int a=0xc, which is used to make the expression clearer when it represents a longer number. such as int b=0b1010 _1010 _1010.

3. Operators

  1. Arithmetic operator + 、-、 *,/. That is, add, subtract, multiply, divide. % becomes the modulo operator or the remainder operator. The arithmetic operators use the same subtraction as the learned one, and they are multiplication and then added and reduced first. (Read more on the example) operators leave a space on each side, which is easier to read.
  2. There are also mathematical comparisons that are greater than, equal to, less than, and are also provided in Java, which are called comparison operators, which have greater than (>), less than (<), equals (= =), not less than (>=), not greater than (<=), and not equal to (!). =) and other conditional operators. Remember "equals" is a two equals sign! One = is the specified operation. Java has a conditional operator, (Conditional Operator): Conditional? Set up return value: Failed return value. such as SYSTEM.OUT.PRINTLN ("whether pass?") %d%n ", score>=60? Yes ': ' no ');
  3. In logical operators, there are several types of logic, "and" or "inverting." Use && (with), | | (OR),! (non) to indicate. && is characterized by the fact that if the && left is false, the logical judgment returns a false result regardless of the right truth; is characterized by the fact that if | | On the left is true, the logical judgment of the return result is true regardless of the right truth. This, to a certain extent, can improve the efficiency of code compilation and execution.
  4. Bitwise operators: There are and, or, not, XOR, and complement operations in the digital design, providing the corresponding bitwise operators in Java, namely & (and), | (OR), ^ (XOR), ~ (complement). If there is no basic bit arithmetic, you can find the feeling from the example in the book. Bitwise operations are bitwise-wise. The complement operation is to change all bits 0 to 0. The Java in-place operation also has a (>>) left Shift and (<<) right shift two operators. The left shift operator shifts all the bits to the left for the specified number of digits. The left side of the extrusion will be discarded, and the right side of 0, the right shift operator is the opposite, will all the bits to the right to move the specified number of bits, the right side is squeezed out of the bit will be discarded, as far as the left side of the original bit, if the left side is 0 to fill 0, is 1 1. >>> operator, after the right shift, the leftmost must be 0.
  5. Increment decrement I also feel that there is no difficulty, similar to the knowledge in C language. The key is to distinguish the i++,++i, if the + + operation is written before the variable, the value of the variable is added 1, and then return the value of the variable, the + + is written in the variable, the value of the variable is returned first, and then the variable is added 1. (examples in the book)
  6. Specify the operation, the table on the reference book is OK, no difficulty.

4. Type conversion

  1. In the actual operation, whether deliberately modified or inadvertently lost, the variable type will appear to confuse the situation. For example, by default, integers are of type int and floating-point numbers are double. As in the following example, float PI = 3.14,3.14 default to double type, can not be given a double type of decimal to float type, this will lose precision, compile error. But if you want to store it with float, you can add an F after 3.14, or float pi= (float) 3.14. When you write an integer in the program, the default is no more than the int type length. If you exceed it, you will tell the compiler to use long to configure the length of the integer, that is, add an L to the number.
  2. Operation, the type is not the same, the operation will be the longest type of primary (automatic promotion). Of course, if the operation is an integer that is not more than int, then all of them are automatically promoted to the int type.
  3. System.out.println (10/3); The display is 3, not 3.3333. Since both 10 and 3 are calculated under the int type, to represent 3.333, you must be System.out.println (10.0/3).

5. Process Control

  1. If......else I feel very simple, direct analogy C linguistics knowledge.
  2. Switch: Uses effects like multiple if......else statements, but is more concise, entering case statements with different switch values.
  3. For: Written for (...). ; ...... ; ...... ), the first semicolon before the content is more variable initialization. After the first semicolon is judged, the result is true to execute the statement after the second semicolon and into the loop, and the result is false to end the loop. Blocks are separated from blocks by semicolons, and the inner blocks are separated by commas.
  4. When do and do while:while (conditional) {description}, first determine whether the conditional formula is established, if it is established, then execute the description sentence. It is also possible to go directly into the loop with a while (true), judge by the IF statement in the loop, and conclude the loop with a certain situation depending on the break. The difference between do and is that a number is generated first (in the case of the book), and then the loop is judged. While doing is a direct step into the loop.
  5. Break, Continue:break is the direct end loop, continue ends the current loop, skips the description statement, and then returns to the beginning for the next round of loops. Break, continue can also be used in conjunction with the label (there are examples in the book), the process will be shown in the program practice.
Problems in teaching materials learning and the solving process

After-school selection: Daddabadad

After-School procedural questions 1

Operating results: The maximum common factor for 1000 and 495 is: 5 1000 and 495 of least common multiple are: 99000

Actually feel difficult, because last semester learned the Cryptography Mathematics Foundation This course, just here to use the concept of the Division method, here through the while statement to achieve the loop, and finally when the remainder of the 0 o'clock end cycle.

After-School procedural questions 2

Running Result: 153 370 371 407

My first thought was to use the For loop to find the number of Armstrong in the 100~1000 by means of a poor lift, and it is worth noting that the number of digits is separated by the%, and a, B, and C are all int types. No other place is difficult.

Problems in code debugging and the resolution process

If it is continue back1, there are no output results. But if it is continue back2, output "Hello World" nine times. Although the break is not shown here, when the break is used in conjunction with the continue tag, the back tag should be preceded by the for!

Other (sentiment, thinking, etc., optional)

The basic grammar in 1.java is not so much different from the basic grammar in C, so you can learn by analogy, which will do more with less. 2. In addition to mastering some basic knowledge, the main thing is to knock the code, details determine success or failure, continuous practice, and constantly find fault, and constantly solve problems, so that their ability to improve!

Learning progress Bar
Lines of code (new/cumulative) Blog volume (Add/accumulate) Learning time (new/cumulative) Important growth
Goal 3500 rows 25 Articles 300 hours
First week 120/120 1/1 14/14
Second week 340/460 1/2 14/28
Third week
Week Four

Resources
    • Java Learning Notes (8th Edition)
    • Java Learning Note (8th Edition) Learning Guide
    • ...

20145326 Java programming Second week of study summary

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.