There are two types of variables in a Java language: 1. Member variable 2. Local variables.
There are two types of data: 1 value types, the basic type, a total of four classes of eight, Boolean, Char, Byte, short, int, long, float, and double;2 reference types, which can be likened to pointers in C + +. However, there is no concept of pointers in Java, and it does not allow memory space such as the Operation stack shown by the program ape, to some extent avoids the memory leaks caused by improper pointer operation in C + +.
Learning Java, the object-oriented language, is essentially creating classes and instantiating classes into objects and invoking methods.
Four types of declaration and the creation of objects is for the reference type mentioned in the data type, the following main introduction of the object of the class declaration and creation, combined with their own understanding of the stack knowledge, there may be inappropriate or wrong place, if the reader can see also hope to help point out, in this first thanked.
In Java, the object to use the class generally takes two steps, that is, the Declaration and creation of the object. The following creates a person class to help explain the problem.
1 Public classperson{2 //member Variables3 String name;4 intAge ;5 String sex;6 7 8 //Method9 Public voidspeak () {TenSystem.out.println ("Then I have meat to eat"); One } A - Public voidrun () { -System.out.println ("Man is running ....") "); the } - } - - + Public classTest { - + /** A * @paramargs at */ - Public Static voidMain (string[] args) { - //TODO auto-generated Method Stub -person P1 =NewPerson (); -person P2 =NewPerson (); - inP1.name = "Jim"; -P2.name = "Kite"; to +P1 =P2; -P1.name = "Sheldon"; the * intA = 10; $ intb = 5;Panax NotoginsengA =b; -A = 3; the + System.out.println (p1.name); A System.out.println (p2.name); the System.out.println (a); + System.out.println (b); - $ $ } - -}
First, the person p1=new the person (); The statement actually completes the Declaration and creation of the object at the same time, which can be written separately: 1 person p1; 2 p1=new Person (); Declaring an object only declares a variable of pointer type in the stack, does not store a specific value in memory, but only stores the address of memory in another heap. Create objects in Java generally with the new keyword, p1=new person (); This line of code does two things: the first is to allocate a piece of memory in the heap that holds the student's specific value, and the second is to assign the first address of the memory to the pointer variable declared above. Below we can access the specific properties by the object name. Property name. Objects must be created before they can be used, and if only declarations are not created, the null pointer exception (NullPointerException) is reported when the object properties and methods are called.
Five combine the above code to illustrate a problem. The above code is executed and output separately: Sheldon Sheldon 3 5. The following is a thought analysis:
1 Analyze the basic data type first. First is the assignment operation, 10 assigns the value to the a,5 to B, at this time B's literal value is 5,a the literal value is 10. Then B (literal value =5) is assigned to a, this step is a=5,b=5 after the operation. Finally, 3 is assigned to a, at this time a=3,b=5. This is a continuous assignment of the basic data type, which is well understood.
2 Next parses the assignment operation of the reference data type. The drawing of two figures is perfectly clear.
OK, the time is late, wash and sleep.
A study of the essays written in Java