"Java Programming Logic" Chapter 1 Programming Fundamentals

Basic data types and variables

Data types are used to declare variables. A variable is actually an identifier, which points to an area in memory. Declaring a variable is equivalent to allocating an area in the memory, and assigning a value to the variable is equivalent to putting the corresponding value in the variable area.

The basic data types of Java are as follows.

type of data Size (bytes)
byte 1
short 2
int 4
long 8
char 2
float 4
double 8
boolean

The char in Java occupies 2 bytes, while the char in C/C++ only occupies 1 byte.

For integer constants, the default type is int. Therefore, if the literal value of an integer constant exceeds the range of int, you need to add uppercase L or lowercase l after it. This is required even when assigning an integer constant to a long.

For floating-point variables, the default type is double. Therefore, when assigning a decimal constant to a float, you need to add an uppercase F or a lowercase f after it.

Array type

There are 3 types of data definition in Java.

int[] arr;
int[] arr = {1,2,3};
int[] arr = new int[3];

Unlike C/C++, the length of an array in Java can be determined dynamically, but its size cannot be modified after the array is declared.

int[] arr = new int[length]; // length是一个在运行时确定的变量

However, an array in Java cannot be given an initial value and a length at the same time.

int[] arr = new int[3]{
    
    1,2,3}; 

The above situation is not allowed.

Array type variables and basic type variables are quite different in memory allocation. There is only a corresponding memory space in the memory of the basic type variable. However, data type variables have two corresponding memory spaces, one is the memory space pointed to by the data name, which is used to store the starting address of the contents of the array; the other is used to store the contents of the data.

In Java, array type variables can be assigned to each other.

int[] A = {
    
    1,2,3};
int[] B = {
    
    1,2};
A = B; // 在Java中被允许

If you look at the above statement from the point of view of "variable name points to a piece of address stored in memory", after A=B;execution, the content stored in the memory space pointed to by variable A is no longer {1,2,3}the starting address of the array , but becomes the address pointed to by variable B The contents of the memory space, namely variables A and B, point to {1,2}the starting address of the array at the same time . {1,2,3}The memory space of the array is garbage collected because it is no longer referenced.

Basic operations

Basic operations in Java have the following types, and the meaning of operations is exactly the same as C/C++.

+ - * / % ++ -- 
> >= < <= == !=
& | ! ^
&& ||
? :

Conditional statements

Java has conditional statements if, if-else, if-else if-else, switchtype, and logical statements of C / C ++ is substantially the same. The only difference is that, in addition to integer variables, switch variables in Java also support enumeration types and String types.

loop statement

Java has a cycle while, do-while, for, for eachfour types. Among them, for-eachthe syntax is as follows.

for (type element : aSet) {
    ...
}

aSetIs a collection or array type variable, typewhich is the type of elements stored in it. The for-eachsyntax can be used without the use of index variables, each time the loop elementwill be updated to aSetthe nextelement

Control loop has two keywords break, continue.

Function usage

In Java, any function needs to be placed in a class , and a class contains multiple functions. At this time, the functions in the class are called methods . The method is defined as follows.

修饰符 返回值类型 函数名字(参数类型 参数名字, ...) {
    ...
   return ...;
}

Each class has one and only one main function. When the Java program is executed, it will look for this main function to start execution.

Functions in Java support variable length parameters.

public static int max(int min, int ... a) {
    ...
}

The variable ais an array type variable, which stores the variable parameter part, that is a, the length is uncertain, but it will be determined when the function is called.

The function supports overloading , but requires inconsistent parameter lists. When calling an overloaded function, the Java compiler will first select a function that exactly matches the calling parameters. If not, the compiler will perform an implicit type conversion to determine the most suitable overloaded function.

Guess you like

Origin blog.csdn.net/NelsonCheung/article/details/109818129