Java 0 Basic Starter series DAY4 variables and constants

Source: Internet
Author: User

This article focuses on variables in Java, what are variables, how variables are used, and how to declare and use variables.

So what is a variable? For beginners, variables can be understood as boxes, which can be used to store data, and different types of data need to be placed in the corresponding type of box. So, why do we use boxes? Mainly used to store the program to run the process of temporary access and operation of data, for example, we do a meal of tomato scrambled eggs, we need to wash the tomatoes first, and then cut into the bowl, the egg after the first put in another bowl, and then add cooking wine, add water stirring, and then put the pot after a blind operation, A delicious tomato scrambled eggs came out, the process, the tomato scrambled egg equivalent to our results, tomatoes and eggs are we need to deal with the data, we first processed the tomato and eggs separately, and the process of processing, we 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 to make this delicious, the meaning of variables exist is to store data, so that the program is clearer, so that the program is easy to modify and maintain.

A variable has four elements, a type, a variable name, a value, and a range of functions. Java is a strong data type language, each variable must belong to a type, an int type of data needs to be stored in a variable of type int, similarly, a double type of data needs to be stored in a double variable. For variable name settings, there are some specifications that must begin with a letter, consisting of letters or numbers, where the letters include uppercase and lowercase letters or Unicode characters that represent letters in a language, numbers include ' 0 ' ~ ' 9 ' and Unicode characters and underscores in a language that represent numbers, As well as the dollar sign $ (but not used), but the Java reserved word cannot be used, moreover, symbols such as ' + ', '-' are not allowed in the variable name, all characters in the variable name are meaningful and case-sensitive, and the length of the variable name is not limited. It is necessary to take a meaningful name for a variable, which becomes more and more important when the amount of code in the program increases. For example, age, Name, sum, Classnum, etc.

In order to standardize, I hope everyone from the beginning to form a good habit, the use of small hump name method is a good choice, the so-called Small hump named method, is the variable name in addition to the first word other words other than the first letter uppercase, the other letters are lowercase, such as teachername, Vacationdays, Classmatesnum and so on.

Variables need to be declared before they are used, such as: int num; To assign a value to a variable, you need to place the variable name on the left side of the equals sign, and the corresponding value on the right side of the equal sign, such as int num; num = 10; You can also initialize a variable at the same time you declare a variable, assigning an initial value to the variable, such as int num = 10; a declaration can appear anywhere in the code, but you must declare the variable before you use it. Unlike C and C + +, in Java, declarations and definitions are not distinguished.

As for the scope of the variable is the effective range, out of this range, the variable can not be used, which is specific to the following code to illustrate.

Give a little chestnut to understand the use of variables.

int length = ten=; System.out.println (length);

This will output 10 and 20 in turn.

In addition to variables, Java also has a guy called constants, with the keyword final to indicate the constant, for example: final double PI = 3.14, constant, as the name implies is the same amount, once declared, its value can not be changed, and therefore must be initialized at the time of Declaration, Constants can be understood as variables whose values cannot be changed. Constant naming generally all letters need to be capitalized, and if more than one word is underlined. Since it cannot be changed, what is the meaning of constant existence? What is the meaning of using pi instead of 3.14? Of course there is the meaning of it, for example, when calculating the area of a circle, using 3.14*5*5 naturally does not pi*5*5 to look clear. And there is a very important point, if the initialization value of the constant needs to change, such as the beginning to set a constant is called Full_mark, first you think full score is only 100 points, but after the program is finished, was told that the full score is 120, so you just need to change the definition of the constant 100 to 120, Instead of having to change every 100 of the code to 120, it not only increases readability, it also increases maintainability.

Don't neglect the code's readability, the code is not just for running, it's more about reading, and the reader is not just yourself, it's probably other programmers, and if you write to the end, you can't see it anymore, that's definitely not a good code.

I hope you can take the code as a work of art to complete, to enjoy the letters from your fingertips to knock out of the pleasure, so as to better reap the smooth running of the program happiness.

At this point, the content of this article will be finished. Dapper series.

Java 0 Basic Starter series DAY4 variables and constants

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.