JIYIK CN >

Current Location:Home > Learning > PROGRAM > Java >

Get the IP address of the current device in Java

Author:JIYIK Last Updated:2025/04/14 Views:

An Internet Protocol (IP) address is an identifier for each device connected to a TCP/IP network. This identifier is used to identify and locate nodes in the middle of communication.

The IP address format, such as 127.0.0.0, is a human-readable notation. This tutorial demonstrates how to get the IP address of the current machine using Java.


Get the system IP address of the current device in Java

IP address has two main functions: local addressing and host or network interface identification. Let's try to get the system IP address of the current device in Java.

example:

package Delfstack;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Get_IPAddress {
  public static void main(String[] args) {
    InetAddress My_IP;
    try {
      My_IP = InetAddress.getLocalHost();
      System.out.println("The IP address of the Current Device is: " + My_IP.getHostAddress());
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
  }
}

Output:

The IP address of the Current Device is: 172.23.96.1

After running the code, we get the system IP address of the current device where the Java code was compiled.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Generate a random double value between 0 and 1 in Java

Publish Date:2025/04/14 Views:153 Category:Java

This article will introduce three methods to generate double random values ​​between 0 and 1 of primitive types. To demonstrate the randomness of the generated values, we will use a loop to generate ten random double values ​​betwee

Setting the seed of a random generator in Java

Publish Date:2025/04/14 Views:62 Category:Java

A seed is a number or vector assigned to a pseudo-random generator to generate the desired sequence of random values. If we pass the same seed, it will generate the same sequence. We usually assign the seed as the system time. In this way,

Switching on enumeration types in Java

Publish Date:2025/04/14 Views:96 Category:Java

This article explains how to use enum in Java . We will use statement switch with enum in two ways . switch In Java, we use traditional switch and case to perform enumeration operations. switch In this example, we SwitchEnum create an enume

How to delay for a few seconds in Java

Publish Date:2025/04/14 Views:131 Category:Java

This tutorial explains how to create program delays in Java and gives some sample code to understand it. There are several ways to create a time delay, such as TimeUnit.sleep() ,, ScheduleAtFixedRate() etc. Thread.sleep() Let's look at an e

How to Convert Hashmap to JSON Object in Java

Publish Date:2025/04/14 Views:112 Category:Java

This article explains how to convert a Hashmap to a JSON object in Java. We will see a detailed example of creating a hashmap and then converting it to a JSON object. Hashmap and JSON are both very commonly used by developers as they help u

How to sort a Map by value in Java

Publish Date:2025/04/14 Views:171 Category:Java

This tutorial explains how to Mapkey, value sort pairs by value in Java and lists some sample codes to understand it. There are several ways to Map sort . Here we use sort() the , sorted() method, and Comparator interface. Let's look at an

How to Print a HashMap in Java

Publish Date:2025/04/14 Views:85 Category:Java

This tutorial explains how to print HashMap an element in Java and also provides some sample code to understand the topic. HashMap It is Map an implementation class of the interface and is used to collect elements into key and value pairs.

Updating Hashmap Values in Java

Publish Date:2025/04/14 Views:88 Category:Java

This article explains how to use the two methods contained in the HashMap class in Java - update put() and replace() update the values ​​in a HashMap. hashmap.put() Update the value in Hashmap in Java using When we want to HashMap inser

Difference between hashmap and map in Java

Publish Date:2025/04/14 Views:193 Category:Java

This tutorial explains the key differences between Map and in Java HashMap . In Java, Map is an interface for storing data in key-value pairs, and HashMap is Map an implementation class of the interface. Java has several classes ( TreeHashM

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial