Scala does not have checked exceptions

Good! so, the following code will not impact your method signature

def doSomething(n:Int) = {
if (n 10) {
throw new Exception("Only values between 1 and 10")
}
...

Leave a Comment

vim 7 spell check

vim as of version 7 had introduced built-in spellchecker. I had not used till date, but recently checked it out. So, to turn it on, type command
set spell
If you have some text on your vim and you turn sell on with above command, you will see red underlines for words that vim thinks are incorrectly spelled. Of course, while coding you might not want spell check, so you can turn it off by
set nospell
Now, about navigation and correction.
To go to next spelling error, the keyboard command is ]s to go to previous error, the command is [s. To see spelling suggestions, while the cursor is on word with error, press =z and it presents a list of possible words to choose from. Just type the number corresponding to the word that you are interested in, and you are all set.

Leave a Comment

Java Generics puzzler

Ok, so you are very excited about Generics and would “Generify” whatever you can. But here some interesting pointers.

One: This will cause a compilation error


 Set<String> set = new HashSet<String>();
 set.add("Testing");
 set.add("Testing again");
 set.add("Testing again and again");
 set.add(new Integer(1));

Okay, that we all know.

Two: Now, what will happen with this?


  public static void main(String[] args){
    Set<String> set = new HashSet<String>();
     set.add("Testing");
     set.add("Testing again");
     set.add("Testing again and again");
     doSomething(set);
     System.out.println(set);
   }
   private static void doSomething(Set s){
     s.add(new Integer(21));
   }
}

This will compile fine (With a warning actually, that we all would ignore) and print contents of the set, including a number.

Three: Well, how to fix this ? How to throw an error when a legacy code is attempting to change the sole purpose of your HashSet? The answer is to declare your set as

Set<String> set = Collections.checkedSet(new HashSet<String>(),String.class);

Leave a Comment

If you want to add an Object to java.util.TreeSet, it must implement Comparable

public class Employee{
  private String name;
  Employee(String str ){
    this.name= str;
  }
}
public class Test {
  public static void main(String argv[]){
    TreeSet myMap = new TreeSet();
    Employee s3 = new Employee("abcdef");
    Employee s4 = new Employee("abcdef");
    myMap.add(s3);
    myMap.add(s4);

This code will throw a
Exception in thread "main" java.lang.ClassCastException

Unless Employee implements comparable

Leave a Comment

Java Splitting string based on space

Quick and easy way to do this is :

stringVariable.split("\\s");

Leave a Comment

Hibernate Search ( with lucene) quick tip

By default, all the indexing done by lucene is stored in lower case. So, whenever constructing a WildCardQuery or TermQuery, remember to use smaller case matching criteria by default.
Also, never under-estimate luke, the lucene index toolbox

Leave a Comment

Maven POM

The Project Object Model (POM) describes :

  • Project co-ordinates (name, version..)
  • Project type (war,jar,ear..)
  • Source structure
  • Build Phases (standard | custom..)
  • Dependencies
  • Plug-ins

Leave a Comment

Java pass-by-value

After a few recent Java interviews, I want to re-affirm that
“Everything in Java is pass-by-value” – repeat after me.
There are so many twisted descriptions and statements that makes you believe that Java passes-by-reference and so, in a method you can change where the pointer corresponding to method argument points.

Comments (2)

Enable USB drive plug and play if disabled by your admin

psst…
Hope you have admin rights on your windows XP workstation.
Goto
registry editor (regedit.exe)
edit the Value “Start” under
HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/USBSTOR to 3

To set back to “disabled”, edit same value set to 4.

Leave a Comment

Java: volatile and synchronized

Call it attention deficiency or getting old, I need to remind myself every often what is volatile - hm? or may be because I never used it.
volatile : field modifier. Synchronizes value of a given variable between thread memory and “main memory” – you are guaranteed to get the most recently written value (thanks Josh Bloch) of the variable whenever you refer it.
synchronized : modifier for blocks and methods. Synchronizes value of *all* variables between thread memory and “main memory” ; locks and releases monitor.
While volatile make sure that the thread’s variable has the latest value of main memory, it may supply corrupt data while a change to that variable is in progress. synchronized allows you to lock an execution path until you are done completely modifying the shared resource.

Leave a Comment

Older Posts »