根据java面向对象的原理,ip地址也被封装成了对象,InetAdress

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import org.junit.Test;

/**
 * 获取ip地址
 * @author WangShuang
 *
 */
public class Demo {
	
	@Test
	public void run1() {
		try {
			//获取本机的ip地址
			InetAddress localHost = InetAddress.getLocalHost();
			System.out.println(localHost);
			
			//已知别人的域名 获取别人的ip地址对象
			InetAddress byName = InetAddress.getByName("www.baidu.com");
			System.out.println(byName);
			String hostAddress = byName.getHostAddress();//返回ip地址的字符串表现形式
			System.out.println(hostAddress);
			
			InetAddress[] allByName = InetAddress.getAllByName("www.baidu.com");
			System.out.println(Arrays.toString(allByName));
			
			
		
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
}