Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - URL getPort() Method with Examples



Description

The Java URL getPort() method returns the port number of the protocol associated with this URL. If the port is not set, then -1 is returned.

Declaration

Following is the declaration for java.net.URL.getPort() method

public int getPort()

Parameters

NA

Return Value

the port number.

Exception

NA

Example 1

The following example shows the usage of Java URL getPort() method for a valid url with https protocol. In this example, we're creating an instance of URL class. Now using getPort() method, we're getting the port and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https","www.tutorialspoint.com","/index.htm");
         int result = url.getPort();
         System.out.println(result);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

-1

Example 2

The following example shows the usage of Java URL getPort() method for a valid url with http protocol. In this example, we're creating an instance of URL class with a port number as 80. Now using getPort() method, we're getting the port and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http","www.tutorialspoint.com",80,"/index.htm");
         int result = url.getPort();
         System.out.println(result);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

80

Example 3

The following example shows the usage of Java URL getPort() method for a valid url with file protocol. In this example, we're creating an instance of URL class. Now using getPort() method, we're getting the port and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("file","www.tutorialspoint.com","/index.htm");
         int result = url.getPort();
         System.out.println(result);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

-1
java_url.htm
Advertisements