For these two expressions:
I will give it to you when I no longer need it.
I will give it to you if I no longer need it.
Let us say it is an object such as a book or a laptop or something similar. Is there any difference between the meanings of these two sentences?