Traditional Culture Encyclopedia - Photography and portraiture - What are the basics of Java?
What are the basics of Java?
1. Identifiers
The character sequences used by Java to name various variables, methods, classes and other elements become identifiers; in layman’s terms, anywhere you can name yourself Called identifiers, they all abide by the rules of identifiers
1. Identifier naming rules:
1) Identifiers consist of characters, underscores, dollar signs or numbers.
2) Identifiers should start with characters, underscores, and dollar signs
3) Java identifiers are case-sensitive and have no length limit
4) By convention, When selecting java identifiers, you should pay attention to "knowing the meaning by seeing the name" and not having the same name as the keywords of the java language (the colored ones in eclipes are basically keywords)
2. Keywords
< p>Some strings in java that are assigned specific meanings and used as keys for special purposes become keywords; and most editors will mark the keywords in a special way. All java keywords are in lowercase English p>Some common keywords:
3. Basic data types of java
1.java constants
Java constant values ??are represented by strings , divided into different data types. For example: integer constant: 1234 real constant: 3.14 character constant: 'a' logical constant: true, false string constant: "HelloWorld"
Note:
1) Distinguishing Character constants and string constants
Character constants are enclosed in single quotation marks, and string constants are enclosed in double quotation marks of unlimited length
Since Java uses Unicode encoding, each Characters occupy two bytes, so they can be expressed in hexadecimal encoding. Of course, they can also be represented by a Chinese character (a single Chinese character occupies two bytes)
2) "Constant" will still be ranked Used in other contexts to represent variables with immutable values
2.java variables
Java variables are the most basic storage unit in the program, and their elements include variable name, variable type and scope. Each variable in the java program belongs to a specific data type and must be declared before use. The declaration format is: type varName [=value]. For example: int i =100;foloat f=12.3f;double d1,d2, d3=0.123; (Three variables are declared, of which d1 and d2 are default values, d3=0.123) String s=”hello”
Essentially, a variable is actually a small piece of memory Area, use variable name to access this area, therefore, each variable must be declared before use, and then must be assigned a value before it can be used.
1) Java variable classification
Divided according to the position where it is declared:
Local variables: variables defined inside a method or statement block, which can only be defined Used within methods or statement blocks
Member variables: variables defined outside methods and inside classes can be used throughout the class, including methods or statement blocks in the class
Note: Variables cannot be declared outside the class
Divided according to the data type they belong to:
Basic data type variables
Reference data type variables
4. Operators
Java language supports the following operators:
Arithmetic operators: + - * / % ++ --
< p>Relational operators: > < >= <= == !=Logical operators: ! & | ^ && ||
Bitwise operators: & | ^ ~ > > << >>>
Assignment operator: =
Extended assignment operator: + = -= *= /=
String concatenation operator :+
Ternary conditional operator? :
1. Arithmetic operator
Note:
1)
In bitwise operators! , &, |, ^ are the same as Logitech operators, except that bitwise operators operate on the binary value of variables. I personally don’t use them, so I won’t introduce them here.
2)
++(—)
Compute before taking the value in the former tense
Get the value before taking the value in the latter tense
2. Logical operations operator
For example:
3. Extended assignment operator
4. String concatenation operator
Both sides of the " + " operator As long as one of the operands is of string type, the system will automatically convert the other operand into a string and then connect it, for example:
4 + 5 = 94 + "ab" = "4ab"
5. Trinary conditional operator
Grammar format: x? y: z where x is a boolean expression, first calculate the value of The result of the operation is the value of the expression y, otherwise the result of the entire operation is the value of the expression z.
5. Expressions and statements
1. Expression
An expression is a sequence of operators and operands that conform to certain grammatical rules, for example: a5 .0 + a(a – b) * c – 4i < 30 && i %10 !=0
1) The type and value of the expression
The operands in the expression The result of the operation is called the value of the expression
The data type of the expression value is the type of the expression
2) The order of operation of the expression
Operators should be processed in order from high to low priority
Operators with the same priority should be combined according to the implementation agreement
I personally think that the order of operations is It can be ignored. First of all, my logical thinking ability is not particularly strong, and my memory is not particularly good, so if I need to distinguish priorities in expressions, I will choose to add parentheses. However, I think that for some more complex and critical logical operations, if your personal logical operation ability and memory are relatively good and you can ensure that there are no errors, making good use of Logitech operator priority may not be a way to prevent others from understanding your code copy. .
It’s a small protection, anyway, people like me won’t try to analyze this kind of code, it’s too tiring
2. Branch (conditional) statement
ifif? … elseif ?… else if … else ifif ?… else if … else ?if …else
switch () {case xx:
…………case xx: p>
………………default:
…………}
1. The switch statement in java can only detect values ??of type int (values ??of type char It’s also possible, because it can convert to int type)
2. Be careful about case penetration, so it’s best to use it with break
3. Multiple cases can be used together, as shown in the following sample code It can also be written like this (when i=1, 2, 18, 18 will be output):
3. Loop statement
for(…;…;…){…}while( ...){...} first determine and then execute the content in the curly brackets, and then determine whether to continue execution do{...} whilee(...); first execute the content in the curly brackets and then determine whether to continue execution
4.break & continue statement
The break statement is used to terminate the execution of a statement block.
Can it be used in a loop body statement to forcibly exit the loop? The continue statement is used in a loop body statement to terminate a certain loop process, skip the unexecuted loop under the continue statement in the loop, and start the next loop process
< p>8. MethodsJava methods are similar to functions in other languages. They are a piece of code used to complete a specific function. The declaration format is: [Modifier 1? Modifier 2?…] Return value type ?Method name? (formal parameter list) ?{
java statement}
Formal parameters: used to accept external input data when the method is called
Actual parameters: the data actually given to the method when calling the method
Return value: the data returned by the method to the environment where it is called after completion of execution
Return value type: realize the agreed return The data type of the value. If there is no return value, the return value type void must be given
Calling the method in the Java language: object name. method name (actual parameter list)
The actual parameters The number, data type and order must match the formal parameter list declared by the called method
The return statement terminates the execution of the method and specifies the data to be returned
Passing parameters when making a function call in Java When passing by value, follow the principle of value transfer:
The basic type transfers the data value itself, and the reference type transfers the reference to the object, not the object itself
In the example In method 1, the data type defined earlier is void, so there cannot be a return value in the method. In method 4, because there is a return value, the type of the return value must be defined previously, which is the int in front of m4
9. Recursive call
Recursive call refers to the call of the method itself during the execution of the method
Let’s look at an example first. This is a simple recursive call:< /p>
A brief analysis:
1. First, the main method outputs a string, which is the return value of the test method when the parameter is equal to 5. 2. Then pass the parameter 5 into test. method, the return value is: 5 * test(4)3. Pass parameter 4 into the test method again, the return value is: 4 * test(3)4. Pass parameter 3 into the test method again, the return value is: 3 * test(2)5. Now pass parameter 2 into the test method: the return value is: 2 * test(1)6. Then pass parameter 1 into the method: the return value is: 1 and then the program starts to go back Go, pass the return value into test(1) to get 2*1 and then go back, pass the 2*1 you just got into test(2) to get 3*2*1 and go back again, put the 2*1 you just got Pass the 3*2*1 into test(3) to get 4*3*2*1 and go back, pass the 4*3*2*1 just obtained into test(4), which is test The return value of (5) is 5*4*3*2*1. Finally, the return value of test5 is passed into our main method and output in the statement 5*4*3*2*1=120, then our output The output of the statement should be 120
This is an example of a simple recursive call
Let’s look at another example:
Non-recursive writing:
Please understand by yourself
Finally, we provide a complete learning roadmap for basic Java syntax. In addition, there are other carefully organized Java learning roadmaps, learning books and e-books, Alibaba manuals, and video tutorials. You can click here to get it:
/p/6
- Previous article:Is Jiaozhou Bay cross-sea tunnel fun or cross-sea bridge fun?
- Next article:How about Shanghai Yumin Entertainment Co., Ltd.?
- Related articles
- Rosso, is it good to study abroad in art?
- Xiaoquan photographer
- Food is also a kind of life.
- Why do you say "full of flowers and half fruits, half flowers and half fruits?"
- How much does it cost to apply for picture copyright?
- Opening remarks of Chinese weddings
- What police officer schools are there in Jiangsu?
- Nanning Fengling Children's Park Skydiving Tower Video
- What are the classic works of traditional Suzhou embroidery?
- What is Weibo?