Java zero foundation entry series – day4 variables and constants

This article mainly explains variables in Java, what variables are, the role of variables, and how to declare and use variables.

So what are variables? For beginners, variables can be understood as boxes, which can be used to store data. Different types of data need to be placed in corresponding types of boxes. So why do we use boxes? It is mainly used to store the data that needs temporary access and operation during the operation of the program. For example, when we make a tomato scrambled egg, we need to wash the tomato first, then cut it and put it in a bowl. After the egg is beaten, we need to put it in another bowl, then add cooking wine, add water and stir it, and then put it in the pot. After a blind operation, a delicious tomato scrambled egg comes out, In this process, scrambled eggs with tomatoes are equivalent to our results. Tomatoes and eggs are the data we need to process. We first process tomatoes and eggs respectively. In the process of processing, we all need a container to store them, bowls, plates, pots or cups. These containers are equivalent to variables. Obviously, if there are no variables, It is difficult for us to make this delicious meal. The significance of variables is to store data, make the program clearer, and make the program easy to modify and maintain.

A variable has four elements, type, variable name, value and scope. Java is a strong data type language. Each variable must belong to a type. An int type data needs to be stored in an int type variable. Similarly, a double type data needs to be stored in a double variable. There are also some specifications for the setting of variable names. They must start with a letter and consist of letters or numbers. The letters here include upper and lower case letters or Unicode characters representing letters in a language. Numbers include '0' ~ '9' and Unicode characters and underscores representing numbers in a language, And dollar sign$ (but do not use), but Java reserved words cannot be used. In addition, symbols such as' + ',' - 'are not allowed to appear in variable names. All characters in variable names are meaningful, case sensitive, and there is no limit on the length of variable names. It is very necessary to choose a meaningful name for variables, which will become more and more important when the amount of program code increases. For example, age, name, sum, classnum, and so on

For the sake of standardization, I hope you can form a good habit from the beginning. Using the small hump naming method is a good choice. The so-called small hump naming method is that the first letter of the variable name is capitalized except the first word, and other letters are lowercase, such as teachername, vacationdays, classmatesnum, etc.

Variables need to be declared before use, such as int num; To assign a value to a variable, you need to put the variable name to the left of the equal sign and the corresponding value to the right of the equal sign, such as int num; num =10; You can also initialize variables while declaring variables and assign initial values to variables, such as int num = 10; Declarations can appear anywhere in the code, but you must declare the variable before using it. Unlike C and C + +, in Java, there is no distinction between declaration and definition.

As for the scope of action, it is the effective range of variables. Out of this range, variables cannot be used. This will be explained later in combination with the code.

Give a little chestnut to understand the usage of variables.

  int length = 10;//定义一个整数型变量,变量名为length,并初始化赋值为10
  System.out.println(length);//输出这个变量的值
  length = 20;//给这个变量赋新值
  System.out.println(length);//输出

Here, 10 and 20 will be output in turn. Comments are used here. The comments after the double slash will be automatically ignored and will not affect the operation of the code.

Therefore, the correct way to use variables is to define them first and then use them. For example, compare the computer to a warehouse administrator, and the memory to a warehouse. When I need to use a bowl, I need to register first, and then the administrator gives me a bowl, so I can use it recklessly. Registration here is like a declaration. Then why declare? em.... If you don't tell me how I know whether you want a bowl or a dish or a cup, I have to give you an error gift bag. Secondly, the cups, dishes and dishes mentioned here are dedicated, that is, each device can only hold a specific type of things, just like int variables can only hold int data.

In addition to variables, there is a guy called constant in Java, which uses the keyword final to indicate constants, for example: final double pi = 3.14; A constant, as its name implies, is an invariant quantity. Once declared, its value cannot be changed. Therefore, it must be initialized when declared. A constant can be understood as a variable whose value cannot be changed. In general, all letters of constant naming need to be capitalized. If there are multiple words, they are connected with underscores. Since it cannot be changed, what is the meaning of constant existence? What is the significance of replacing 3.14 with Pi? Of course, it has its significance. For example, when calculating the area of a circle, using 3.14 * 5 * 5 naturally does not have pi * 5 * 5, so it looks clear. Another important point is that if the initialization value of a constant needs to be changed, for example, set a constant called full at the beginning_ Mark, at first, you thought the full score was only 100, but after the program was written, you were told that the full score was 120, so you only need to change 100 to 120 where the constant is defined, instead of changing each 100 in the code to 120, which not only increases readability, but also increases maintainability.

Don't ignore the readability of the code. The code is not only used to run, but also used to read, and the readers are not only yourself, but also other programmers. If you can't see your own at the end, it's definitely not an excellent code.

I hope you can complete the code as a work of art and enjoy the pleasure of those letters beating from your fingertips, so as to better harvest the happiness of the smooth operation of the program.

So far, the content to be introduced in this article is over. Short and tough series.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>