The getHost
method of the URL
class can be used to get the hostname of the URL object.
public String getHost()
This method doesn’t take any parameters.
The return type of the getHost
method is a String
. If the URL
contains an IPv6 address instead of a domain name, then this method returns the IPv6 address enclosed in square brackets ([IPV6_address]
).
The
URL
class is present in thejava.net
package.
The
getHost
method only returns the host name, whereas thegetAuthority
method returns the host name with theport number
.
The code below demonstrates how to use the getHost
method:
import java.net.URL;class GetHostExample {public static void main( String args[] ) {try {URL url= new URL("https:// www.educative.io:8000");String hostName = url.getHost();System.out.println("The URL is : "+url);System.out.println("Host name is : "+ hostName);} catch (Exception e) {System.out.println(e);}}}
In the code above,
URL
object.URL url= new URL("https:// www.educative.io:8000");
getHost
method to get the host name of the URL
object.String hostName =url.getHost();
URL
and the host name of the URL
.