What is a variable?
Variable, as the name implies, is the amount of change--
All right, give me a chestnut.
The various tableware on the picture is a variable, because the same plate can be different at different times, the table can be loaded with shredded potatoes, the next table can be filled with fried cucumber ( of course, this plate should be washed clean drops).
So what are the main points of the variables in Java, and what are their syntax?
Let's write a piece of code to save the fried cucumber on top of it ...
| 1 |
String pan9527 = "清炒黄瓜"; |
This is the usage of the standard variable, its syntax format: data type variable name = value;
Three points:
1. Data type
Data types in Java fall into two main categories: value types, reference types.
value types include:
1) integer type: Byte, short, int, long
2) Float type: float, double
3) Character type: Char
4) Boolean type: Boolean
There are a lot of reference types, the most common one:
1) Strings: string
These types of differences and so on are not detailed, or become a waiver of the article ·
Why should there be so many types? Very simple, because need AH!!!
Look at that picture above, we'll put rice in our bowls, dish with dishes, soup with bowls or pots, and a bamboo bowl with a cake. There are also many types of tableware, you Sir can think about it, if you want to use a cake of bamboo bowl soup is what effect? Conditional can go home to try, hey hehe ...
Give me another chestnut, for example, we want to keep an employee's information:
| 1234567 |
publicstaticvoidmain(String[] args){ String name = "李大爷"; //姓名 intsalary = 8600; //薪水 charsex = ‘女‘; //性别 doubleheight = 1.83; //身高 booleanisDangYuan = false; //是否党员 } |
Different information is saved using different types.
2. Variable name
Naming rules for variable names:
1) can only be preceded by letters and underscores (_)
2) consists of letters, numbers, underscores (_), and dollar signs ($)
3) cannot be a Java keyword or a reserved word ( The simplest way to differentiate: an int or if or for a color-changing word cannot be used as a variable name).
Naming conventions for variables: to make sense, such as saving names, it's best to use name instead of N1 or XM or any other combination of characters that you can't see well-known.
rules are like the laws of the real , the violation of the end of the (there will be a syntax error in Java, the program can not be executed), the specification is tantamount to the moral of the reality , the violation will not put you in prison, at most will be beaten to a meal ( If you save the name of the variable to be called ABC, the program can run as normal, but other programmers see you will feel that you are not moral). So we must be a law-abiding and four graces and programmer!!!
3. Value
Assigning values to variables is a matter of compatibility, such as the int type cannot be assigned to "haha". For the specific assignment syntax, look at the example that holds the employee information above.
Summarize:
characters, floats, and integers are numeric types . They can be converted, except that they have a different range of values.
| 123456 |
publicstaticvoidmain(String[] args){ char c = ‘哈‘; //最小 inti = c; //稍大 floatf = i; //较大 doubled = f; //最大 } |
In this case, put the small value into the large type, which we call the automatic type conversion.
For example, we can transfer the water from a 500cc bottle to a 2000cc bowl is sure to be fine, but the 2000cc Bowl (full) of water in a 500cc bottle will definitely overflow.
| 123456 |
publicstaticvoidmain(String[] args){ doubled = 86.5; floatf = (float)d; inti = (int)d; charc = (char)i; } |
So if you put large types of data into small types, you have to cast them like this (that is, throw away the extra parts ...).
Finally, the string type and other types of conversions.
| 12345678910 |
publicstaticvoid main(String[] args){ String s = "10"; //字符串类型转数值类型最常用的方式 inti = Integer.parseInt(s); //数值型转字符串类型最常用的有种方式 String s1 = String.valueOf(i); String s2 = Integer.toString(i); String s3 = ""+ i; } |
Let's introduce a little bit of content today, the more detailed content, you crossing download a java_api_1.x version of the Help manual can be done.
"Software Thinking" blog address:51CTO, Blog Park , interested in small partners can go to see the relevant other blog posts.
Java from getting started to giving up: variables