An IP(Internet Protocol) address is an identifier assigned to each computer and other device(e.g., router, mobile, etc) connected to a TCP/IP network that is used to locate and identify the node in communication with other nodes on the network. IP addresses are usually written and displayed in human-readable notation such as 192.168.1.35 in IPv4(32-bit IP address).
An IP address serves two principle functions : host or network interface identification and local addressing. It’s role has been characterized as follows : “A name indicate what we seek. An address indicate where it is. A route indicate how to get there.”
InetAddress.getLocalHost() is used to find private IP address used in LAN or any other local network.
To find public IP, we use http://bot.whatismyipaddress.com (An online utility to find your public IP), we open the URL, read a line and print the line.
Below is Java implementation of above steps.
System IP Address : 10.0.8.204 Public IP Address : 126.96.36.199
Note : The above output is for machine that is used by GeeksforGeeks online compiler, ide.geeksforgeeks.org
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.