Java Basics Point (reprinted)

Source: Internet
Author: User
Tags export class

Reprint Please specify source: http://blog.csdn.net/ns_code/article/details/8846697

1, when using floating-point numeric values, the default type is double, followed by F or F is recognized as the float type.

2, before using the array must first open up memory space, of course, you can also directly use static assignment.

3, the basic data type variable in Java is a global variable (specifically, is the domain of the class variables, Java is not a global variable), you can not assign value, direct use, because there is a default value, but as a local variable, it must be assigned to use after the value, For reference data types, either global or local, a default value of NULL is given. In addition, any object or array created with the New keyword is initialized with the default value, and the variable declared with the static keyword is initialized to the default value. For default values, the default value for the Boolean data is false,string, and the default value for the class object is null, and the others are 0,0.0f,0.0, and so on.

4. Any data type that encounters a variable or constant of type string will convert to a string type.

5. Reference data type: Similar to a pointer in C + +, this variable does not allocate memory when declared, and must have additional memory space operations, such as classes and arrays belong to this data type.

6, because the array is a reference data type, so in the array operation, the stack memory is always the access address of the array (that is, the reference, the equivalent of a pointer in C/s + +), only the stack memory space of the array is unusable, must have a point to the heap memory to use, to open up heap memory must use the keyword , and then simply hand over the heap memory to the corresponding stack memory space, and a heap memory space can be pointed at the same time by multiple stack memory space.

7, the method directly called by the Main method, to be declared with the static keyword.

8. The constructor method is called when the object is instantiated with the new keyword.

9, the anonymous object is not explicitly given the name of the object, the general anonymous object is used only once, and the anonymous object only in the heap memory or constant pool open space, and there is no reference to the stack memory.

10, when comparing objects, use "= =", is the reference to the object is compared, and the Equals method, the object's content is compared. When the Equals method is a method in the object class, its default behavior is to compare references, classes in the class library overwrite them in order to compare content, and for custom classes, we need to implement our own methods of covering them as comparisons.

11. The difference between two instantiation methods of the String class:

First, a string is an anonymous object of a string class, and an anonymous object is an object that has opened up memory space and can be used directly.

First type: String str1 = "Hello"

In fact, this kind of instantiation is the use of a constant pool of open memory space to the STR1 object, and this way there is another benefit, that is, if a string has been referenced by a name, then there will be the same string declaration, will not be re-open new space, and continue to use the open space, such as: string str1 = "Hello" and string str2 = "Hello" in the str1 and str2 all point to the same block of constant pool memory. This becomes a "shared design" in Java.

Second type: string str1 = new String ("Hello")

Because a string is an anonymous object of a string class, it has opened up a constant pool of memory space, and if you use the New keyword, no matter how it will open up a new heap memory space, but at this time, the content of this space is "hello", So this instantiation is actually open up two memory space, but the real use of the new keyword only to open up the heap memory space, and the other is a constant pool of garbage space.

12. When working with the string class, it is particularly important to note that the contents of a string cannot be changed once it is declared. In a program, when you change the contents of a String class object, you actually change the reference to the String class object, pointing it at the address of the other string, which is very memory-intensive, so if you encounter changes to the contents of the string object many times in development, Be sure to use the StringBuffer class instead of complete.

13, about reference passing (for reference data types) and value passing (for the base data type) (in fact, in Java there is no reference to pass, all parameter passing is the value of the pass, which is said to refer to pass for easy understanding):

The so-called reference transfer refers to the use of the heap memory space to multiple stack memory space, the parameter type passed by reference can only be class object or array reference data type. When a value is passed, its formal parameter is the basic data type, and when the method is called, the actual argument passes its value to the corresponding formal parameter, and the formal argument initializes its own storage unit with an argument, which is two different storage units (the parameter is just a copy of the value passed in outside the method), so after the method executes, Changes in parameter values do not affect the actual parameter values.

Second, the General object class, in the case of a reference pass (or, to be exact, a value pass, except where the argument in the method is a reference to the object), the operation is valid after modifying the value of the property in the class (although the reference is a copy, but the parameters and arguments point to the same piece of heap memory, so through the formal parameter, which is the reference , changing the information about the instance in the heap memory, which is sure to be saved), changes the value of the attribute in the original object (note that the immutable class of string is an exception, and that the anonymous string instance is stored in a constant pool in the method area).

Third, the General object class, when the reference pass operation, if within the method, the parameter points to other instances, that is, change the value of the reference (note, not the value of the heap in-memory instance), it is obvious that this situation only changes the value of the reference replica in the method, and the value of the argument is the argument reference, Instead of an object instance in the heap) is not affected, that is, the argument or the previous instance.

Iv. passing an array as a formal parameter affects the value of elements within a real parameter group.

14. There are two points to note when using the This () Call construction method:

First, because the construction method is called automatically when instantiating the object, that is, in all methods of the class, only the construction method is the first call, so the constructor with this method must also be placed in the first row of the construction method.

Second, this call construction method must be left a construction method as an exit, that is, there is at least one construction method in the program does not use this drop yo even that other construction method.

15, static related applications:

The property that uses the static keyword is static (shared by all class objects) and can be modified by a static reference to its property value, as a result of which the property value is modified in all objects. The This reference does not exist.

Second, the method of using the static declaration, can be directly referenced by the class name. A method that is not a static declaration can invoke a property or method of a static declaration, and a method that is declared by static cannot invoke a property or method declared by a non-static type. This is not difficult to understand, because all the properties and methods in the program must be called after the object has opened up heap memory, and the static type method can be called by the class name when the object is not instantiated, so if the static method calls the non-static operation directly, It is possible to be called when the property has not been initialized, which is syntactically impossible.

16, the construction block takes precedence over the construction method execution, and the code in the construction block is executed each time the object is instantiated. Static code blocks take precedence over construction method execution, whereas static code blocks defined in a class take precedence over construction block execution, and static code blocks execute only once, regardless of how many objects are produced.

17, Singleton design mode: Define the Singleton class, privatize the construction method, generate the instantiated object of this class with the Static keyword inside the class, and then get the instance of singleton class by static method in this class. The advantage of this is that you can control the generation of instantiated objects, that is, no matter how many singleton objects are declared in the program, there is actually only one instantiation object of the Singleton class, but only a few references to that object.

18. The only advantage of an inner class is that it can be easily accessed as a private property in this class.

First, the static internal class instantiation method: External class. Inner class Inner Class object = new External class. Inner class ();

Second, non-static instantiation of the Inner class method: External class. Inner class Inner Class object = External class instance. New inner class ();

19. The inner class using the static declaration becomes an outer class, that is, externally, it can be called directly from the name of the outer class, but an inner class using static declaration cannot access the non-static property in the outer class. An inner class can also be defined in a method, but the inner class defined in the method cannot directly access the parameters in the method, and if the parameters in the method are to be accessed by an internal class, the parameter must precede the final keyword.

20. Memory areas commonly used in Java:

Stack memory: A variable value that holds the base data type, holds the local variables in the class and the parameters in the method, and holds references to arrays, class objects, and so on.

Heap Memory: Holds the specific property content of each reference data type object, the array or object instantiated by the new keyword, and so on.

Chang in the method area: a constant value that holds the base data type (Final declaration), a string constant value such as the "AVC" string in string str = "AVC", and a specific character such as the method in the code, the name of the class, and so on.

Static storage in the method area: a static variable that holds the static declaration.

V. Eight basic types of Java (Byte short, Integer, Long, Character, Boolean, Float, Double), except for Float and double accidents, the other six have a constant pool, However, they can use a constant pool only if it is greater than or equal to 128 and less than or equal to 127, and if not within this range, the new one will be saved in heap memory.

21. Only single inheritance is allowed in Java, multiple inheritance cannot be used, but multilayer inheritance is allowed.

22. The subclass object will only call the non-parametric construction method in the parent class by default before instantiating it, so the super method must be explicitly added to the constructor method in the subclass to call the parent class.

23, Quilt class Overwrite method can not have more strict access permissions than the parent method, when the method is overwritten, the method that the subclass object calls will be the method of overwriting, the method overwriting from private to default does not calculate the overwrite of the method, This is because the private method is automatically considered the final method, and the subclass is masked, in fact, it is redefining a method, which is not associated with the override of the method. In addition, the domain (that is, the initialization variable) and the static method are not polymorphic, if both the child class and the parent class have the domain and static method, even if an upward transformation occurs, the calling domain or static method is also the parent class, not the subclass of "overwrite" (in fact, is not a overwrite, just a duplicate definition),

24. In inheriting classes, this and super are not allowed to occur at the same time, because both call constructs must be placed in the first row of the constructor method.

25. Final keyword: A class that uses final declaration cannot be inherited, a method that uses final declaration cannot be overwritten, a variable that uses final declaration becomes a constant, cannot be modified, and when a final declaration variable is used, all uppercase letters are required. If a variable in a program uses the public static final declaration, this variable becomes a global constant.

26, the definition of abstract class and the use of rules:

A class that contains an abstract method must be an abstract class.

Both abstract and abstract methods use the abstract keyword declaration

Abstract methods only need to be declared and not implemented.

Abstract classes must inherit from the quilt class, and subclasses (if not abstract classes) must overwrite all abstract methods in the abstract class.

How far abstract classes cannot use the final keyword declaration.

Abstract methods in abstract classes do not use the private declaration.

27, the definition of the interface and the use of rules:

The interface is composed of global constants and public abstract methods.

The abstract method in the interface must be defined as public access, which is absolutely immutable when access is omitted and the method in the interface is the public permission.

Third, the constant in the interface general public static Final declaration, even if not declared, the default is the global constant.

The implementation interface of the 四、一个 class (if not an abstract class) must also overwrite all the abstract methods in the interface.

28, class, interface and other inheritance and implementation of the relationship

The one subclass inherits only one abstract class, but can implement multiple interfaces.

The second interface does not allow inheritance of abstract classes, but allows inheritance of multiple interfaces.

Third, allow an abstract class to implement multiple interfaces.

29. Polymorphism

There are 2 main forms of polymorphism in Java: Overloading and overriding of methods, and polymorphism of objects. The polymorphism of the object is mainly divided into the following two types:

One, upward transformation: Instantiate the parent class object or interface object with a subclass. When calling a method overridden by a quilt class, it is called a overridden method and cannot call a method that is unique in the subclass (that is, not in the parent class or interface), and must be transformed downward if it is to be called.

Second, the downward transformation: The instantiated parent class is coerced into the instantiated object of its child class. An upward transformation of the object must occur before the object's downward transformation.

30. instanceof Keyword: Object instanceof class--Returns a Boolean value that returns True if the object is an instantiated object of that class, otherwise false.

31. The Equals method in the object class is used by default to compare by address, and the content is not compared, and the method to overwrite in the string class is to compare the content.

32. All reference data types can be received using object, including arrays and interface types.

33, finally as an exception of the unified export, so in this statement block writing as far as possible do not appear like throw or return statements, so as to avoid unnecessary problems arise. If return is present in a try statement, the return statement is executed only after the statement in finally is executed.

34, exception in the program must use Try...catch for processing, and runtimeexception can not use try...catch for processing, but if there is an exception, it will be referred to the JVM processing. However, when developing code, it is best to use try...catch for processing.

35. If the methods in a class are all static methods that use static declarations, they can be imported directly using import static, so that when a method in that class is called, it is no longer necessary to use the class. The form of a static method () "can be called directly.

36. Four kinds of access rights:

One, private access rights: Is privately access, can only be accessed in the class.

Second, the default access rights: The access rights, can be accessed by other classes in this package, but cannot be accessed by classes in other packages.

Third, protected access rights: Protected Access, can only be accessed by the classes in this package and subclasses in different packages.

Iv. Public access rights: Common access rights, which can be accessed in all classes.

37 Multi-Threading implementation of two methods: Inherit the thread class, implement the Runnable interface.

First, contact: two ways, regardless of which, eventually must rely on the thread class to start multi-threading, and in the thread class, the Run method is called the Run method in the Runnable interface.

Two, difference: If a class inherits thread, it is not suitable for multiple threads to share resources, because each thread class's instantiated object can only correspond and start a threads, and implement Runnable interface, can easily realize the sharing of resources, Because the instantiated object for each Runnalbe interface can be passed in as a parameter to multiple thread objects, it starts multiple threads with an instantiated object of a runnable interface.

38, because of the uncertainty of the thread operation, so the main thread can be executed first, then the other threads will not be affected at this time, and will not end with the end of the main thread.

39, Thread synchronization: That is, multiple operations in the same time period of only one thread, and other threads to wait for the thread to finish before it can continue execution. If you add the Synchronized keyword to a normal block of code, this code block is called a synchronous code block, and when you use a synchronous block of code, you must specify an object that needs to be synchronized (typically an instantiated object of the Class). If you precede the method with the Synchronized keyword, this method is called the synchronization method.

40, multi-threaded development can be set to stop the operation of a thread by setting a flag, generally do not recommend the use of the thread class suspend, resume, stop and other methods, because these three methods in operation will produce a deadlock problem. 、

41. In a reference pass, you can set the range upper and lower bounds of a generic object in a generic operation. Range caps Use the Extends keyword declaration, which indicates that the type of the pattern may be the type specified or a subclass of this type, and that the lower bound is declared with super, which indicates that the type of the generic may be the type specified, or the parent type of this type, or the object class.

42. A subclass of a class can be instantiated for its parent class through object polymorphism, but in a generic operation, the generic type of the subclass cannot be received using the generic type of the parent class, for example:info<string> cannot be received using info<object>. It is therefore not possible to make an upward transition when the parameter is passed, but the restriction can be resolved with wildcards, that is, you can accept arbitrary types of generics by using parameters like info<?> in the parameters of the method.

43. Java allows any basic data type to be converted to another base data type, except the Boolean type, which does not allow any type of conversion processing at all, unlike in C + +.

44. If the constructor of a class is not explicitly defined, the compiler automatically creates a default parameterless constructor, which is called by default when an instance object of the class is created, and if a constructor (with or without parameters) is explicitly defined, the compiler does not automatically create a default constructor.

45, the Java program initialization of the book is a static object, after the non-static object, and static initialization only when the class object is loaded for the first time.

46. For class access, there are only two choices: Package access (default) or public (an inner class can be private or protected, but then a special case). If you do not want anyone else to have access to the class, you can designate all of the constructors as private, so that no one is allowed to create objects of that class.

47, you can create a main method for each class. This technique makes unit tests for each class easy and can be left to the next Test after the unit test is complete without removing the main method. When a program contains multiple classes , only the tired main method called by the command line is called, and the class is not public.

48. Final modified data is a compile-time constant and must be assigned to final with an expression at the domain definition or in each constructor . Final is used on the base data type to ensure that its value remains the same, and when the object is decorated with final, it guarantees that the reference is constant, and once the reference is initialized to an object, it cannot be changed to point to another object. All private methods in a class are implicitly specified as final.

49. The load of a class typically occurs when the first object of the class is created, but also when the static or static method is accessed (the constructor is also the static method, although the static keyword is not explicitly written. ).

50, binding refers to the invocation of a method associated with the class in which the method resides (the method body). For Java, bindings are divided into static bindings and dynamic bindings.

Static binding: The method is already bound before the program executes, and is implemented by the compiler or other linker. For Java simple can be understood as the program compile time of the binding; In particular, the Java method is only final,static,private and the constructor method is pre-binding.

Second, late binding: binds at run time according to the type of the specific object. In Java, almost all methods are late-bound.

51, a complex object call constructor to follow the following order:

1) Call the parent class constructor. This step is repeated recursively, first of all to construct the root of this hierarchy, then the next layer of subclasses, and so on, until the lowest layer of the export class.

2) Invoke the initialization method of the member in the Order of Declaration.

3) Call the body of the subclass constructor.

Java Basics Point (reprinted)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.