A good thing in Japan is that you will not be in trouble looking for a restroom when you are out. You can find it easily at station too. Then toilets are much clean and comfortable than in Europe even it is free of charge to use them.
Although metro trains and trains operated by private operators are not equipped with lavatories, many JR's trains are equipped with lavatories except most of commuter trains such as Yamanote line trains.
Probably you will refrain from using the lavatory on train and wait till the station which you will get off in case it is not urgent. However, sometimes using the train's lavatory is better than using the restroom at station. There are two reasons. The first one is that the lavatory is modern than the restroom. Then, the second reason is in case you have to change train or take a bus. Sometimes using the restroom at the transfer station is waste of time and you might miss the connecting train or bus.