-
List Class Methods
Object
equals()
hashCode()
toString()
getClass()
notify()
notifyAll()
wait()
wait(long timeOut)
wait(long timeOut, int nanos)
-
equals
Why are & methods neededhashCode
?Used to compare objects.
The purpose of the method
equals
is to determine if the objects are identical internally by comparing the internal contents of the objects. Thisequals
works slowly, first the hash codes of the objects are compared, and if the hash codes are equal, the check is carried out againstequals
-
What happens if you override equals but not override
hashCode
?Initially
hashCode
, a random number.Collections in Java
equals
always search/compare objects using thehashCode()
. And if identical objects have differenthashCode
, then the objects will be considered different -equals
it simply won’t come to comparison with the help. -
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 inventedwait()
. Calling this method causes the thread to release the monitor and "pause".To unpause, use the methods
notify
,notifyAll
. The methodnotify
"unfreezes" one random thread, the methodnotifyAll
- all "frozen" threads of the given monitor. -
How to properly clone an object?
Two types of cloning.
To clone a default object:
- Add an interface
Cloneable
to your class - Override the method
clone
and 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
clone
yourself:class Point { int x; int y; public Object clone() { Point point = new Point(); point.x = this.x; point.y = this.y; return point; } }
- Add an interface
-
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,
finalize
for 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. -
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
final
read-only, attempting to modify will result in a compilation error - Variables
final
are 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
final
andabstract
(mutually exclusive concepts) a compilation error will occur - Since
final
a class cannot be inherited, its methods can never be overridden.
finally
- a block in a bundletry-catch-finally
, the code in which will be executed regardless of whether an exception has been thrown in the blocktry
or not. Used to release resources.finalize
- method in the class,Object
see 6. -
What is
try-with-resources
?This is a special construct
try
calledtry-with-resources
, in which Pay attention - followedtry
by parentheses, where variables are declared and objects are created. These objects can be used inside a blocktry
indicated by parentheses{}
. When the execution of the block commandstry
ends, regardless of whether it ended normally or there was an exception,()
the method will be called on the object created inside the parenthesesclose()
; -
What is the difference between methods
wait(1000)
andsleep(1000)
?sleep()
pauses the thread for the specified. state changes to TIMED_WAITING, after expiration - RUNNABLEwait()
changes thread state to WAITINGcan 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 -
What is the difference between
i++
and++i
++i
,i
first increases by 1, then participates in the expression.i++
,i
first participates in the expression, then increases by 1.
GO TO FULL VERSION