Java Foundation Troubleshoot!!!

Source: Internet
Author: User

Question one: The result of 0.01+0.09?
 Public class  publicstaticvoid Double A = 0.01, B = 0.09; System.out.print (a+B); }}

Why the result of printing is 0.09999999999999999 instead of 0.1, the loss of precision, whether double or float will encounter this problem, take float to speak, float 0.1 binary form is 0 0111101110011001100110011001101, the value expressed according to the symbol bit conversion to 10 is exactly what should be calculated 110011001100110011001101 multiplied by 2 minus 27 times, The actual value is 0.100000001490116119384765625 so that the actual error is generated.

Cases

 Public float throws Exception {//  TODO auto-generated method stubfloatnew  Moneydaoimpl ( ). Countallmoney (); float New  = m-c; System.out.println (m); System.out.println (c); System.out.println (less); return Less ;}

Workaround:

 Public floatGetleftmoney ()throwsException {//TODO auto-generated Method Stub floatm =NewMoneydaoimpl (). Countallmoney (); floatc =NewDetailsdaoimpl (). Countdetailsmoney (); BigDecimal B1=NewBigDecimal (Float.tostring (m)); BigDecimal B2=NewBigDecimal (Float.tostring (c)); System.out.println (m); System.out.println (c); Float Less=b1.subtract (B2). Floatvalue (); System.out.println (less); returnLess ;}

Question two: What type does Null belong to?

Solution:

    1. 4 system-defined constants in Java: NaN non-numeric, lnf infinity,-lnf negative infinity, null empty.
    2. Null is used to identify an indeterminate object that can be assigned to a reference variable and cannot be assigned to a primitive type variable
    3. Object is a superclass of all classes known to exist, but does not contain non-existent classes and does not contain null. The base data type is not a class that is not contained in Object.
Question three: What is the difference between heap memory and stack memory? Solution:

①heep (Heap) is a dynamically applied memory space where all created objects are normally placed. Stack (stack) is an advanced data structure, usually used to hold parameters in a method (function), local variables. Stack (stack) space is small, but faster, storing the object's reference, through the stack of address index can find objects in the heap.
The ② stack (Java stacks) is also thread-private, with the same life cycle as the thread. The virtual machine stack describes the memory model that is executed by the Java method: Each time a method is executed, a stack is created to store the local variable table, the Operation Stack, the dynamic link, the method exit, and so on. Each method is called until the completion of the process, corresponding to a stack frame in the virtual machine stack from the stack to the process of the stack.
The heap (Java heap) is the largest piece of memory managed by a Java virtual machine. The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started. The only purpose of this area of memory is to hold object instances where almost all of the object instances are allocated memory.

Question four: What is an instance variable? What is a class variable

Solution:

The ① class variable is also called a static variable, that is, a variable that has been added in front of the variable, the class variable exists in memory before the object is created, created with the class, and the instance variable is also called an object variable, that is, a variable without static.
The ② class variable is common to all objects, where one object changes its value, the other object gets the changed result, and the instance variable is the object's private, and an object changes its value without affecting other objects.
③ all instance objects share a class variable, there is only one space in memory where the class variable value is placed. Therefore, if an object changes the value of a class variable, the other object takes the value of the class variable again after it has been changed. When creating an instance object, memory will open up a memory space for each non-static member variable of each instance object to store all non-static member variable values for that object, even if two different instance objects belong to the same class. But their non-static member variables with the same name occupy a different amount of space in memory.

 Public class  staticint// class variable privateint// instance variable }
Publicclassb{ Public voidMain (string[] args) {A A1=NewA (); A A2=NewA (); a1.a= 3;//equivalent to A.A = 3;a1.b = 4 ; System.out.println (a2.a); //result is 3//The class variable is for all objects, so A1 changes A, a2 a also changesSystem.out.println (a2.b);//result is 0//The instance only changes itself, so the B change of the A1 object does not affect the B variable of the object A2 }}
Question five: What is the difference between overloading and rewriting?

Solution:

① overloaded overload means that there can be more than one method with the same name in the same class, but these methods have different parameter lists (that is, the number of arguments or the type differs).

Condition: A) The method name must be the same (the case must be the same) B) The parameter list is different, the parameter list is different: The number of parameter list is different; The parameter list has different order; The data type of the parameter list is different. Overloads that must satisfy both the condition A and B are called methods.

Attention:

    • You can only pass different parameter styles when using overloads. For example, different parameter types, different number of parameters, different parameter order (of course, several parameter types within the same method must not be the same, such as can be Fun (int,float), but not fun (int,int));
    • cannot be overloaded by Access permission, return type, thrown exception;
    • The exception type and number of methods do not affect overloading;
    • For inheritance, if a method is priavte in the parent class, it cannot be overloaded in subclasses, and if defined, it simply defines a new method without overloading the effect.

② override override means that a method in a subclass can be exactly the same as the name and argument of a method in the parent class, and when called by the instance object created by the subclass, the definition method in the subclass is called, which is equivalent to overwriting the exact same method defined in the parent class. This is also a representation of the polymorphism of object-oriented programming. When a subclass overrides a method of a parent class, it can only throw fewer exceptions than the parent class, or a child exception that throws an exception thrown by the parent class, because subclasses can solve some problems with the parent class and cannot have more problems than the parent class. The subclass method can only be accessed more than the parent class, and cannot be smaller. If the method of the parent class is private, then the subclass does not have an override limit, which is equivalent to adding a new method to the subclass.

Notice the following points in the overlay: A) The flags of the covered methods must match the flags of the overridden methods to achieve the effect of coverage; B) The return value of the overridden method must be the same as the return of the overridden method; C) The exception that is thrown by the overridden method must be the same as the exception that is thrown by the overridden method. or its subclasses; D) The overridden method cannot be private, otherwise only a new method is defined in its subclass and is not overwritten.

Java Foundation Troubleshoot!!!

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.