devxlogo

Java

Image

Calling Date.setTime()

account.changePassword(oldPassword, newPassword);Date lastModified = getLastModified();lastModified.setTime(System.currentTimeMillis()); The above code updates the last modified date of the password and wants to avoid creating a new Object, but uses instead the setTime method

Image

Setting the Log Level in Java

We have been using logging in Java and different code requires different levels of logging. Hence these Levels will come in handy. Code sample: import java.util.logging.Logger;import java.util.logging.Level;public class JavaLogger{ public

Image

Assuming Char Represents One Character

Do not assume char represents only one character: “uD83DuDC31”.length() == 2 The escape sequence represents the Unicode code point 0x1F431 in UTF-16 as 2 chars. So even though this is

Image

Creating a JSON Object Using Java

JSON is being widely adapted by the industry for easier transformation of data. Using the json-simple-1.1.1.jar in the classpath will provide you with the basics of JSON and its functionality.

Image

A Better Way to Achieve Random File Access

RandomAccessFile raf = new RandomAccessFile(f, ???r???);for (???) { raf.seek(pos); byte b = raf.readByte();} Despite its name, the java.io.RandomAccessFile class is not very convenient for accessing files in a random-access manner,

Image

How to Avoid Cloning in Java

Cloning is the easiest way of copying a class. With this, however, you also have all properties being passed on to the cloned class, which probably is a security hassle.