Java is a new generation of programming languages developed by Sun's development. Its biggest feature is to develop software in a wide variety of different machines, different operating platform network environments, with portability, and high security. Sex, simple, unrelated to the architecture, dynamic implementation, etc., Java is gradually become the main development language of the Internet application. Today we come to discuss the concept of objects in Java language.
Everything is object: ??? Java in programming language design, completely assumes that the user is designed to be in the object-oriented mode, in Java, there is a very important concept: all things are object, even if the Java program itself, Also an object.
1. Control the object's reference: ??? relationship between Reference and objects, comparable to TV and remote control, you can have a referenc, but not to connect to an object if you want to generate a string reasoner: for example:: String s; you have a Reference, but don't produce an actual object. At this time, you want to connect it to a newly generated object, you can complete it through New. For example: string s = new string ("ASDF")
2.Class: ????? In fact, things you need to do in Java are nothing more than: Define Class, generate objects, send messages to objects. If everything is an object, in order to solve the feature of a type of object, we need to define it, this is Class. For example: Class atypename {/ * class body * /} ???????????? atypename a = new atypename (); To specify the value of its data idiom, you can specify the name of the member, the name of the member: for example:
Class Dateonly
{
?? int i;
?? float f;
?? Boolean B;
}
Dateonly d = new dateonly ();
D.i = 47;
D.f = 1.1f;
D.B = false;
???? Different from C , in Java, when the variable is the basic type (Primitive Type) in the class, Java will guarantee that 100% have a provincial default, but if the variable is just a function definition area When the local variable, the variable will not be initialized, you need to give him an appropriate value, if you forget, the Java compiler will be compiled, and the C compiler will only have an uninitialized variable. Give a warning.
3. Reference Components (import): ????? For example: import java.until.arrayList means you want to use ArrayList Class, you can use * to replace ArrayList to avoid distinguishing one by one (although convenient, but affect compilation time)
4. "Shepherd" function static: ??? In Java, in general, you need an object, and then use the object to use its data and functions, but if a function or data is declared as static. It will not limit the Class Object of the required, even if there is no Object, the outside world can still call its Static function. ???? ?E.g:
Class StaticTest {Static Int i = 47;}
StaticTest St = New StaticTest (); here St.i and Statictest.i the same effect, they collectively point to a memory.
Class staticfun
{
?? static void incr () {stitictest.i }
}
StaticFun sf = new staticfun ();
Whether it is through the object ---- sf.incr () or directly through the Class --- StaticFun, INCR () calls is the same.
Some features of Java relative to C / C : ????? Java and C are Hybird Language, and the foundation of Java and C , but Java is more "pure" on OOP, C for Compatible and C, including many properties that are not suitable in C , make C more complicated in some ways.
1. Array: ????? In C / C , the array is actually a fast memory. If you have access to a region outside of the array block or use it before initialization, it will cause many errors. occur. Unlike C / C , Java pays more attention to safety, which guarantees that the array will be initialized (when generating the array of References, each References will be set to Null This special value, Java will treat it as "Don't point to any object." Before using any Reference, you must first assign an object to it) and make each array out of one more space, and check the alignment index value on the execution period, thus Programmers cannot exceed the range.
2. Space (Scoping): C / C has the ability to cover up variables in larger living space: for example:
{
?? INT x = 1;
???? {
?????? int x = 2;
????}
}
Even if the above program is legal in C / C , Java can't do this, the compiler will think that X has been defined, Java believes that this is easy to cause misunderstanding and confusion of the program. In C , after using the object, it must be destroyed, otherwise it may breed a bug, and when is it the best time? The Java provides solutions around the life of variable life. There is a so-called (garbage collector) Garbage Collector, a mechanism in Java, which will review all the objects generated by new, when there is any reference pointing to them. When the memory of these objects will be released so that the programmer does not have to worry about the problem of memory recycling, because all objects will disappear automatically when you don't need them.