In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you what the Java variable declaration and scope is, the content is simple and clear, and I hope it can help you solve your doubts. Let me take you to study and learn what the Java variable declaration and scope is.
1 preface
Variable is a concept in computer language, which can represent a specific value, and this value can be changed, so it is called a variable. Corresponding to the constant, the constant is also used to represent a certain value, but the value is fixed and cannot be changed.
Declaration of 2 variables
To declare a variable is to tell the compiler the data type of the variable so that the compiler knows how much space needs to be allocated for it and what kind of data it can store. In the process of running the program, the value in the space changes, and this memory space is called a variable. For ease of operation, give the space a name, called the variable name.
Syntax:
Data type variable name [= initial value] [, variable name [= initial value].]
/ / declare a variable of integer type, the variable name is ageint age;// to assign the variable, the variable name is age, the value is 18age = 18 suffix / data type variable name [, variable name.] int age, age1;// data type variable name [= initial value] [, variable name [= initial value].] int age = 20, age1 = 21
Variable names should be defined in accordance with the rules, can contain numbers, letters, underscores, $, cannot contain spaces, operators, cannot be named with pure keywords, cannot begin with numbers, can be mixed with uppercase and lowercase letters, and capitalize the first letters of subsequent words. The naming of variables is not arbitrary, and the following rules should be followed:
The variable name must be a valid identifier (with variable name and data type).
You cannot use keywords in Java for variable names.
The variable name cannot be duplicated.
Counterexample: naming in the code cannot begin with an underscore or a dollar sign, nor can it end with an underscore or a dollar sign.
Scope of _ name / _ name / $Object / name_ / name$ / Object$3 variable
Scope refers to the scope of action, within which the variable is valid.
Because the variable is only temporarily stored in memory after being defined, the variable will be released when the program is executed to a certain point, that is, the variable has its life cycle. Therefore, the valid scope of a variable refers to the area where the program code can access the variable, beyond which an error will occur at compile time. In a program, variables are generally divided into "member variables" and "local variables" according to their "valid range".
/ * this is a test class * / public class TestVar {/ / member variable int age1 = 18; / / this is a method public static void main (String [] args) {/ / local variable int age2 = 20; System.out.println (age1) } / / this is a method public void hello () {/ / variable age1 does not exceed scope System.out.println (age1); / / variable age2 is out of scope, compilation error System.out.println (age2);}}
Member variables: defined in the class, outside the method
Local variables: defined in method
The scope of a variable is to find the nearest {} to determine whether it is a member variable or a local variable.
4 Summary
A variable is actually a storage space in memory that is used to store data. So, declare a variable = the opening up of the variable space, the data type of the variable = what data to store in the space, the name of the variable = what the name of the space is, and the value of the variable = what the data in the space is.
What are the commonly used java frameworks? 1. Spring Web Web MVC is a Java-based lightweight Web framework that implements the request-driven type of Spring design pattern. 2. Shirojia Apache Shiro is a security framework for Java. 3. Mybatistion MyBatis is an excellent persistence layer framework that supports normal SQL queries, stored procedures and advanced mapping. 4. DubboPol Dubbo is a distributed service framework. 5. Mavenfine Maven is a project management and build automation tool. 6. RabbitMQ _ RabbitMQ is a highly concurrent and reliable AMQP message queuing server implemented with Erlang. 7. EhcacheJournal EhCache is a pure Java in-process caching framework.
The above is about "what is the declaration and scope of Java variables". If this article is helpful to you and think it is well written, please share it with your friends to learn new knowledge. if you want to know more about it, please pay more attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.