Some amazing java features



 Goto initialization;

// code goes here
 
getmeout:{
    for (int i = 0; i < N; ++i) {
        for (int j = i; j < N; ++j) {
            for (int k = j; k < N; ++k) {
                //do something here
                break getmeout;
            }
        }
    }
}



Null Check : 

Haven't seen anyone mention instanceof being implemented in such a way that checking for null is not necessary.
Instead of:
if( null != aObject && aObject instanceof String )
{
    ...
}
just use:
if( aObject instanceof String )
{
    ...
}


Parameter passing: 

As of Java 1.5, Java now has a much cleaner syntax for writing functions of variable arity. So, instead of just passing an array, now you can do the following

public void foo(String... bars) {
   for (String bar: bars)
      System.out.println(bar);
}

bars is automatically converted to array of the specified type. Not a huge win, but a win nonetheless.



Not really a feature, but an amusing trick I discovered recently in some Web page:
class Example
{
  public static void main(String[] args)
  {
    System.out.println("Hello World!");
    http://Phi.Lho.free.fr
 
    System.exit(0);
  }
}
is a valid Java program (although it generates a warning). If you don't see why, see Gregory's answer! ;-) Well, syntax highlighting here also gives a hint!

String secrets

Can you guess what is the output of this statement in Java
 
System.out.println(1+2+” = “+1+2);
 
3=12
Why? Apparently Java starts treating everything as a String once it has encountered a string in System out statement 

Comments

Popular posts from this blog

SinglePass Terms of Service

Jasper Report Viruatization

JasperReports Tutorial