Thursday, December 24, 2015

Java Programming: Swap two integer values without using a temporary variable


Every time I had to take programming exam for a software developer position, this is one of the tricky questions I usually encountered: "How to swap two integer values without using a temporary variable?". The first time I had this question I spent about 45 minutes thinking about the solution until I gave up and thought that it was impossible to solve. Fast forward many years after, I am wiser now so I already know the solutions. It turned out that there are two possible ways to solve this tricky question.

Solution #1: Using Arithmetic Operator

public class SwapVariablesDemo1
{
  public static void main(String[] args)
  {
    int x = 1;
    int y = 2;
    
    System.out.println("Values before swapping: x = " + x + ", y = " + y);
    
    x = x + y; // x = 3, y = 2
    y = x - y; // x = 3, y = 1
    x = x - y; // x = 2, y = 1
    
    System.out.println("Values after swapping: x = " + x + ", y = " + y);
  }
}

Output:

Values before swapping: x = 1, y = 2
Values after swapping: x = 2, y = 1


Solution #2: Using XOR Bitwise Operator

Aside from the arithmetic solution, the more technical way to solve this problem is to use the XOR bitwise operator. The XOR bitwise operator returns 0 if both operands are the same and returns 1 if both operands are different (refer to the XOR truth table below).

X Y X ^ Y
0 0 0
0 1 1
1 0 1
1 1 0

public class SwapVariablesDemo2
{
  public static void main(String[] args)
  {
    int x = 1; // 0001 in binary
    int y = 2; // 0010 in binary
    
    System.out.println("Values before swapping: x = " + x + ", y = " + y);
    
    x = x ^ y; // x = 3 (0011), y = 2 (0010)
    y = x ^ y; // x = 3 (0011), y = 1 (0001)
    x = x ^ y; // x = 2 (0010), y = 1 (0001)
    
    System.out.println("Values after swapping: x = " + x + ", y = " + y);
  }
}

Output:

Values before swapping: x = 1, y = 2
Values after swapping: x = 2, y = 1

If you want to impress your future employer during your programming exam, then I suggest that you use the XOR bitwise solution provided that you can explain the logic behind it.

Now, try asking your fellow developers this question and find out how many of them know the solution :)

Monday, December 14, 2015

Eclipse Plugin Tutorial: Add New Menu and Toolbar Items to Eclipse 3.x Plugin


This tutorial is based on Eclipse Mars and Java 1.7.

In the previous tutorial, I showed you how to create a basic Hello World Eclipse 3.x plugin using the plugin template wizard. This time I will show you how you can manually add (contribute) new menu and toolbar items to your own plugin.

Tutorial Outline:

Prerequisites
Command
Handler
Bindings
Menu Item
Toolbar Item
Run the plugin

Prerequisites

You have already created a basic Hello World plugin using the plugin template wizard.

Why are there only few female software developers?


When I was still on my first year in college, the very first thing I noticed was that I only had 12 fellow female computer science students out of 40 in my batch. The number decreased each year and by the time I graduated there were only last 5 women standing proud for surviving long hours of solving brain crunching programming problems. When I started with my first job, I was the sole female software developer in the company. With my second job, there were only two of us. For my current job, there were five of us in our department. I was already so used to a work environment where most of my colleagues are male. If you are like me who wonder why there are so few female software developers, then I came up with the following probable reasons.

  1. Most women don't like studying Math. Since Computer Science is a course that has lot of Math in the curriculum, it is a big turn off to female college students. I myself needed to put extra effort just to pass all of my Math and Physics subjects in college. Most of my female classmates simply gave up the course after failing their Math subjects.
  2. Most female software developers can't stand the stressful nature of the job. Seriously, every single day of my life as a software developer involves STRESS. Stress from tight deadlines, lots of sleepless nights trying to figure out how to solve bugs, the need to learn new programming language/framework, etc. I can't blame why most female software developers shifted to QA Engineers or management positions after a few years.
  3. Since the IT (Information Technology) is a male-dominated industry, there are some female software developers who feel awkward and lonely to be surrounded by male colleagues. I remembered on my first job, during lunch breaks all of my male colleagues are busy discussing stuffs like NBA, Dota and every single sports and computer games you could think of, while I was left alone on my cubicle reading Hollywood Gossip websites. Every time there is a job opening for software developer on my work, I always wish that the company will hire a female software developer so I will have someone to share girly talks with.
Those are some of my observations and opinions of why there are only few female software developers. If you have other opinions to share about this matter, let me know in the comment section below.

Friday, December 4, 2015

Eclipse Plugin Tutorial: Eclipse 3.x Plugin Development for Beginners


This tutorial is based on Eclipse Mars and Java 1.7.

Before I became an Eclipse Plugin developer, I used to wonder if it is possible to extend the Eclipse IDE to add an MP3 player so I can easily switch music without leaving the IDE. It turned out that it is possible because the Eclipse IDE is made out of several reusable software components called plugins. This means that you can write your own plugin to contribute a new functionality to the Eclipse IDE (like an MP3 player, pretty cool huh?).

For this tutorial, I will show you how to create a basic “Hello World” plugin using Eclipse 3.x framework which is the traditional way of creating Eclipse plugins. With the release of Eclipse version 4.x (E4), you can already create plugins based on a model and it also supports dependency injection and CSS styling. I will write a separate tutorial for E4 plugin development. So, let’s start!


Monday, November 30, 2015

R.I.P Internet Explorer 7 and 8


Last week, I received an office email that will forever change my life as a web developer. The email says that starting January 13, 2016, Internet Explorer 7 and 8 will no longer be supported by Microsoft and that means our company will also no longer support them in our future product releases. OMG, that is the best news ever!!! IE 7 and 8, let me take this opportunity to say my short final words to you.

Dear IE 7 and 8,

We had our good times and bad times (most of the time). I can't count how many sleepless nights I had trying to figure out what's wrong with my JavaScript and CSS codes and why it's working on most browsers except you. Although we had incompatibility issues in the past, I hope that we can remain as good friends. I wish you all the best in your future retirement. 

Sincerely,

Dev-Who-Waited-For-This-Moment-To-Come  

Saturday, November 28, 2015

Programming Jargon: Yoda Condition


I have been in the software development industry for almost 10 years but it was only recently that I learned about the programming jargon Yoda Condition

It all started when my code reviewer noticed an if-condition similar to the following:

if (stringVariable.equals(STRING_CONSTANT)) {

}
He pointed out that it’s not null pointer-safe so I said I’ll just change the condition into this:

if (stringVariable != null && stringVariable.equals(STRING_CONSTANT)) {

}
That was the time that he mentioned about a better way to rewrite the above if-statement using the Yoda Condition and I had a big question mark on my face upon hearing it. After asking Mr. Google, I found out that in Yoda Condition you have to place the constant on the left side of the comparison operator while the variable is on the right side. Obviously, it was named after the Star Wars character Yoda who speaks English using the form object-subject-verb (e.g. Apple I eat).