Saturday, February 4, 2012

Java 7 - Binary Literals

Java 7 enables developers to 'create' and assign binary values to integral types (byte, short, int, and long) . Binary values starts with 0b or 0B followed by and combination of 0s and 1s. For instance

0b1111 is equivalent to 15.

You can perform arithmetic operations as well as comparisons. Below is the code which demonstrates the use of binary literals

package com.benjmaz.literals;
/***
 * 
 * @author Ben Mazyopa
 * The purpose of this code is to illustrate the use of binary strings
 */
public class BinaryLiterals {

 /**
  * @param args
  */
 public static void main(String[] args) {
  //assigns 15 to x
  int a=0b1111;
  //assigns a values decimal
  int b=15;
  long c=a;
  float d=-0b100111;
  
  // A 64-bit 'long' value. Note the "L" suffix:
  double e= 0b1010000101000101101000010100010110100001010001011010000101000101L;
  // A 64-bit 'long' value. Note the "L" suffix:
  long f = 0b1010000101000101101000010100010110100001010001011010000101000101L;
  
  //adds 15 + 15 and stores to ans
  int ans =a+b;
  //prints 30 = 15 + 15
  System.out.format("%d = %d + %d%n",ans,a,b);
  //prints true = 15 == 15
  System.out.format("%s = %d == %d%n",c==a,c,a);
  //prints d = -39.0
  System.out.println("d = " + d );
  System.out.println("e = " + e );
  System.out.println("f = " + f );
 }
}

The above code will output the following

30 = 15 + 15
true = 15 == 15
d = -39.0
e = -6.8258723397796086E18
f = -6825872339779608251

For more information consult oracle technotes

Java 7 Language Enhancements - Use String in the switch statement

Switch statement is used to evaluate expressions based only on a single integer, enumerated value, or String object. Therefore it cannot be used to evaluate a range expression.

Before Java 7, strings were not supported in the switch statement. Below is the code which demonstrates the use of a switch statement using a string expression

com.benjmaz.string

public class SwitchDemo {
 public static void main(String[] args){

        String ans="A";
        switch (ans){
            case "C":
             System.out.println("C is the answer");
             break;
            case "B":
             System.out.println("B is the answer");
             break;
            case "D":
             System.out.println("D is the answer");
             break;
            case "A":
             System.out.println("A is the answer");
            default:
             System.out.println("This is the default answer");
        }
    }
}

What would be output? Yes you guessed right! The output will be

A is the Answer
This is the default answer

The default statement was execute since case "A" block is not terminated by break, hence execution continues to the default block

Lets look at the second scenario. We have opted to embed the default block in the midst of other statements. Analyze the code and suggest as to whether the code will compile or not, and if it compiles what would be the result

com.benjmaz.string

public class SwitchDemo {

 public static void main(String[] args){
        String ans="X";
        switch (ans){
            case "C":
             System.out.println("C is a the answer");
             break;
            default:
             System.out.println("This is the default answer");
         case "B":
             System.out.println("B is a the answer");
             break;
            case "D":
             System.out.println("D is a the answer");
             break;
            case "A":
             System.out.println("Buza! A is the answer");
        }
    }
}

The above code will compile and the output will be

This is the default answer
B is the answer

Note the "B is the answer" is printed as well due to the fault that break statement was omitted in the default block hence case B block was executed as well.

If you are working out towards Java 7 certification, you need to be mindful and alert on all the possible ways (legal and illegal) how the switch statement can be constructed