I found some basic examples online about sending messages between computers in Java. However i realized that i only used local ip addresses so i could only send messages to a pc connected in the same network. I rewrote most of the code to make a slightly better version but if i wanted to send a message to a computer in a different network, connected to the internet, would switching the local ip with the receiving computer's public ip address in the code do the trick?
Yes it should work, however it's common place to connect to the Internet via a modem, which in term is the device holding the public IP. Some NAT and likely port forwarding would be needed at the endpoint.