It's not illegal to eat food while walking in Japan, although it's considered rude and in bad manner to eat food while walking in Japan.
When eating food or drinking in Japan it's best to stand near the food establishment or nearby spot and eat or drink and then throw any garbage away in the bins.
The Japan culture views eating as an act, which deserves your full attention and respect and eating or drinking while walking is seen as careless, but you won't get into trouble for eating or drinking while walking in Japan, but it's best to avoid it.
It's considered rude while eating in Japan to walk while eating, stick the chopsticks upright in rice, make loud chewing noises or burping when eating and it's also rude to pass food from chopstick to chopstick in Japan while eating.
Other rude behaviors while eating in Japan are resting your chopsticks on the bowl, stabbing your food with chopsticks as well as blowing your nose at the table.
When in Japan do not stick your chopsticks upright in your rice as it's a funeral custom and do not pass your food directly from one pair of chopsticks to another, which is also associated with funerals.
While you can drink coffee or other beverages on the street in Japan it's also considered rude to walk and drink coffee or other beverages in Japan.
It's not illegal to drink coffee or other beverages while walking in Japan, but it's simply seen as rude so it's best to avoid it.
The custom in Japan when drinking coffee and other beverages, is to finish the coffee or other drinks when standing near where you purchased the coffee or other beverage.
Then you can dispose of the empty cup or trash where you're located.
The main reason for not drinking your coffee or other beverage while walking in Japan is because of a lack of public trash cans, which means that you would have to carry your trash with you.
Although I've seen some people dispose of their cups and cans and other trash around trees etc, when I visited Japan but it's not what you should do.
Drink your coffee or beverage near the vending machine or convenience store where you bought it and then dispose of the trash properly and use the bins that are provided at the store or the vending machine to dispose of the trash.