Part 2: Basic Information Representation and basic information processing main content: L basic data representation, including basic data types, constants and variables; L basic data processing, mainly introduces various basic operators; l process control, introduction of selection and loop structure; l code sharing; introduction of method definition and use; l string use; l date Use; l number processing; l use of the math class. This section describes the basic data representation. The previous course has already said that the language is used to represent information and then process the information. This content introduces information (sometimes I will use "data", there are some differences, but it does not affect the content we are talking about. Let's take a look at our natural language and describe someone who is not around you. You may say, "Medium body, small eyes, long hair, and a mole in the lower jaw, eloquence is very good. "If everyone knows this person, you can say" Jackie Chan "directly, because when you hear the name, you will know who it is and the basic information about it. How can I describe the person's information in Java? The simplest way is to use this sentence directly. For example, to output the information of this person, you can write: system. out. println, you can directly write this sentence in the database. This sentence becomes a string. With a string, all the information can be expressed as a string. In this case, the string is used to indicate whether information is displayed, in addition, strings are the most important in any language (I personally think), because any information can be expressed as strings. From the perspective of input and output, it also follows the string format. However, if you want to extract information from such a long string or perform some operations, it is troublesome to represent all the information as a string. For example, to compare the height of two people, we need to extract the height information from the information of two people respectively (we are relatively simple to retrieve, but it is more difficult to obtain the Program), and then compare. Therefore, we need to break it into smaller units and constrain the description method. The above information can be converted into multiple records: Height: moderate height = middle or Height = 170 eyes: relatively small eyes = little hair: relatively long hair = long chin whether there is a mole: it is haszhi = true. In this case, the height can also be represented by numbers. If a number is used, it can be expressed as 170 or 1.7, because the unit is different. At this time, we can add comments to tell the user, for example, when entering, We can prompt whether the user unit is meter or centimeter, and add the unit behind the number when outputting. This is how to convert our information into a programming language, and how to convert the information in a programming language into a natural language. To let the Java language express these basic information, you must first know the types of basic information in the real world. A simple score can be divided into string, number, date, and true/false. The date will be introduced later. The following describes the representation of these information types in Java.
StringJava. Lang. String is used to represent strings. Note that strings are not of the basic data type, but strings are special. The special character is described in "use of strings. The string-related character type provided in Java is Char. It can be divided into constants and variables.
Character constantThe character is usually enclosed in single quotes. For example, the character "A" can be expressed as: 'A' character "1" can be expressed: '1' note that uppercase letters A and lowercase letters a indicate two different characters. Some special characters, such as line breaks, cannot be directly expressed. escape characters must be used. The format is a backslash with special characters. For example, line breaks can be expressed as '/R '. Escape characters in the following Java:/N carriage return (/u000a)/T horizontal tab (/u0009)/B space (/u0008)/R line feed (/u000d) /f form feed (/u000c)/'single quotation marks (/u0027)/"Double quotation marks (/u0022) // backslash (/u005c)/DDD three octal character/udddd four-digit hexadecimal
Character variableA constant can represent basic character information in a program, but it cannot describe its meaning. For example, to indicate that the type of clothes is a, writing a directly does not indicate that it is the type of clothes. In addition, the entire information may change. Therefore, variables must be used to indicate the meaning of characters. For example, if the type of a dress is a, it can be expressed as: TYPE = 'A', where "type" indicates the type of the dress, "yes" indicates the equals sign, and a value is assigned to the variable. When declaring this variable, a type is required. Therefore, it is usually written as char type = 'A'. in Java, this is a statement. each statement should end with a semicolon, so it should be written: char type = 'a'; Note: Both semicolons and quotation marks should be in English; otherwise, an error will be reported: invalid characters can also be written separately with the variable description and value assignment, for example: char type; type = 'a'; char indicates the character type, which is specified in the syntax and must be written in this way. Type is the name specified by the user. It is an identifier and should be written according to certain naming rules.
Identifier naming rulesThe naming rules for identifiers are as follows: l must start with a letter (uppercase or lowercase), underscore _, or dollar sign $; l can use numbers in addition to the above characters; l The identifier cannot use keywords retained by the system. The system keyword is as follows:
abstract |
continue |
for |
new |
switch |
assert *** |
default |
goto * |
package |
synchronized |
boolean |
do |
if |
private |
this |
break |
double |
implements |
protected |
throw |
byte |
else |
import |
public |
throws |
case |
enum **** |
instanceof |
return |
transient |
catch |
extends |
int |
short |
try |
char |
final |
interface |
static |
void |
class |
finally |
long |
strictfp ** |
volatile |
const * |
float |
native |
super |
while |
* |
|
Not used |
** |
|
Added in 1.2 |
*** |
|
Added in 1.4 |
**** |
|
Added in 5.0 |
The following identifiers are correct: the identifiers under heightinput1 $ valuetime_12 are incorrect: 1 valuevalue # Try: Why is it incorrect? When defining variables, there should also be some coding habits: l the first letter of the variable is lower case; l a variable consisting of multiple words, followed by the first letter of the word in upper case; l variable names should be as meaningful as possible. These are not naming conventions, but should be followed.True/falseIt is usually called a Boolean value and the type name is boolean. There are two constants, true and false, which indicate true and false, respectively, or no. For example, whether a message is valid or not. You can use the Boolean Type: Boolean validate; Validate = true; Validate = false;NumberNumbers are divided into integers and floating-point numbers. The integer is also divided into byte short int long. Float and double. The difference is that the representation range is different. Byte occupies one byte, which indicates the range-128 ~ 127, indicating 28 numbers, from-27 to 27-1. You can use the following method to obtain the maximum and minimum values: byte. max_value and byte. min_value. Short occupies two bytes, int occupies 4 bytes, long occupies 8 bytes, float occupies 4 bytes, and double occupies 8 bytes. Constants of the integer type can be expressed directly by numbers. For example, 20,23,-30. You can use numbers directly for the double type, for example, 32323,333 4.33. A float type constant. If it does not contain a decimal part, you can directly write a number. If it contains a decimal part, you need to use F to represent it later, such as 232323, 23.33f. If F is not written, the system will think this is a double type, and an error will occur during the value assignment. For example, the following code: Float radius = 32.3; an error occurs during compilation. It should be written as: Float radius = 32.3f; note: when a floating point number is stored in a computer, it may not be the number itself, because it uses binary representation in the computer. How much is 32.3 converted to binary?Encapsulation type of the basic data typeByte, short, Int, long, float, double, Char, and Boolean are eight basic data types provided by Java. To make these basic data types consistent with the concepts of classes and objects in Java, Java provides encapsulation types for these basic data types. The basic data type only indicates information and has no processing function. The encapsulation type not only indicates information, but also provides some corresponding information processing methods as follows: byte byteshort character int integerlong longfloat floatdouble doublechar characterboolean can convert the basic data type to the encapsulation type, or convert the encapsulation type to the basic data type. The conversion between int and integer is used as an example below. Int Height = 170; // declare the integer variable and assign the value integer height2 = new INTEGER (height); // convert it to the encapsulation type Height = height2.intvalue (); // after the basic data type is converted to Java 5, you can directly write: int Height = 170; // declare the integer variable and assign the value integer height2 = height; // convert to the encapsulation type Height = height2; // convert to the basic data type to complete automatic conversion. Remember this article: L 8 basic data types; l identifier naming rules; l variable definition and assignment; L 8 constants of basic data types; L basic data type and encapsulation type. Last Lecture:Lecture 8 use the integrated development environment (I)Next Lecture: Lecture 10: basic operations-operators: next time we will introduce basic data processing, mainly using various operators. Li xucheng csdn blog: Why? U= 124362 & C = 7be8ba2b6f3b6cc5