Home » Core Java » net » InetAddress » java.net.Inet4Address Example

About Aldo Ziflaj

Aldo Ziflaj
Aldo is a student of Computer Engineering and a programming addict. He spares his free time coding, whether mobile, web, or desktop programming. He is also one of the co-founders of Things Lab.

java.net.Inet4Address Example

In this example we will discuss about Inet4Address and its usage. Inet4Address represents an Internet Protocol version 4 (IPv4) address.

Inet4Address is a subclass of InetAddress class, which represents either a 32-bit or 128-bit unsigned number used as an IP, the lower-level protocol on which protocols like UDP and TCP are built.

The Inet4Address class exists since JDK1.4.
 
 
 

Inet4Address in Java

To see a basic usage of Inet4Address in Java, create a class called SimpleInet4AddressExample with the following source code:

SimpleInet4AddressExample.java

package com.javacodegeeks.examples;

import java.net.Inet4Address;
import java.net.UnknownHostException;

public class SimpleInet4AddressExample {

	public static void main(String[] args) {
		String url = "javacodegeeks.com";
		
		try {
			Inet4Address address = (Inet4Address) Inet4Address.getByName(url);
			
			System.out.println("The IP of "+url+" is "+address.getHostAddress());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		

	}

}

In this example, firstly I get the InetAddress of http://www.javacodegeeks.com/ by using the Inet4Address.getByName(), passing the URL as the parameter. The getByName() method is inherited from InetAddress, hence defined there.

After getting the InetAddress, I use the getHostAddress() method to print the IP address of Java Code Geeks homepage.

The output would be like this:

The IP of javacodegeeks.com is 64.64.30.146

By using other methods, we can get more information. Consider this other example called MoreInet4AddressMethods:

MoreInet4AddressMethods.java

package com.javacodegeeks.examples;

import java.net.Inet4Address;
import java.net.UnknownHostException;

public class SimpleInet4AddressExample {

	public static void main(String[] args) {
		String url = "javacodegeeks.com";
		
		try {
			Inet4Address address = (Inet4Address) Inet4Address.getByName(url);
			
			System.out.println("IP address:"+address.getHostAddress());
			System.out.println("Cannonical host name: "+address.getCanonicalHostName());
			System.out.println("Is Local Link? "+address.isLinkLocalAddress());
			
			System.out.println("Is this address equal to local host? " + address.equals(Inet4Address.getLocalHost()));
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		
	}

}

In this other example I showed how to use some of the methods to get information about the Java Code Geeks website. I used getHostAddress() to get the IP address, getCannonicalHostName() the fully qualified domain name and isLinkLocalAddress(), in order to check if it is a local address.

Also, I tested the equals() method by checking equality with the localhost.

The output is this:

IP address:64.64.30.146
Cannonical host name: server.javacodegeeks.com
Is Local Link? false
Is this address equal to local host? false

More about Inet4Address

Textual representation of IPv4 address used as input to methods takes one of the following forms:

d.d.d.d
d.d.d
d.d
d

When four parts are specified, each is interpreted as a byte of data and assigned, from left to right, to the four bytes of an IPv4 address.

When a three part address is specified, the last part is interpreted as a 16-bit quantity and placed in the right most two bytes of the network address. This makes the three part address format convenient for specifying Class B net- work addresses as 128.net.host.

When a two part address is supplied, the last part is interpreted as a 24-bit quantity and placed in the right most three bytes of the network address. This makes the two part address format convenient for specifying Class A network addresses as net.host.

When only one part is given, the value is stored directly in the network address without any byte rearrangement.

For methods that return a textual representation as output value, the first form, i.e. a dotted-quad string, is used.

Download Code

Download
You can download the full source code of this example here : Inet4AddressExample
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of