FloatCalc

There’s a simple Java program FloatCalc.java:

public class FloatCalc {

    public static void main(String[] args) {
        System.out.println(1 - 0.56);
    }

}

What will be the output of this program?

Bonus question: Does the computation occur in run-time or compile-time?

Java 8 Launch Event

Java logoLast week Oracle launched Java 8, a new release of Java platform which includes many new features, Lambdas and Streams to name a few. See Java 8 Central for more information, or go to Java 8 download page to download it and test-drive its new features.

Today, on March 25, 2014, there will be an online Java 8 launch event. The event starts at 10:00 AM PST, or 21:00 MSK.

Backspace character

In December I asked a question: What’s wrong with this code? I agree that the question was rather broad without context. I just asked it in the same way a friend asked me.

Many people noticed '\b' character in the code… Is it good or bad? Well, it depends.

What is '\b' character?

Quote from an answer by T.J. Crowder at Stack Overflow: On most terminals or consoles, '\b' is a nondestructive backspace. It moves the cursor backward, but doesn’t erase what’s there.

Another answer provides a detailed explanation of how the backspace character works.

If you’re interested, see The “backspace” escape character ‘\b’ in C, unexpected behavior? question.

What can be wrong?

As long as the output is written to terminal or console, it does what it’s supposed to do, most of the time at least. But what about capturing the output to a file? Then you’ll see a weird character in the file.

In my opinion, backspace character in that code serves no purpose. Much better way would be just to remove the last space from the buffer:

sb.deleteCharAt(sb.length() - 1);

It is the safest approach and it has no (possible) undesirable effects.

Another issue is if objs is empty, the opening '[' would be removed from the string. I think the assumption here is that objs is never empty which can be enforced by the calling code.

What’s wrong with this code?

There’s a code snippet. What’s wrong with it?

StringBuffer sb = new StringBuffer(" - The required object is unknown: ");
sb.append("Please choose an object from [");
for (String tmp : objs) {
    sb.append(tmp);
    sb.append(' ');
}
sb.append('\b');
sb.append(']');

Mojibake at Flickr

Русская версия

Since the middle of August Flickr stopped showing search requests where my photos were displayed. Actually it shows them but there are mojibake (or кракозябры) instead of Russian words.

A detailed statistics is available to Flickr Pro-accounts, if the user asked for it. Thanks to this statistics, I found my photos at Wikipedia and at other sites.

This is how it looks on the referrer page:
Part of Flickr referrer statistics page

English search terms are displayed correctly (it would have been weird, if it had been otherwise). Search terms 37 and 38 are undecipherable. If I click them, nothing is found, naturally.

Read the rest of this entry »

Is it possible to change a String in Java

Everyone knows String object in Java is immutable. It means it is impossible to change its value after the object was created.

It was my answer to the question How can the value of string be changed? I was asked this question on a job interview.

But is it really impossible? Yes, it is… unless you use reflection. The following program demonstrates how you can change the value stored in a String object:

Read the rest of this entry »

Classics of not using braces in if-else statement

There’s a snippet of code:

if (event == null)
    if (logger.isWarnEnabled()) logger.warn( "event is null");
else
    fireEvent(event);

This beautifully formatted code does not do what its author wanted it to do: The compiler sees this as

if (event == null)
    if (logger.isWarnEnabled())
        logger.warn( "event is null");
    else
        fireEvent(event);

In this case the event is never fired as long as log level includes warning. Things get worse if log level is above warning: fireEvent(null) is executed, however that’s the situation the author tried to avoid.

This is why Java Coding Style Guidelines requires using braces in all cases:

Note: if statements always use braces {}. Avoid the following error-prone form:

    if (condition) //AVOID! THIS OMITS THE BRACES {}!
        statement;