zor07
Level 31
Санкт-Петербург

Level 21

Published in the Random EN group
Level 21
  1. List Class MethodsObject

    • equals()
    • hashCode()
    • toString()
    • getClass()
    • notify()
    • notifyAll()
    • wait()
    • wait(long timeOut)
    • wait(long timeOut, int nanos)
  2. equalsWhy are & methods needed hashCode?

    Used to compare objects.

    The purpose of the method equalsis to determine if the objects are identical internally by comparing the internal contents of the objects. This equalsworks slowly, first the hash codes of the objects are compared, and if the hash codes are equal, the check is carried out againstequals

  3. What happens if you override equals but not override hashCode?

    Initially hashCode, a random number.

    Collections in Java equalsalways search/compare objects using the hashCode(). And if identical objects have different hashCode, then the objects will be considered different - equalsit simply won’t come to comparison with the help.

  4. Why are the methods wait, notify, notifyAll?

    Sometimes a program may have such a situation that the thread has entered the code block synchronized, locked the monitor and cannot continue to work, because. some data is still missing: for example, the file that it should process has not yet loaded, or something like that. To solve this problem, a method was invented wait(). Calling this method causes the thread to release the monitor and "pause".

    To unpause, use the methods notify, notifyAll. The method notify"unfreezes" one random thread, the method notifyAll- all "frozen" threads of the given monitor.

  5. How to properly clone an object?

    Two types of cloning.

    To clone a default object:

    • Add an interface Cloneableto your class
    • Override the method cloneand call the base implementation in it:
    class Point implements Cloneable
    {
     int x;
     int y;
    
     public Object clone()
     {
      return super.clone();
     }
    }

    Or you can write the implementation of the method cloneyourself:

    class Point
    {
     int x;
     int y;
    
     public Object clone()
     {
      Point point = new Point();
      point.x = this.x;
      point.y = this.y;
      return point;
     }
    }
  6. Why is the method needed finalize()and how does it work?

    If you remember, then finalize()is a special method that is called on an object before the garbage collector destroys it.

    The main purpose of this method is to release external non-Java resources used: close files, I/O streams, etc.

    finalize()works unstable.

    This method does not justify the hopes placed on it. The Java machine can delay the destruction of an object, as well as a method call, finalizefor as long as it likes. Moreover, it does not guarantee at all that this method will be called. In a bunch of situations for the sake of "optimization" it is not called.

  7. What is the difference between final, finally, finalize?

    • final- modifier
    • Fields cannot be changed, methods are overridden
    • Classes cannot be inherited
    • This modifier only applies to classes, methods and variables (also local variables)
    • Method arguments marked with are finalread-only, attempting to modify will result in a compilation error
    • Variables finalare not initialized by default, they must be explicitly assigned a value when declared or in the constructor, otherwise a compilation error
    • If a final variable contains a reference to an object, the object can be changed, but the variable will always refer to the same object.
    • This is also true for arrays because arrays are objects - an array can be changed and a variable will always refer to the same array.
    • If a class is declared finaland abstract(mutually exclusive concepts) a compilation error will occur
    • Since finala class cannot be inherited, its methods can never be overridden.

    finally- a block in a bundle try-catch-finally, the code in which will be executed regardless of whether an exception has been thrown in the block tryor not. Used to release resources.

    finalize- method in the class, Objectsee 6.

  8. What is try-with-resources?

    This is a special construct trycalled try-with-resources, in which Pay attention - followed tryby parentheses, where variables are declared and objects are created. These objects can be used inside a block tryindicated by parentheses {}. When the execution of the block commands tryends, regardless of whether it ended normally or there was an exception, ()the method will be called on the object created inside the parentheses close();

  9. What is the difference between methods wait(1000)and sleep(1000)?

    sleep()pauses the thread for the specified. state changes to TIMED_WAITING, after expiration - RUNNABLE

    wait()changes thread state to WAITING

    can only be called on the object holding the lock, otherwise an IllegalMonitorStateException will be thrown . when the method fires, the lock is released, allowing other threads waiting to acquire the same lock to continue. in the case wait(int)of an argument, the state will be TIMED_WAITING

  10. What is the difference between i++and++i

    • ++i, ifirst increases by 1, then participates in the expression.
    • i++, ifirst participates in the expression, then increases by 1.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION