Sunday, March 22, 2009

Breaking information hiding in Java

Information hiding is one key feature of Java in particular, and OO in general. It is not to be confused with encapsulation, since you can bundle data with your methods and not hide it at all. Even though these two (information hiding and encapsulation) are different, they are usually used together by making the attributes of your class private data members and providing getter (accessor) and setter (mutator) methods to them. Having said that, let us look at an example:

public class InformationHiding {
   private Position position;

   public InformationHiding(double latitude, double longitude)
      throws IllegalArgumentException {
      //make sure you have proper values for latitude and longitude
      if (latitude >= -90 && latitude <=90 && longitude >= -180 && longitude <= 180) {
         throw new IllegalArgumentException();
      }
      position = new Position(latitude, longitude);
   }

   public Position getPosition() {
      return position;
   }
}

Can you spot the problem with this code (beside the lack of synchronization)? Let's see how we can break the check for proper values of latitude and longitude:

InformationHiding inf = new InformationHiding(45, 120);
Position pos = inf.getPosition();
pos.latitude = -100; //illegal value
pos.longitude = 240; //illegal value

How can that be? We did make the position data member private? The problem is that we returned a reference to that member. This is a common mistake. Even when you generate the getter method from inside our IDE (i.e. Eclipse), it returns a reference to the attribute, rather than a copy to it. How can we fix this? Easy, just return a copy of the data member:

public Position getPosition() {
   return new Position(position.latitude, position.longitude);
}

That's it! You could have used the clone method to make a copy of the object, but then some changes need to be made, like not implementing clone by using the constructor.

There are some cases when we do not need to return a copy of the data, namely when we deal with immutable objects (like Strings and the wrapper classes).

Thursday, March 19, 2009

Android 1.1 and iPhone 3.0

If you want a feature-by-feature comparison of Android and iPhone, read the Android Versus iPhone 3.0 article by lifehacker. Do remember that Android is only in its 1.1 release, while iPhone has seen its 3.0 version. Even though iPhone has more available features, let us not forget that Android is open source.

Sunday, March 15, 2009

Smartphone sales numbers

According to a Gartner report, smartphones sales grow by almost 14% compared to 2007. What is encouraging are the news regarding Android-based smartphones:

"Sales of Linux-based smartphones were up by 19 per cent year-over-year, mainly through Android-based smartphones being available through T-Mobile during the fourth quarter of 2008."

I expect this number to grow with upcoming Android-based smartphone models.

Thursday, February 12, 2009

Top 25 Most Dangerous Programming Errors

SANS Institute came out with a top 25 most dangerous programming errors, errors which can lead to serious security breaches.

"... experts from more than 30 US and international cyber security organizations jointly released the consensus list of the 25 most dangerous programming errors that lead to security bugs and that enable cyber espionage and cyber crime. Shockingly, most of these errors are not well understood by programmers; their avoidance is not widely taught by computer science programs; and their presence is frequently not tested by organizations developing software for sale."

Tuesday, February 10, 2009

Android Presentation

Today I did a presentation to a group of students on Android, basically an overview of the platform. If you are interested, you can find the presentation here. Any comments are welcomed!

Thursday, January 22, 2009

BOLT - the Java-based Browser

What intrigued me about BOLT (which is in a beta version at the moment of this writing) is the fact that it is written in Java (Java ME). It is designed for entry-level phones (with MIDP 2.0 and CLDC 1.0 or higher), but certainly it runs just fine on smartphones too. It is considered to offer fast and secure web browsing. How secure? 128-bit SSL Connections, filtering done on the server to protect you from malicious code, certification error notifications, and pop-up blocker. How fast is it? Reports have mentioned that on a Nokia 6120 (which runs on the S60 platforms), BOLT loaded the phonearena.com website in 14 seconds, while the built-in browser loaded it in 40 seconds.

BOLT provides ECMA Script 262 JavaScript support (asynchronous java script will work) , but does not currently offer support for Java Applets. Data reductions and lower power consumptions are also achieved.

If you are looking for screenshots (and further test results), head to phonearena.

Top 5 Cellular Phone Producers for 2008

Based on reports from PhoneArena, here is a list of the top 5 cellular phone producers in the global market for 2008:

  1. Nokia - 470 million units sold.
  2. Samsung - 200 million units sold.
  3. LG - 100 million units sold.
  4. Motorola - 99.9 million units sold.
  5. Sony Ericsson - 96.6 million units sold.
I am still hoping Motorola will come through (eventually) and will revive its mobile devices unit. Until then, another 4000 jobs are expected to be lost (beside those 3000 from November of last year).