Showing posts with label programming language. Show all posts
Showing posts with label programming language. Show all posts

05 July 2011

Java String comparison

A common mistake you might make when doing Java string comparison is like this:

String s = "abs";
String t = "cde";

// test for pointer equality, in other words,
// test if s and t are the same object
if (s == t)


What you really want to do might be this:

if (s.equals(t)) // compare characters in two strings


However in python, s==t is actually what you want. When you know too many programming languages, you can easily mess up the syntax and semantics of them accidentally. Be careful.

22 February 2011

[Technotes] Java Inner classes

First thing first, there is some terminology that needs to be explained.

In Java, you can define a class inside another class; such a class is called nested class. Nested classes can be divided into two categories: static or non-static. Static nested classes are simply called static nested classes. Non-static nested classes are called inner classes.

As for nested classes' privileges, inner classes have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class, instead, it can only access to static methods and members of the enclosing (parent) class.

To make the story even more complex, there are 2 other flavors of 'inner classes': local classes and anonymous classes (also called anonymous inner classes). You get to see lots of these two special kinds of inner classes in GUI codes. So you must understand and master them to be able to do GUI programming in Java. To see some concrete examples of these two classes, check out here and there.

In addition, you'd better read the following three web pages carefully before using inner or nested classes:

http://bit.ly/fPoJad - Nested classes introduction
http://bit.ly/dQzVvD - Inner class code example

Finally, to instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

14 February 2011

07 December 2010

Deep copy vs. Shallow copy vs. Lazy copy

There are basically three strategies for copying objects; each has its own advantages and disadvantages.

Shallow Copy

Shallow copy is basically passing references, not actually copying data inside objects. That is why shallow copy is otherwise known as address copy. The main advantage of shallow copy is it is very fast and does not depend on the size of data; however, if the language does not support automatic garbage collection, a shallow copy could lead to a memory leak if not cautious enough. In addition, two object references refer to the same data block on memory after shallow copy.

Deep Copy 

This is the opposite of shallow copy. The data from one object is actually copy over to another object. Therefore, two object bare the same data after deep copy and yet there are still different objects! The disadvantage of this strategy is it is slow and expensive; however, deep-copied objects do not depend on each other and are independently modifiable.

Lazy Copy

A lazy copy is a combination of both strategies above. When initially copying an object, a shallow copy is used. A counter is also used to track how many objects share the data. When the program wants to modify an object, it can determine if the data is shared and can do a deep copy if necessary. This is also very similar to 'copy-on-write' technique used in most OSes.

So, how are copy actually implemented in programming languages?

In Java:  

Using clone method of a class.

In Python: 

the library's copy module provides shallow copy and deep copy of objects through the copy() and deepcopy() functions, respectively. Programmers may define special methods __copy__() and __deepcopy__() in an object to provide custom copying implementation.