URL class — Java Technical Interview Questions & Answers

URL class

URL class

  • java.net covers sockets, URLs, addresses. Socket = endpoint for TCP communication.
  • URL/URLConnection fetch remote resources.
  • Always handle IOException.
  • Write one tiny example for URL class. Compile, print one result, and say the output out loud before you type.
  • Use URL class only when the problem needs it. Don’t force it into every program. Know one beginner trap.
  • Let's take this on the board with one tiny Main class — no extra files. URL class — prints local machine host name and IP — basic networking API. Start from main, go line by line, and stop at each print. That output is the proof for URL class.

Technical Interview Tip: Client socket needs host + port.

Code Examples for URL class

Example

import java.net.InetAddress;
public class Main {
  public static void main(String[] args) throws Exception {
    InetAddress local = InetAddress.getLocalHost();
    System.out.println(local.getHostName());
    System.out.println(local.getHostAddress());
  }
}

URL class — prints local machine host name and IP — basic networking API.

How to Explain URL class to an Interviewer

What is URL class?

URL class is a Java idea you should explain with one small example. Know the keyword / API used for URL class. Type a tiny Main and print one result. For URL class, say the output before you code.

Why do we use URL class in Java?

Makes the program clearer or safer URL class is a Java idea you should explain with one small example.

Write one small example of URL class.

Show syntax Say what prints

What mistake do beginners make with URL class?

Only the name, no example URL class — only memorising the heading. No example, no trap.

Where is URL class used in a real program?

Day-to-day Java code