当前位置:网站首页>Day06 class variables instance variables local variables constant variables naming conventions
Day06 class variables instance variables local variables constant variables naming conventions
2022-07-05 07:36:00 【33 year old Java enthusiast】
Variable
Variable is the most basic storage unit in a program ,
Its elements include :
- Variable name
- Variable type
- Scope
Notes on declaring variables :
- Variable types must be declared , It can be a basic type , It can also be a reference type
- There must be a variable name , The identifier of variable name must be legal ,$ _ Start with upper and lower case letters . Cannot contain keywords .
- Declared variables must have values .
- Variable declarations are complete statements , So every statement needs ; ending
- Within the same function , Variable names are unique .
public class demo{
public static void main(String[] args){
byte a1 =127; // perhaps -128
short a2 =32767; // perhaps -32768
char c1 ='a' // According to the character , Single quotation marks
int a3 = 2147483647; // perhaps -2147483648
long a4 = 9223372036854775807L;// perhaps -9223372036854775808. Pay attention to add L Follow int distinguish
float a5 = 3.14F; // Pay attention to add F Follow double distinguish
double a6 = 3.14;//
}
}
public class demo_day3 {
public static void main(String[] args) {
//int a,b,c; // Although it can be written like this , But not recommended
int a=1;
String name =" Kaiwei ";
System.out.println(name);
}
}
Three variable forms : Class variables , Instance variables , local variable
- Class variables . On the method , It can be used directly in the method
- Instance variables . On the method , Class must be introduced , Can be used
- local variable . In the method , Direct declaration can be quoted
// Three variables , Class variables , Instance variables , local variable
public class demo_day3 {
static double salary = 20;// Class variables , You can reference directly in the method
String name = "kaiwei" ;// Instance variables , You must reference variables before you can use them. Understand
public static void main(String[] args) {
int a1 = 20;// Reference local variables
System.out.println(a1);
demo_day3 Baby =new demo_day3();// Reference instance variable , I'm not familiar with it for the time being. Just know how to write it
System.out.println(Baby.name);
System.out.println(salary);// Reference class variables
}
The default value of uninitialized variables
The number will return 0
The string returns null
Boolean values will return false
Constant
Understood as a special variable , After setting the value , It is not allowed to be changed while the program is running
final double HIGHT = 300
public class demo_day3 {
static final double HIGHT = 400; // Declare class variables One high is 400 The constant
// final static double HIGHT =400; // The meaning is the same as above ,static It's a modifier , It doesn't matter before or after
public static void main(String[] args) {
System.out.println(HIGHT);
}
}
Variable naming conventions
All constants , Variable , Class name Must see the name to know the meaning . Replace with English words
- Class member variable : First character lowercase and hump principle .monthSalary For example, monthly salary
- local variable : First character lowercase and hump principle .
- Constant : Capital letters and underscores .MAX_VALUE
- Class name : Capital letters and hump principle .Hello
- Method name : Lowercase and hump principle .run(),runRun()
Streamlining
Constants are all uppercase .
Class names begin with uppercase
Members of the class Follow Method name local variable equally The first letter Hump principle
边栏推荐
- What is deep learning?
- CADD课程学习(6)-- 获得已有的虚拟化合物库(Drugbank、ZINC)
- Batch convert txt to excel format
- Miracast技术详解(一):Wi-Fi Display
- Don't confuse the use difference between series / and / *
- 苏打粉是什么?
- Raspberry pie 4B arm platform aarch64 PIP installation pytorch
- Apple terminal skills
- Database SQL practice 3. Find the current salary details of the current leaders of each department and their corresponding department number Dept_ no
- Cookie operation
猜你喜欢
Rough notes of C language (2) -- constants
Daily Practice:Codeforces Round #794 (Div. 2)(A~D)
Shadowless cloud desktop - online computer
How to deal with excessive memory occupation of idea and Google browser
Close of office 365 reading
Unforgettable summary of 2021
What is Bezier curve? How to draw third-order Bezier curve with canvas?
Play with grpc - go deep into concepts and principles
Butterfly theme beautification - Page frosted glass effect
Ugnx12.0 initialization crash, initialization error (-15)
随机推荐
【idea】Could not autowire. No beans of xxx type found
纯碱是做什么的?
氫氧化鈉是什麼?
611. Number of effective triangles
Leetcode solution - number of islands
Course learning accumulation ppt
UE5热更新-远端服务器自动下载和版本检测(SimpleHotUpdate)
The number of occurrences of numbers in the offer 56 array (XOR)
selenium 元素定位
Detour of Tkinter picture scaling
(tool use) how to make the system automatically match and associate to database fields by importing MySQL from idea and writing SQL statements
并查集理论讲解和代码实现
Basic series of SHEL script (III) for while loop
Clickhouse database installation deployment and remote IP access
Database SQL practice 3. Find the current salary details of the current leaders of each department and their corresponding department number Dept_ no
deepin 20 kivy unable to get a window, abort
The golang timer uses the stepped pit: the timer is executed once a day
Line test -- data analysis -- FB -- teacher Gao Zhao
R language learning notes 1
I can't stand the common annotations of idea anymore