Q: clear() method of ByteBuffer in java I was reading about DatagramChannel in one of the tutorial website.They explained the following sample program
String newData = "New String to write to file..."
                    + System.currentTimeMillis();

ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());
buf.flip();

int bytesSent = channel.send(buf, new InetSocketAddress("example.com", 80));

They have added the line buf.clear() after created ByteBuffer class object. clear() is for clear the buffer.But initially we dont need to clear right??.if it is needed there,then why should we use ??.
thanks.(sorry, if any grammer mistake) 

A: Maybe it's just a bad tutorial/website. It's not needed in a situation where you've just allocated a brand new ByteBuffer. You could just use
ByteBuffer buf = ByteBuffer.wrap(newData.getBytes());

to get rid of the put() and flip() too.
