Integer and Object

36.Integer a= 127,Integer b = 127;Integer c= 128,Integer d = >128;, are they equal?

the answer is
a
and
b
equal,
c
and
d
not equal.

For basic data types
= =
?Compared value

For index data types
= =
?What’s more important is the address

Integer a= 127
This kind of assignment, is it?
Integer
?Active boxing mechanism. ?When dynamically packing, it will go to the cache pool?

Integer
Object will not create a new object until it is obtained.

If the value of an integer literal is within
-128
arrive
127
between them, then the automatic boxing will not
new
new
Integer
Object, it is a direct reference to the Integer in the cache pool.
Object, out of scope
a1==b1
The result is
false

 public static void main ( String [] args) {
 Integer a = 127;
 Integer b = 127;
 Integer b 1 = new Integer( 127 );
 System . out . println ( a = = b ); //true
 System . out . println ( b = = b 1 ); //false
 Integer c = 128;
 Integer d = 128;
 System . out . println ( c = = d ); //false
 }

what is
Integer
cache?

Because according to practice, it is found that some data operations are concentrated in the range of relatively large values, so
Integer
Created a cache pool, the default range is -128
arrive
127
, which can be set according to
JVM-XX:AutoBoxCacheMax=
To modify the cached latest value, the latest value cannot be changed.

The principle of implementation is
int
It will be adjusted when moving the box.
Integer.valueOf
, the entrance has arrived
IntegerCache
.

It’s very simple, just check whether the value is within the cache range, and if so, go to
IntegerCache
Get the result, if not, create a new Integer
object.

IntegerCache
It is a static inner class, and the cache value will be initialized in the static block.

private static class IntegerCache {
… …
static {
//Create Integer object storage
for ( int k = 0 ; k < cache . length ; k + + )
cache [ k ] = new Integer( j + + );
… …
}
}


37. How to convert String into Integer ? principle?

String
Turn into
Integer
, there are two main methods:

  • Integer.parseInt(String s)
  • Integer.valueOf(String s)

No matter which kind, it will eventually adjust?
Integer
within class
parseInt(String s, int radix)
?Law.

Throw away some boundaries and the like and look at the kernel code:

public static int parseInt(String s, int radix)
throws NumberFormatException
{
int result = 0;
//Yes No Yes Negative number
boolean negative = false;
//Char character array subscript and length
int i = 0 , len = s. length ();
… …
int digit;
// Determine whether the character length is 0 or not, otherwise throw an exception
i f ( len > 0 ) {
… …
while ( i < len ) {
// Accumulating negatively avoids surprises near MAX_VALUE
//Return the numerical value of the character table ? in the specified base. (Here is the ? base value)
digit = Character. digit(s. charAt ( i + + ) , radix ) ;
//Multiple base digits by number value
result * = radix ;
result - = digit ;
}

}
//Based on whether the above result is a negative number, return the corresponding value
return negative ? result : - result ;
}


Remove the branches and vines (of course you can check out these branches and vines, source code
cover
(many cases have been solved), in fact, what is left is a simple string traversal calculation, but the calculation formula is a bit unconventional, it is the cumulative subtraction of negative values.

38. Common methods of Object class?

Object
A class is a special class that is a class of all classes, which means that all classes can adjust its methods. It mainly provides the following 11
Individual methods can be roughly divided into six categories:

  • Object? Compare:
  1. public native int hashCode(): native method, used to return the hash code of the object, mainly used in hash tables, such as HashMap in JDK.
  2. public boolean equals(Object obj): To compare whether the memory addresses of two objects are equal, the String class overrides this method to allow users to compare whether the string values are equal.
  • Object copy:
  1. protected native Object clone() throws CloneNotSupportedException: naive method, used to create and return a copy of the current object. ?Generally, for any object x, the expression x.clone() != x is true and x.clone().getClass() == x.getClass() is true. Object itself does not implement the Cloneable interface, so if you do not override the clone method and perform debugging, a CloneNotSupportedException exception will occur.
  • Object to string:
  1. public String toString(): Returns the hexadecimal string of the class name@the hash code of the instance. It is recommended that all Object classes override this method.
  • Multi-thread scheduling:
  1. public final native void notify(): native method and cannot be overridden. Wake up a thread waiting on the monitor of this object (the monitor is equivalent to the concept of a lock). If there are multiple threads waiting, only one will be woken up arbitrarily.
  2. public final native void notifyAll(): native method and cannot be overridden. Like notify, the only difference is that it will wake up all threads waiting on this object monitor, not just one thread.
  3. public final native void wait(long timeout) throws InterruptedException: native method and cannot be overridden. Pause the execution of the thread?. Note: The sleep method does not release the lock, and the wait method releases the lock. timeout is the waiting time.
  4. public final void wait(long timeout, int nanos) throws InterruptedException: There is an additional nanos parameter. This parameter indicates additional time (in nanoseconds, range is 0-999999). Therefore, nanos milliseconds need to be added to the timeout time.
  5. public final void wait() throws InterruptedException: Same as the previous two wait methods, except that this method waits continuously and there is no concept of timeout.
  • Reflection:
  1. public final native Class getClass(): native method, used to return the Class object of the current runtime object, uses the final keyword modification, so class rewriting is not allowed.
  • Garbage collection:
  1. protected void finalize() throws Throwable: Notify the garbage collector to recycle the object.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 57406 people are learning the system