Basic data types and arrays of Java program development and learning

Basic data types and arrays of Java program development

(Study reference book: Java University Practical Course Third Edition)

1. Identifier

The valid character sequence used to mark class names, variable names, method names, type names, array names, and file names is called a marker. Simply put, the identifier is a name .

The provisions of the Java language identifier:

  • It consists of letters, underscores, dollar signs, and numbers.
  • The first character cannot be a number.
  • It cannot be a keyword.
  • Sensitive to case.

Naming conventions in Java:

  • Package name : All letters are lowercase when multiple words are formed: xxxyyyzzz
  • Class name, interface name : when multiple words are formed, the first letter of all words is capitalized: XxxYyyZzz
  • Variable name and method name : when multiple words are formed, the first letter of the first word is lowercase, and the first letter of the following words is uppercase: xxxYyyZzz
  • Constant name : All letters are capitalized, and multiple words are connected with underscores: XXX_YYY_ZZZ

Two, keywords

Some words that have been given specific meanings in the Java language are called keywords, and there are the following 50:

  • Used for data types : boolean, byte, char, double, false, float, int, long, new, short, true, void, instanceof.
  • 用于语句:break、case、 catch、 continue、 default 、do、 else、 for、 if、return、switch、try、 while、 finally、 throw、this、 super。
  • 用于修饰: abstract、final、native、private、 protected、public、static、synchronized、transient、 volatile。
  • Used for methods, classes, interfaces, packages and exceptions : class, extends, implements, interface, package, import, throws.
  • Contains defined values : true, false, null
  • Meaningless but reserved : cat, future, generic, innerr, operator, outer, rest, var

Three, basic data types

  1. Logic type
    Constant: ture, false.
    Definition: Use the keyword boolean to define logical variables. In Java, conditions and loops are both boolean tests. Java cannot use integers as conditional judgments (such as: 1 is true, 0 is false), and can only be passed through boolean variables; logical variables Initial value can be assigned during definition.
    boolean a = true;
    while (a) {……}

  2. Integer type
    (1) int type: defined by the keyword int; memory allocation is 4 bytes, occupying 32 bits, and the value range is -2 31 to 2 31 -1.
    (2) Byte type: Use the keyword byte to define; memory allocation is 1 byte, occupying 8 bits, and the value range is -2 7 ~ 2 7 -1.
    (3) Short type: Use the keyword short to define; memory allocation is 2 bytes, occupying 16 bits, and the value range is -2 15 to 2 15 -1.
    (4) Long type: Use the keyword long to define; the memory is allocated 8 bytes, occupying 64 bits, and the value range is -2 63 ~ 2 63 -1. (L is added to the end of the constant)

  3. Character type
    char type: use the keyword char definition; memory is allocated two bytes, occupying 16 bits. The value range is 0~65535.
    The Java language uses the Unicode standard character set table, which can recognize up to 65536 characters. (The first 128 characters in the Unicode character set table are the ACSII codes)
    Note the escape character constants:'\n','\b','\t','\'','\'''

  4. Floating point type
    (1) float type: defined by the keyword float; memory allocation is 4 bytes, occupying 32 bits, the value range is 10-38 –10 38 and -10 38 – -10 -38 (F is added to the end of the constant) .
    (2)  The double type is
    defined by the keyword double; the memory is allocated 8 bytes, occupying 64 bits, and the value range is 10 -308 –10 308 and -10 308 – -10 -308 (add d at the end of the constant (or not )).

Four, basic data type conversion

Arrangement of basic data types from low to high precision: byte, short, char->int->long->float->double
(1) When assigning the value of a low-level variable to a high-level variable, the system automatically completes the data Type conversion.
(2) When assigning the value of a high-level variable to a low-level variable, an explicit type conversion operation must be used. (Forced type conversion)
such as: int x = (int)34.89; long y = (long)56.98F;
matters needing attention in type conversion:

  • The boolean type cannot be type converted.
  • You cannot convert object types to objects of unrelated classes.
  • You must use coercive type conversion when converting a large-capacity type to a small-capacity type.
  • The conversion process may cause overflow or loss of accuracy

Five, data input and output

1. Data output

System.out.printf("格式控制部分",表达式1,表达式2);

The format control part is composed of format control symbols and ordinary characters. The format control symbols need to be replaced by expressions, and ordinary characters are output as they are.

Format control symbols: %d (integer type), %c (character type), %f (floating point type, with a maximum of 6 decimal places), %s (string), %md (integer data in m columns) , %M.nf (floating point number in column m, with n decimal places reserved); Java advocates using %n to wrap.

2. Data input

Scanner reader = new Scanner(System.in);

Scanner is a class, and reader is an object created using this class. The reader object can call the following methods (functions) to read various data types input by the user:
nextByte(), nextDouble(), nextFloat(), nextInt(), nextLine(), nextLong(), nextShort()

  • When the above methods are executed, they will be blocked, waiting for the user to input data on the command line and press Enter to confirm.
  • If the data entered by the user exceeds the range of the type specified by the method or the integer and floating-point types are not uniform, the reader object will return false when calling the hasNextXXX method.
  • In Java programs, readers are often asked to call the hasNextXXX method (as a condition) first, and then call the nextXXX method to read in data.

Six, array

An array is a compound data type composed of data of the same type in sequence. The data in the array is used by adding an array subscript to the array name, and the subscript is sorted from 0.

1. Declare the array

Two ways of one-dimensional array declaration:

数组元素类型 数组名字[];
数组元素类型[] 数组名字;

Two ways of two-dimensional array declaration:

数组元素类型 数组名字[][];
数组元素类型[][] 数组名字;

2. Create an array
Declaring an array only gives the name of the array and the data type of the elements. If you want to actually use the array, you need to allocate a memory space of the corresponding length for it, that is, create an array. (The array name stores the first address of the allocated memory unit. ) The format is as follows:

数组名 = new 数组元素的类型[数组元素的个数];

Declaring an array and creating an array can be done together:

数据类型 []数组名 = new 数据类型[元素个数];

3. Initialization of the
array When the array is initialized , the declaration, creation and assignment are completed at the same time.

数据类型 []数组名 = {
    
    x,y,z...};

4. The use of length
For one-dimensional arrays: the value of array name.length is the number of elements in the
array ; for two-dimensional arrays: the value of array name.length is the number of one-dimensional arrays it contains;

5. Array reference
For: int []a = {1,2,3},b = {4,5};
Two numbers store different numbers, but as long as the two arrays have the same data type, use the assignment statement " a = b", then the references of a and b are the same. The value and number of a is exactly the same as the value and number of b.

System.out.print(数组名);//除字符数组外的数组通过该语句输出该数组的引用
System.out.print(""+数组名);//字符数组通过该语句输出引用

Guess you like

Origin blog.csdn.net/YCF8746/article/details/112304504