devxlogo

Java

Image

Learn Some Efficient Programming Practices

Avoiding null pointer exceptions is a good programming practice. However, simple things such as string comparison can also lead to null pointer exceptions. Considering the following: You have a constant

Image

Creating a Link to a File in Java

There are times when we need to reflect the changes made to a file in a different file as well. Mostly, they are made available in a common place so

Image

Understanding the Long Data Type in Java

Java long behaves differently when used with the letter L and without it. Code snippet public class JavaLong{ public static void main(String args[]) { JavaLong javaLong = new JavaLong(); javaLong.proceed();

Image

Simple Annotation in Java

Annotations are similar to interfaces in that you can convey your own specifications that others will need to follow. Here we are defining an Annotation named ClassHeaderAnnote, which has few

Image

Reducing Unbuffered Streams

InputStream in = new FileInputStream(file);int q;while ((q = in.read()) != -1) { …} The code above reads a file byte by byte. Every read() method call on the stream causes

Image

Converting Numbers to Strings

“” + set.size()new Integer(set.size()).toString() The return type of the Set.size() method is int, but it is desired a conversion to String. These two lines do the conversion, but the first

Image

Parsing and Converting Numbers in Java

int x = Integer.valueOf(str).intValue(); int y = Long.valueOf(Double.valueOf(str).longValue).intValue(); To use the API without allocating unnecessary objects use the following lines of code: int x = Integer.parseInt(str); int y = (int) Double.parseDouble(str);