This article brings you an introduction to basic syntax in Java and related precautions. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The basic syntax of Java has many similarities with PHP, but there are also some specific keywords that are unique to Java.
The basic syntax of Java generally includes: keywords, identifiers, comments, constants, base and base conversion, variables, data types and type conversions, operators, statements
1. Keywords:
(1) Words given specific meanings by the java language
(2) They are all lowercase English words
(3) goto and const exist as reserved words and are not currently used
(4) Keywords used to define data types: class/interface/byte/short/ int/long/float/double/char/boolean/void
(5) Keywords used for data type values: true/false/null
(6) Keywords used to define control flow: if/ else/switch/case/default/while/do/for/break/continue/return
(7) Keywords used to define access permission modifiers: private/protected/public
(8) used to define Keywords for class, function, and variable modifiers: abstract/final/static/synchronized
(9) Keywords for modifiers used to define the relationship between classes: extends/implements
(10) Use Keywords used to define instances, reference instances, and judge instances: new/this/super/instanceof
(11) Keywords used to handle exceptions: try/catch/finally/throw/throws
(12) Keywords used for packages: package/import
(13) Other modifier keywords: native/strictfp/transient/volatile/assert
2, identifier:
(1 ) Overview: It is the character sequence
used when naming classes, interfaces, methods, variables, etc. (2) Composition rules: English uppercase and lowercase characters, data characters, $ and _
(3) Notes: Distinguish Upper and lower case, cannot start with a number, cannot be a keyword in java
3. Constant:
(1) Overview: Its value cannot be changed during program execution
(2) String constants: content enclosed in double quotes
(3) Integer constants: all integers
[1] Binary: composed of 0, 1. Starting with 0b
[2] Octal: composed of 0, 1,...7. Starting with 0
[3] Decimal system: composed of 0, 1,...9. The default integer is decimal
[4] Hexadecimal: composed of 0, 1,...9, a, b, c, d, e, f. Starting with 0x
(4) Decimal constants: all decimals
(5) Character constants: content enclosed in single quotes
(6) Boolean constants: only true/false
(7) Empty constants : null
4. Data type:
(1) The Java language is a strongly typed language. A clear specific data type is defined for each type of data, and different sizes are allocated in the memory. Memory space
(2) Basic data types:
[1] Numeric type: integer type (byte, short, int, long), floating point type (float, doublt)
[2] Character type : char
[3] Boolean type: boolean
(3) Reference data type:
[1] Class (class)
[2] Interface (interface)
[3] Array ( [])
(4) Notes on using variables:
[1] Scope: The scope of the variable is within the curly braces. Two variables with the same name cannot be defined in one scope.
【2】Initialization value: The value must be initialized, otherwise the variable cannot be used.
【3】Define a variable in one line. You can also define multiple, but it is not recommended.
(5) Data type conversion:
[1] boolean type cannot be converted to other types
[2] Default conversion: automatic conversion, which is a conversion performed quietly during the execution of the program, no need The user declares in advance that the conversion is generally from a low-digit type to a high-digit type. (Low--->High: byte, short, char-> int -> long -> float -> double) (byte, short, and char are not converted to each other. They will be automatically converted before participating in the operation. Convert to int)
[3] Forced conversion: must be declared in the code, the conversion order is not limited. Target type variable name = (target type) (converted data)
5. Operator:
(1) Arithmetic operators, assignment operators, comparison operators, logical operators, bitwise operators, ternary operators6. Method:
(1) Overview: A method is a code block that completes a specific function
(2) Format: Modifier return value type method name (parameter type parameter name 1, parameter type parameter name 2...) {function body; return Return value; }
(3) Notes:
[1] The method will not be executed if it is not called
[2] Methods are in a horizontal relationship and cannot be nested.
[3] Method definition Parameters are separated by commas
[4] There is no need to pass the data type when calling the method
[5] If the method has a clear return value, there must be a return to bring back a value
[ 6] When there is no clear return value, it is actually a call to a void type method
(4) Method overloading:
[1] Overview: In the same class, more than one method with the same name is allowed, as long as they The number of parameters or parameter types can be different.
[2] Features: 1. It has nothing to do with the return value type, only the method name and parameter list are looked at. 2. When calling, the virtual machine distinguishes methods with the same name through the difference in parameter lists.
7. Array:
(1) An array is a thing (container) that stores multiple variables (elements)
(2) The data types of these multiple variables must be consistent
(3) Format: 1. Data type [ ] Array name; 2. Data type array name[];
The above is the detailed content of Introduction to basic syntax in java and related precautions. For more information, please follow other related articles on the PHP Chinese website!