Java Foundation Improvement Chapter

Source: Internet
Author: User

One, rounding in Java

 Public Static void Test () {        System.out.println (Math.Round (12.5d));        System.out.println (Math.Round (-12.5d));         // using the banker algorithm         BigDecimal d=New BigDecimal (100000);        BigDecimal r=new BigDecimal (0.0187*3);        BigDecimal I=d.multiply (R). Setscale (2, roundingmode.half_even);        System.out.println (i);}

The results of the operation are as follows:

13
-12
5610.00

Second, deep copy and shallow copy of Java

Class implements the Clone method in object after implementing the Cloneable interface, Super.clone () implements the copy. However, you will find that only the base type is copied.

The copy rules are as follows:

1. If the variable is a basic type, copy its value.

2. Object copy its address reference

Shallow copy is not conducive to the use of objects, in order to allow the object to create a new solution can be in the Clone method of a new object.

To implement a copy of an object using serialization:

ImportJava.io.ByteArrayInputStream;ImportJava.io.ByteArrayOutputStream;Importjava.io.IOException;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream;Importjava.io.Serializable; Public classCloneutil {//the object of the tool must implement the Serializable interface     Public Static<textendsSerializable>T clone (T obj) {t cloneobj=NULL; Try {            //Write byte streamBytearrayoutputstream out=NewBytearrayoutputstream (); ObjectOutputStream Obs=NewObjectOutputStream (out);            Obs.writeobject (obj);                        Obs.close (); //allocate memory, write to original object, generate new ObjectBytearrayinputstream ios=NewBytearrayinputstream (Out.tobytearray ()); ObjectInputStream Ois=NewObjectInputStream (iOS); //returns the generated new objectcloneobj=(T) ois.readobject ();        Ois.close (); } Catch(ClassNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }        returnCloneobj; } }

Third, what does static represent?

Java divides memory into stack memory and heap memory, where stack memory holds some basic types of variables, arrays and object references, heap memory mainly holds some objects, when the class is loaded, the JVM will be static modified variables, methods open a memory space, All object invocation modifications cause modifications to variables within other objects. He will only be allocated one memory space at a time.

Limitations of Static:

1. Only static variables can be called

2. Only the static method can be called

3. Super,this cannot be quoted in any form

4.static variables must be initialized, and initialization time must be earlier than non-static variables

Iv. Internal class

Instantiate an inner class:

External class variable name =new external class ();

External class. Internal class variable name = Outer class variable name. New internal class ();

V. equals overrides

 Public Boolean equals (Object obj) {        if(this = =obj)            {returntrue ;        }         if instanceof Test) {            // to determine if the variables inside themselves define rules equal, equal to return True        }        return false ;}

GetClass should be used because the instanceof inheritance relationship also returns true

 Public Boolean equals (Object obj) {        if(this = =obj)            {returntrue ;        }         if (obj!=nullthis. GetClass ()) {            // to determine whether the variables inside the variable define the rules are equal, equals returns True        }returnfalse;}

Cond...

Java Foundation Improvement Chapter

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.