Saturday, July 31, 2010

Fun with Javascript --Flying images!!

Go to Google Images, search for any image so that you get a result page full of images.
Now in the address bar at the top copy and paste the following java code:

javascript:R=0; x1=.1; y1=.05; x2=.25; y2=.24; x3=1.6; y3=.24; x4=300; y4=200; x5=300; y5=200; DI=document.getElementsByTagName("img"); DIL=DI.length; function A(){for(i=0; i-DIL; i++){DIS=DI[ i ].style; DIS.position='absolute'; DIS.left=(Math.sin(R*x1+i*x2+x3)*x4+x5)+"px"; DIS.top=(Math.cos(R*y1+i*y2+y3)*y4+y5)+"px"}R++}setInterval('A()',5); void(0);

and press Enter, and watch the images fly.


Bad code looks like this..

Just came across this pic which reminded me of the display of a smelly code.. Save projects to look like one below.. :)



Friday, July 30, 2010

New Collection APIs in Java-6!!

New interfaces:
  • Deque
  • BlockingDeque
  • NavigableSet  
  • NavigableMap
 New Classes
  • ArrayDeque
  • LinkedBlockingDeque
  • ConcurrentSkipListSet
  • ConcurrentSkipListMap
  • AbstractMap.SimpleEntry
  • AbstractMap.SimpleImmutableEntry
Updated classes of java 6
  • LinkedList
  • TreeSet  
  • TreeMap
  • Collections
Classes are modified to implement the new interfaces in the existing classes.LinkedList is modified to implement Deque. TreeSet is modified to implement NavigableSet.TreeMap is modified to implement NavigableMap.In Collections 2 new methods newSetFromMap and asLifoQueue are added.

Thursday, July 29, 2010

What is the difference between char *a and char a[]?

There is a lot of difference!


char a[] = "rupali";

char *a = "rupali";

The declaration char a[] asks for space for 7 characters and see that its known by the name "a". In contrast, the declaration char *a, asks for a place that holds a pointer, to be known by the name "a". This pointer "a" can point anywhere. In this case its pointing to an anonymous array of 7 characters, which does have any name in particular. Its just present in memory with a pointer keeping track of its location.

char a[] = "rupali";


+----+----+----+----+----+----+------+

a: |r|u|p|a|l|i|'\0\'| 


+----+----+----+----+----+----+------+

a[0] a[1] a[2] a[3] a[4] a[5] a[6]

char *a = "rupali";

+-----+           +---+---+---+---+---+---+------+


a: *======> |r|u|p|a|l|i|'\0'|

+-----+          +---+---+---+---+---+---+------+

Pointer          Anonymous array

It is curcial to know that a[3] generates different code depending on whether a is an array or a pointer. When the compiler sees the expression a[3] and if a is an array, it starts at the location "a", goes three elements past it, and returns the character there. When it sees the expression a[3] and if a is a pointer, it starts at the location "a", gets the pointer value there, adds 3 to the pointer value, and gets the character pointed to by that value. If a is an array, a[3] is three places past a. If a is a pointer, then a[3] is three places past the memory location pointed to by a. In the example above, both a[3] and a[3] return the same character, but the way they do it is different! Doing something like this would be illegal. char *p = "hello, world!"; p[0] = 'H';

Friday, July 23, 2010

Java 5 features

Java 5 features for easier dev: Generics, Autoboxing/Unboxing, Static Import, Enhanced for, Typesafe enums, Annotaion/Metadata

I had to give a presentation on Java 5.0 few weeks back in my company and during that time I went through an interesting interview (taken sometime in 2003) of Joshua Bloch who touch based on the six popular new features of Java 2 Platform, Standard Edition 5.0 (also knwon as J2SE 5.0) including Generics, Autoboxing, Static Imports, Annotations, Enhanced for loop, and Typesafe Enums.
He talked about how these new features are going to be accepted by the developers worldwide and how actually will these features make application development in Java easier, safer, and more robust. Bloch is an architect at Sun Microsystems and he has been involved in the design and implementation of several Core Java features including the highly regarded Collections Framework and the java.math package. He has authored the Jolt award winning book named "Effective Java".

Before we move ahead let's discuss what the six major new features (aimed towards ease of development) introduced in Java 5.0 are all about.


  1. Generics: This feature is used to ensure compile-time safety for Collections and eliminates the need for having the casts. The feature guarantees the code using Collections won't throw the infamous runtime exception named 'ClassCastException' as such cases can be detected at compile-time itself if the programmer uses Generics. A big relief, isn't it?
  2. Autoboxing/Unboxing: this feature is used to make the automatic casting possible between the primitive data types and their corresponding wrapper data types. For example: an 'int' can now be assigned to a reference of type 'Interger' and vice-versa. The compiler automatically takes care of this.
  3. Static Import: remember using Interfaces just for using static constants in Java programs. Interfaces are not meant for that, instead they should be used for defining types. Using them just for the sake of using constants not only defeats the actual meaning of interfaces, but it also makes the code less flexible as implementing an interface is a public contract and even if you plan not to use the constants defined in the interface in the newer implementations of the class then also you got to maintain the contract as the clients might have used the interface as a data type for the implementing class in their code. Static import actually imports all the static members of a class/interface making them available to be used with their simple names and thus you can avoid implementing the interfaces for using the constants.
  4. Enhanced for-loop: this feature makes the for-loop more compact. The iterators now don't need to be explicitly checked for boundary conditions.
  5. Annotations/Metadata: this feature helps the programmer by letting the tools generate the obvious code just by supplying the corresponding annotation tags. It makes the programming more "declarative" in nature.
  6. Typesafe enums: most of the shortcomings of the enums which previously require lot of coding around their use to ensure safe usage have now been resolved in this new version of enums which are completely Typesafe and additionally they can be used with switch statement as well.

Thursday, July 22, 2010

StringBuilder

Just to add my experience working with StringBuilder:

StringBuilder  hasn't got an overridden equals() method, so if you want to see whether two StringBuilders have the same contents try

builder1.toString().equals(builder2.toString()).

For example:

StringBuilder a = new StringBuilder("rupali");
StringBuilder b = new StringBuilder("rupali");

If we want to check if content of a and b are same (as we do in StringBuffer or String class with equals() method)

if (a.equals(b))
{ System.out.println("Equal"); }
else {System.our.println("Not equal"); }

Unlike String or StringBuffer, this will give "Not equal" as answer.

The correct implementation to check equals() in case of StringBuilder is:

if (a.toString().equals(b.toString()))
{ System.out.println("Equal"); }
else {System.our.println("Not equal"); }


The answer is "Equal".

Criteria to choose among String, StringBuffer and StringBuilder

   1. If your text is not going to change use a string Class because a String object is immutable.
   2. If your text can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.
   3. If your text can change, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous.

Thanks
Rupali

Go experimenting

Lookin to the code below

public class CrazyJava {

public static void main(String[] args) {

String arg1 = args[0];
String arg2 = args[1];
String arg3 = args[2];

System.out.println(arg1+arg2+arg3);
}


}

Just taking 3 command line arguments and print the result of concatenation.

Ok, lets compile the code
javac CrazyJava

now run as
java CrazyJava1

This will throw AIOB (ArrayIndexOutofBound)exception..you are correct
Note it and then run i now as following

java CrazyJava *


Amazing :) it doesn't give any error / exception but it prints the file(s) names which are present in the current directory (from where you are running the java ...)

why java is not behaving crazy when the command line argument is *
Is it a bug in Java ? It can't be. There is something behind the scene.

cheers,
Rupali