Java Basics !!!, Java Basics

Source: Internet
Author: User

Java Basics !!!, Java Basics
Question 1: What are the results of 0.01 + 0.09?

public class MathTest{ public static void main(String[]args){ double a = 0.01, b = 0.09; System.out.print(a+b); }}

Why is the printed result 0.09999999999999999 instead of 0.1, and the loss of precision occurs? Both double and float will encounter this problem. Let's talk about float. The 0.1 binary form of float is 00111101110011001100110011001101, the exact value expressed in decimal notation Based on the symbol bit should be calculated in this way, the negative 27 power is multiplied by 110011001100110011001101 by 2, and the actual value is 0.100000001490116119384765625, resulting in an actual error.

Example

public float getLeftMoney() throws Exception {// TODO Auto-generated method stubfloat m = new MoneyDaoImpl().CountAllMoney();float c = new DetailsDaoImpl().countDetailsMoney();flaot less = m-c;System.out.println(m);System.out.println(c);System.out.println(less);return less;}

Solution:

public float getLeftMoney() throws Exception { // TODO Auto-generated method stub float m = new MoneyDaoImpl().CountAllMoney(); float c = new DetailsDaoImpl().countDetailsMoney(); BigDecimal b1 = new BigDecimal(Float.toString(m)); BigDecimal b2 = new BigDecimal(Float.toString(c)); System.out.println(m); System.out.println(c); Float less = b1.subtract(b2).floatValue(); System.out.println(less); return less;}

 

Question 2: What type does Null belong?

Solution:

Question 3: What is the difference between heap memory and stack memory? Solution:

① Heep (heep) is a dynamically applied memory space. Generally, all created objects are stored here. Stack is an advanced data structure. It is usually used to save parameters and local variables in methods (functions. Stack has a small space, but the speed is relatively high. It stores object references. You can find objects in the stack through the address index in the stack.
② The stack (java stacks) is also thread-proprietary, and its lifecycle is the same as that of the thread. The Virtual Machine stack describes the Memory Model of java method execution. When each method is executed, a stack is created to store information such as the local variable table, Operation stack, dynamic link, and method exit. The process of calling each method until execution is complete corresponds to the process of a stack frame in the VM Stack from the inbound stack to the outbound stack.
Heap is the largest memory managed by java virtual machines. Java heap is a memory area shared by all threads. It is created when the VM is started. The only purpose of this memory area is to store object instances, where almost all object instances allocate memory.

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

Solution:

① A Class variable is also called a static variable, that is, a static variable is added before the variable. The class variable already exists in the memory before the object is created and is created along with the class creation; instance variables are also called object variables, that is, variables without static.
② Class variables are shared by all objects. One of the objects changes its value, and other objects get the changed result. The instance variables are private to the object, an object changes its value without affecting other objects.
③ All instance objects share a class variable. Only one space in the memory is included in the class variable value. Therefore, if an object changes the value of a class variable, and another object obtains the value of a class variable, it is changed. When creating an instance object, the memory will open up a memory space for each non-static member variable of each instance object to store all the non-static member variable values of this object, even if two different instance objects belong to the same class, their non-static member variables with the same name occupy different space in the memory.

Public class A {static int a = 0; // class variable private int B = 0; // instance variable}
Public class B {public void main (String [] args) {A a1 = new A (); A a2 = new A (); a1.a = 3; // equivalent to. a = 3; a1. B = 4; System. out. println (a2.a); // The result is 3 // class variables are for all objects, so a1 changes a and a2's a also changes System. out. println (a2. B); // The result is 0 // The instance only changes itself, so the B change of the a1 object does not affect the B variable of the a2 object }}
Question 5: What is the difference between heavy load and rewrite?

Solution:

① Overload indicates that multiple methods with the same name can exist in the same class, but the parameter lists of these methods are different (that is, the number or type of parameters are different ).

Condition: A) the method name must be the same (the case must be the same). B. The parameter list is different and divided into: the number of parameter lists is different; the order of the parameter list is different. The data type of the parameter list is different. Method Overloading is required only when both conditions A and B are met.

Note:

  • You can only use different parameter styles when using overload. For example, different parameter types, different parameter numbers, and different parameter Order (of course, several parameter types in the same method must be different, such as fun (int, float ), but cannot be fun (int, int ));
  • It cannot be overloaded by access permission, return type, or thrown exception;
  • The method exception type and quantity do not affect the overload;
  • For inheritance, if a method has the priavte access permission in the parent class, it cannot be overloaded in the subclass. If it is defined, it only defines a new method, this will not achieve the effect of heavy load.

② Override indicates that the method in the subclass can have the same name and parameter as a method in the parent class. When this method is called through the instance object created by the subclass, the definition method in the subclass is called, this is equivalent to overwriting the exactly same method defined in the parent class, which is also a manifestation of object-oriented programming polymorphism. When a subclass overwrites the method of the parent class, it can only throw fewer exceptions than the parent class, or throw the child exception thrown by the parent class, because the subclass can solve some problems of the parent class, there cannot be more problems than the parent class. The access permission of the subclass method can only be larger than that of the parent class, and cannot be smaller. If the method of the parent class is of the private type, there is no overwrite restriction on the subclass, which is equivalent to adding a new method to the subclass.

Pay attention to the following points during coverage: A) the logo of the covered method must match the logo of the covered method to achieve coverage. B) the returned value of the overwritten method must be the same as the returned value of the overwritten method. C) The exception thrown by the overwritten method must be the same as that thrown by the overwritten method, or its subclass; d) the method to be overwritten cannot be private. Otherwise, a new method is defined in its subclass and not overwritten.

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.