Difference between TCP and UDP in Java

What is the difference between TCP and UDP is a popular networking question from Java interviews? Though TCP or UDP is Java independent concept and very likely to be asked in other programming language interviews as well, many programmers not really understand them clearly. They sure have heard them because TCP and UDP are two of the most important transport protocol of internet, but when it comes to listing down the difference between them, they fail to mention key differences in terms of ordering, guaranteed delivery, speed, and usage. The biggest benefit of TCP/IP protocol is that it provides guaranteed to deliver of messages and in the order client sent them, that's very important when dealing with important messages e.g. order, trade, and booking messages. You cannot afford to lose them, neither you can process then out-of-sequence. The UDP protocol, on the other hand, provides the much-needed speed and can be used to implement a multicast network. In this article, I'll tell you the difference between TCP and UDP protocol from Java interview perspective.

Difference between TCP and UDP

Here are some important difference between TCP and UDP protocol from Java application developer's perspective.

1) TCP is a reliable stream oriented protocol as opposed to UDP which is not reliable and based upon datagram. This is actually the most significant difference between TCP and UDP as you cannot use the UDP for sending important messages which you can't afford to lose. Though there are some reliable protocols built over UDP e.g. TIBCO certified messaging which implement additional checks whether the message is delivered or not and then facilitate re-transmission.

2) One more important difference between TCP and UDP comes from speed. Since TCP is reliable and connection oriented it has lots of overhead as compared to UDP, which means TCP is slower than UDP and should not be used for transferring message where speed is critical e.g. live telecast, video or audio streaming. This is the reason UDP is popularly used in media transmission world.

3) Another structural difference between TCP and UDP is that data boundaries are preserved in case of UDP but not in the case of TCP because data is sent as it is as one message in case of UDP but TCP protocol can break and reassemble the data at sending and receiving end.

4) Another key difference between TCP and UDP protocol comes from the fact that TCP is a connection-oriented protocol but UDP is a connectionless protocol. What this mean is, before sending a message a connection is established between sender and receiver in TCP but no connection exists between the sender and receiver in UDP protocol.

TCP vs UDP differences

5) One more difference between UDP and TCP protocol which comes from how they work with ordering. TCP provides you order guarantee but UDP doesn't provide any ordering guarantee. For example, if Sender sends 3 messages than the receiver will receive those three messages in the same order, Sender, has sent, even if they are received at different order at receiver end TCP will ensure they are delivered to a client in the order they are sent by the sender. UDP doesn't provide this feature, which means it's possible for the last message to be received first and vice-versa.

6) TCP header size is larger than UDP header size due to excessive metadata information sent by TCP protocol. Those are required to ensure the guarantee provided by TCP protocol e.g. guaranteed ordered delivery.  You can also read The TCP/IP Guide: A Comprehensive, Illustrated Internet Protocols Reference 1st Edition, it's easy to read, interesting, and improve your fundamentals on TCP/IP and UDP protocol along with how to write reliable networking program in Java.

7) TCP stands for Transmission control Protocol while UDP stands for User datagram protocol.

8) Multicast can only be used with UDP, it's not possible with TCP because it's a connection oriented protocol.

Difference between TCP and  UDP in Java Multicasting

In short,
  • TCP is reliable and guaranteed but UDP is not guaranteed. You are at risk of losing messages in UDP.
  • TCP provides ordering guaranteed which means the message will be received in the same order they are sent, UDP protocol doesn't provide such guarantee.
  • TCP is a connection-oriented protocol as compared to UDP which is connectionless.
  • UDP is much faster than TCP protocol.
  • UDP protocol can be used for multicasting e.g. TIBCO RV
  • UDP also preserves data boundary because all data is sent in the same packet, but TCP doesn't as one message may be divided into several packages before sending.

That's all about the difference between TCP and UDP protocol in Java. As I told it's not really a Java specific question but commonly asked to check candidate's knowledge of essential networking protocols and computer networks. If you are interviewing for server side Java development job then knowledge of TCP/IP, UDP, and Multicating becomes very important becuase these the technologies which are used to create high-performance servers in Java.

Other Java TCP/IP articles you may like
  • Top 15 Java NIO and Socket Programming Interview Questions (list)
  • How to create HTTP Server in Java using ServerSocket? (solution)
  • How to fix java.net.SocketException: Too many files open java.io.IOException? (solution)
  • How to solve java.net.SocketException: Failed to read from SocketChannel: Connection reset by peer (solution)
  • How to find IP address of localhost in Java Program? (solution)
  • What is TIBCO Rendezvous or TIBCO RV messaging? (answer)

TCP/IP Illustrated Vol. 1: The Protocols
TCP/IP Illustrated Vol.2: The Implementation
User Datagram Protocol


  1. Hey, I think your posts are great. The language is straightforward and you get to the point without going into too much details. Maybe you can write more articles on how to design applications - what you do before starting to code it. Thanks!

  2. Useful article. god works