This is the first day of my participation in the Gwen Challenge in November. Check out the details: the last Gwen Challenge in 2021

Thank you for meeting me. Hi, I’m Ken

So without further ado, let’s get down to business

📌 Identifier and keyword

identifier

The valid sequence of characters used to identify class, variable, method, type, array, and file names is called an identifier. Simply put, an identifier is a name.

The Unicode character set

The character set is managed by the UNICODE Consortium and is subject to technical modifications and can recognize up to 65,536 characters. The first 128 characters of the Unicode character set, which happens to be ASCII, include Kanji in Chinese, Katakana in Japanese and Hekana in Japanese, Korean, Russian, Greek, and many other languages.

The keyword

Keywords are words that have a specific purpose or are assigned with a specific meaning. Keywords cannot be used as identifiers.

📌 Basic data type

Basic data types are also called simple data types. There are eight basic data types in the Java language: Boolean, byte, short, char, int, long, float, and double. These eight basic data types are conventionally divided into four types: logical types (Boolean), integer types (byte, short, int, long), character types (char), floating point types (float, double)

📌 Floating point type of the character type

Floating point types are float(single precision) and double(double precision).

  • Float type

Represents constants: 453.5439F, 21379.987F, 231.0f(decimal notation), 2e40F (2 times 10 to the 40th, exponential notation). It is important to note that constants must be followed by the suffix f or f.

Declare a float variable:

float x = 22.76 f, tom = 1234.987 f, weight = 1e-12F;
Copy the code

The float variable stores eight significant digits when storing float data (as opposed to double significant digits). For example, x = 12345.123456789f. The actual value of x stored is: 12345.123.

  • double

Represents constants: 2389.539d, 234656.124, 0.05(decimal notation), 1E-90 (1 times 10 to the -90, exponential notation). For double constants, you can have the suffix D or d, but you can omit the suffix.

Declare a double variable:

double height = 23.345, width = 34.56 D, length = 1e12;
Copy the code

The double variable stores 16-bit significant digits when storing double data.

It is important to note that when comparing float data with double data, you must pay attention to the actual precision of the data, for example, for

float x = 0.4 f;
double y = 0.4;
// The actual data stored in variable x is 0.4 000 0000 59604645 (here accurate to 16 bits).
// The actual data stored in variable y is 0.4000000000000000 (here accurate to 16 bits).
// Therefore, y
Copy the code

📌 Type conversion operation

Order part of the data type in order of lowest to highest precision:

byte short char int long float double
Copy the code

When the value of a lower-level variable is assigned to a higher-level variable, the system automatically converts the data type.

float x = 100;
// Output result :100.0

int x = 50;
float y;
y = x;
// Output result :50.0
Copy the code

When assigning a value from a higher-level variable to a lower-level variable, a type conversion operation must be used.

/ / format:
// (type name) the value to convert;

int x = (int)34.89; / / 34
long y = (long)56.98 F;  / / 56
int z = (int)1999L;  / / 1999
Copy the code

When assigning an int constant to a byte, short, or char variable, the type conversion must be performed if the value range is exceeded. Otherwise, precision will be lost.

byte a = (byte)128;  / / - 128
byte b = (byte) (-129);  / / 127
Copy the code

Common error: Assigning a double to a float without a conversion, for example:

float x = 12.4;
Copy the code

The right thing to do is:

float x = 12.4 F;
Copy the code

or

float x = (float)12.4;
Copy the code

📌 Input and output data

Input basic data

// Create an object
Scanner x = new Scanner(System.in); 

// Various basic types of data
// nextBoolean(), nextByte(), nextShort(), nextInt(), nextLong(), nextFloat(), nextDouble()
Copy the code
/ / case anli
import java.util.Scanner;
public class anli {
public  static void main(String []args) {
System.out.println("Please enter several numbers and press Enter for each number.");
System.out.println("Finally enter the number 0 to end the input operation.");
Scanner reader = new Scanner(System.in);
double sum = 0;
double x = reader.nextDouble();
while(x! =0) {
sum = sum + x;
x = reader.nextDouble():
}
System.out.println("sum = "+ sum); }}Copy the code

Output basic data

System.out.println(); / / a newline
System.out.print();   / / not a newline
Copy the code
System.out.println("nihao"); //
System.out.println("nihao" + "java"); // 
Copy the code

In addition, JDK 1.5 added a method to output data similar to the printf function in C.

/ / format:
System.out.printf("Format Control section"The expression1The expression2, expression n);Copy the code

The format control part is composed of format control symbols % D, % C, % F, % S and different characters. Ordinary characters are output as is, and format symbols are used to output the value of expressions.

%d: outputs int data. % C: outputs char data. %f: outputs floating point data with a maximum of 6 decimal digits

You can also control the position of the output data on the command line. %md: The output int data occupies m columns. %m.nf: The output floating point data occupies M columns and the decimal point is reserved for N digits.

// anli:
System.out.printf("%d,%f".12.23.78);
Copy the code

📌 array

Declare an array

Declaring an array includes the name of the array variable and the type of the array. There are two formats for declaring a one-dimensional array:

Array element type Array name []; Array element type [] array name;Copy the code

There are two formats for declaring a two-dimensional array:

Array name [] []; Array element type [] [] array name;Copy the code
// anli: define one-dimensional and two-dimensional arrays
float boy[];
char cat[] [];
Copy the code

You can also declare multiple arrays at once

int [] a, b;
/ / or
int a[], b[];


// In particular,
int [] a, b[];
Int array a; int array B; int array B; int array B;
int a [], b [] [];
Copy the code

Note: Unlike C/C++, Java does not allow you to specify the number of array elements inside square brackets in declared arrays. Int a [12]; Or int [12] a; Both will result in syntax errors.

Allocates elements to an array

// Assign elements to arrays in the following format:Array name =newType of array elements [number of array elements]// anli
boy = new float[4];
boy[0] = 12;
boy[1] = 23.908 F; .// Declaring an array and creating an array can be done together, for example:
float boy[] = new float[4];
// A two-dimensional array, like a one-dimensional array, must be assigned elements by the new operator after the declaration.
int mytwo[] [];
mytwo = new int [3] [4];
/ / or
int mytwo [] [] = new int[3] [4];
Copy the code

Java declaration of multidimensional arrays, a two-dimensional array is composed of several one-dimensional arrays, for example, the two-dimensional array mytwo created above is composed of three one-dimensional arrays mytwo[0], mytwo[1], mytwo[2] of length 4.

// To form a two-dimensional array, you can specify the length of the one-dimensional array to form the array, for example:
int a [] [] = new int [3] [];
The length of the array a[0], a[1], and a[2] is determined
a[0] = new int[6];
a[1] = new int[12];
a[2] = new int[8];
// Note: Unlike C, Java allows a variable of type int to specify the number of elements in an array
/ / such as:
int size = 30;
double number[] = new double[size];
Copy the code

📌 Use of array elements

A one-dimensional array accesses its elements through an index. Note that the index starts at 0, so if the array has 7 elements, the index ends at 6.

boy[7] = 384.98 f;
Copy the code

The program can also compile, but will run with an out-of-bounds index error.

The use of 📌 length

The number of elements in an array is called the length of the array. For a one-dimensional array, the value of array name.length is the number of elements in the array. For a two-dimensional array, the value of “arrayname.length” is the number of one-dimensional arrays it contains.

// anli:
float a[] = new float[12];    // a.length = 12
float b[] [] = new int[3] [6]; // b.length = 3
Copy the code

📌 Array initialization

After creating an array, the system gives each element of the array a default value, such as 0.0 for float.

// We can declare an array and give an initial value to the elements of the array
float boy[] = { 21.3 f.23.89 f.2.0 f.23f.778.98 f };

// As opposed to:
float boy[] = new float[5];
boy[0] = 21.3 f;
boy[1] =... boy[4] = 778.98 f;

// It is also possible to initialize a two-dimensional array directly with several one-dimensional arrays of different lengths
int a[]{  } = {{1}, {1.2}, {1.2.3}, {1.2.3.4}};
Copy the code

A reference to the 📌 array

Arrays are referential variables, so if two arrays of the same type have the same reference, they have exactly the same element.

int a[] = {1.2.3}, b[] = {4.5};

a = b;
// If a and B are of the same type, the above assignment statement can be used, and a and B have identical elements
Copy the code

Note that system.out.println (a) does not print a reference to array A, but all elements of array A.

char a[] = {'in'.'the'.'families'.'big'};
System.out.println(a);// China University of Science and Technology

// If you want to print a reference to an array of type char, you must have array A and the string union
System.out.println("" + a);
Copy the code

Thanks for reading. I’m Ken