Java basic syntax 02

Posted by himnbandit on Mon, 28 Feb 2022 13:39:52 +0100

3. Type conversion

1) Because Java is a strongly typed language, type conversion is required for some operations

Low ------------------------------------------------- > High

​ Byte,short,char ----> int ----> long ----> float ---->double

2) In the operation, different types of data are converted to the same type first, and then the operation is carried out

3) Forced conversion, automatic conversion

//Cast: (type) variable name high -- > low
int i = 128;
byte b = (byte)i;  //out of memory

System.out.println(i);
System.out.println(b);

//Automatic conversion: low -- > High
double c = i;

System.out.println(c);

/*
be careful:
1.Boolean cannot be converted
2.Cannot convert an object type to an unrelated type
3.Cast: (type) variable name high -- > low
  Automatic conversion: low -- > High
4.Memory overflow or accuracy problems may occur during conversion!
 */
System.out.println("================================================");
System.out.println((int)23.7);
System.out.println((int)-45.89f);

System.out.println("================================================");
char a = 'A';
int  d = a + 100;

System.out.println(d);
System.out.println((char)d);

Output result:
128
-128
128.0
================================================
23
-45
================================================
165
¥

4. Variable, constant

(1) Variable: is the variable quantity!

1) Java is a strongly typed language. Each variable must declare its type

2) Java variable is the most basic storage unit in the program, and its elements include variable name, variable type and scope

type varName [=value] {,varName [=value]}
//Data type variable name = value; You can use commas to declare multiple variables of the same type at the same time

3) . note:

1. Each variable has a type, which can be a basic type or a reference type

2. Variable name must be a legal identifier

3. The variable declaration is a complete statement, so it needs to end with a semicolon

4) . variable scope

1. Class variable

2. Instance variable

3. Local variable

public class Variable{
    static int allClicks=0;  //Class variable
    String str="Hello,xiaoShu!";   //Instance variable
    public void method{
        int i = 0;     //local variable
    }
}

Note: [the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-dpyhkri6-1646051003284) (C: \ users \ Kevin \ appdata \ roaming \ typora \ typora user images \ image-20220228091517126. PNG)]

(2) constant: the value cannot be changed after initialization! Value that will not change

1) The so-called constant can be understood as a special variable; After its value is set, it is not allowed to be changed during program operation

//final constant name = value
final double PI = 3.14;

2) Constant names generally use uppercase characters

(3) Naming conventions

1. All variables, methods and class names: see the meaning of the name

2. Class member variables: lowercase and hump rules: monthSalary

3. Constants: uppercase letters and underscores: MAX_VALUE

4. Class name: initial capitalization and hump rule: Man, GoodMan

5. Method name: initial lowercase and hump rule: run(), runRun()

5. Operator

The java language supports the following operators:

1. Arithmetic operators: +, -, *, /,%, + +, –

2. Assignment operator:=

3. Relational operators: >, <, > =, < =, = =,! = instanceof

4. Logical operators: & &, |!

5. Bitwise operators: &, |, ^, ~, > >, <, > > (understand)

6. Conditional operator:?:

7. Extended assignment operators: + =, - =, * =/=

Priority of Java operator:

operatorAssociativity
[ ] . () (method call)From left to right
! ~ ++ – + (unary operation) - (unary operation)Right to left
* / %From left to right
+ -From left to right
> >>>From left to right
< >= instanceofFrom left to right
== !=From left to right
&From left to right
^From left to right
|From left to right
&&From left to right
||From left to right
?:Right to left
=Right to left

6. Package mechanism, JavaDoc

(1) Package: in order to better organize classes, java provides a package mechanism to distinguish the namespace of class names

1. Generally, the company domain name is inverted as the package name: www.baidu.com com -----------> com. baidu. www

2. In order to use the members of a package, we need to explicitly import and change the package in the java program. Use the "import" statement

import package1[.package2...].(classname|*);     //. * indicates that all wildcards under the package are changed
import com.kum.shu.*;           //Import all classes under this package

(2)JavaDoc

1. javadoc commands are used to generate their own API documents: javadoc parameter files

cmd Execute from the command line:
D:\Java SE\Basic grammar\src\com\kum\shu\base>javadoc -encoding UTF-8 -charset UTF-8 Doc.java

Generate the file and click index html

2. Parameter information

1) @ author author author name

2) @ version version number

3) @ since indicates that the earliest JDK version is required

4) param parameter name

5) @ return return return value

6) @ throws exception thrown

Topics: Java Back-end