This time there are two themes, the first one is too simple, is the naming rules of variables in Java, pure memory things. The second topic is to discuss a point of note for object reference variables.
If you are familiar with the naming rules, look directly at the second one. (The picture above is a little bit chaotic.)
I. Naming rules for variables in Java
The naming conventions of most languages are similar, with only a few minor differences.
- The first position of the variable can be a letter, an underscore (_), a dollar symbol ($) (this is not possible in C/c++,python)(note: It can't be a number oh, you know why Mulao)
- Other positions can be numbers, letters, underscores
- Cannot use keywords in Java
The keywords in Java are:
Boolean byte char double float int long short public Private
Protected abstract final native static strictfp synchronized transient volatile if
else do and switch case default for break continue assert
Class extends implements import Instanceof Interface new Package Super this
Catch finally try throw throws return void const goto enum
There are so many naming conventions in Java, and in addition to the naming conventions, variables should be noted in naming conventions.
Second, the object reference variable attention
Basic variables do not have this problem, through code to introduce problems.
1 Public classTest {2 Public Static voidMain (string[] args) {3 intI1 = 10;4 intI2 =I1;5I1 = 10000;6System.out.println ("I1 =" +i1);7System.out.println ("i2 =" +i2);8 }9}
Let's predict the results. OK, as you expected:
Look at a piece of code and if the predictions are correct, don't look down.
1 Public classTest {2 Public Static voidMain (string[] args) {3Test T =NewTest ();4Test.dog D1 = T.NewDog (1);//If you don't understand, just think of creating an instance variable of a dog class5Test.dog D2 = d1;//The D1 is assigned a D2, at this time D2 age is not 1? 6D1.age = 10;//re-assigns the age property of the D1 to a value of ten7 //Print Results8System.out.println ("Dog1 ' s age =" +d1.age);9System.out.println ("dog2 ' s age =" +d2.age);Ten } One A //in order to save space to see the end, this is written in the inner class - classDog { - //In order to facilitate the definition of properties as public, it is better not to actually the Public intAge ; - PublicDog (intAge ) { - This. Age =Age ; - } + - } +}
Guess the result:
Guess yes no, why hasn't the change of D2 's AGE,D2 age changed from 1 to 10?
The underlying variable is the actual value, and the object reference variable is one that can find the actual worth of data (can be considered as the address in the C language), so when D1 assignment D2, only the data that can find the actual object is assigned to D2, only one instance of dog in memory, d1,d2 points to the same variable.
So, when D1 changes the value of object, D2 points to the same object, so the result is self-evident.
This is not enough to say, want to know more, please look for books.
Challenges is interesting and overcoming them was what makes lifes meaningful.
A pit of variable naming and referencing variables in Java