Here is simple and concise way to learn about the new java 7 feature and new Exception criteria added which give you confident in interview and as well as while writing the code in your project with functionality.
So Let’s Start:
Binary Literals:
Java added a new feature Binary Literal in Java 7. It allow you to express integral types (byte, short, int, and long) in binary number system. To specify a binary literal, add the prefix 0b or 0B to the integral value.
Example:
Binary literal represent in byte type:
· Using b0, The b can be lower : byte b1 = 0b101;
· Using b0, The b can be upper case : byte b2 = 0B101;
you can compile and run the code in: www.codechef.com
System.out.println(“Binary Literal in Byte”);
System.out.println(“b1 = ” + b1);
System.out.println(“b2 = “+ b2);
output:
b1 = 5
b2 = 5
String in Switch Statement Java
Java allows you to use string objects in the expression of switch statement.
In order to use string, you need to check below important points:
It must be only string object:
Object game = “Param”; // It is not allowed other wise will get Exception
details: incompatible types: Object cannot be converted to int at switch statement.
String Example:
String game = “Param”; its fine will work well.
Case sensitive:
String objects are case sensitive:
example: “Param” and “param” are not equal object of string.
No Null Object:
Always be careful while passing string object, passing a null object cause to NullPointerException.
please refer below piece of code for better understanding:
| Object employee = “A”; switch(employee){ case “A”: case”B”: case”C”: System.out.println(“This is employee of abc company”); break; case “D”: case”E”: case”F”: case”G”: System.out.println(“This is this is employee of xyz compay”); break; default: System.out.println(“Which company employee he is?”); } |