当前位置:网站首页>Byte order, object class

Byte order, object class

2022-06-10 04:31:00 little-peter

  • Java The byte order of the integer is ?

answer :Big-Endian( Big end )

Byte order refers to the storage order of each byte when multi byte data is stored in the computer memory or transmitted through the network . Usually there are Little-Endian( The small end ) and Big-Endian( Big end ) Two ways . These two methods will be introduced respectively .

(1)Little-Endian

Little-Endian( The small end ) It means that the low order bytes are stored in the low address end of the memory , The high bits are stored in the high address of memory .

for example , When stored in small end mode , Hexadecimal digits represent 0x12 34 56 78 The storage mode in memory is :

Low address ---------------------------------------> High address

0x78 | 0x56 |0x34 |0x12

(2)Big-Endian

Big-Endian( Big end )

A little ( Opposite to the small end , I won't go into details here )

Why should we distinguish between small and large ends ?

Because in the computer system , All storage is stored in bytes , But in most programming languages , Except for one byte char Out of data type , There are other data types that take up more than one byte . for example , stay Java In language ,short Type takes two bytes ,int Type account 4 Bytes . So how to store these data that occupy multiple bytes ? You can use the big end to store , You can also use the small end to store . Different programming languages , Different processors may use different storage methods .

So how do you know Java The byte order of the language ?

You can know by looking at how byte arrays are stored in memory , The sample code is as follows :

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
public class T {
    public static void main(String[] args) throws IOException {
        byte[]arr=new byte[4];
        arr[0]=0x78;
        arr[1]=0x56;
        arr[2]=0x34;
        arr[3]=0x12;
        ByteArrayInputStream bais=new ByteArrayInputStream(arr);
        DataInputStream dis=new DataInputStream(bais);
        System.out.println(Integer.toHexString(dis.readInt()));
    }
}
 

The running result of the program is :

From the above running results, we can see that , High byte (78) Stored in low address , It's obviously big end .

although Java The big end is used , In some cases it may also be necessary to obtain CPU Big end or small end ,Java Provides a class library that can be used to get CPU Big end or small end :

java.nio.ByteOrder.*
  • Object class
  •  

    object Class method

    Method name Return type Methods described clone() Object Create and return a copy of an object equals(Object obj) boolean Judge obj Whether the object is equal to this object finalize() void When the garbage collector determines that there are no more references to the object , This method is called by the object's garbage collector getClass() Class<?> return Object Runtime class for hashCode() int Returns the hash code value of the object notify() void Wake up a single thread waiting on this object monitor notifyAll() void Wakes up all threads waiting on this object monitor toString() String Returns the string representation of the object wait() void This object is called in another thread notify() Method or notifyAll() Before the method , Make the current thread wait wait(long timeout) void This object is called in another thread notify() Method or notifyAll() Before the method , Or before the specified amount of time , Make the current thread wait wait(long timeout,int nanos) void This object is called in another thread notify() Method or notifyAll() Before the method , Or some other thread breaks the current thread , Or has exceeded a certain actual amount of time before , Make the current thread wait

    Object Class is the root of class hierarchy , stay Java In language , All classes basically inherit from this class . and Object Class is Java The only class in a language that does not have a parent class , And all the other classes , Including standard containers , For example, an array of , Inherit from Object class .

 

  • Math.round(12.5) The return value of is equal to ?Math.round(-12.5) The return value of is equal to ?

  answer :13,-12

Math Class mainly provides the following 5 Methods related to rounding :

1)static double ceil(double a): Return greater than or equal to a Minimum integer of

2)static double floor(double a): Return less than or equal to a Maximum integer for

3)static double rint(double a): The rounding method , Return and a The nearest integer to the value of , by double type

4)static long round(double a): The rounding method , Return and a The nearest long integer to the value of

5)static int rount(float a): The rounding method , Return and a The closest integer to the value of

For this problem ,round Is a rounding method ,12.5 The decimal part of is 0.5, When executed Math.round() In operation , Results need to be entered , So the result is 13;-12.5 The decimal part of is also 0.5, When executed Math.round() In operation , The result also needs to enter ,-12>-13, therefore , The result is rounded up to -12.

 

 

 

 

原网站

版权声明
本文为[little-peter]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091240103944.html