Posts

Showing posts with the label parameter

Java Enum in details

Java 5 has brought linguistic support for enumerated types. An enum type is a type whose fields consist of a fixed set of constants. The enum provides following features Typesafe Namespace Printable constants values Typical and often enum is being used to replace int constants. For example constants for color codes public static final int RED = 0; public static final int GREEN = 1; public static final int BLUE = 3; public static final int WHITE =4; public static final int BLACK = 6;   This code can be replaced by following enum public enum StandardColor { RED,GREEN,BLUE,WHITE,BLACK } Given the above enum declared, we can use the same to communicate the color preferences. For example we have Window class and it supports setting background color using setBackgroundColor(StandardColor color) . One can call setBackgroundColor method on Window's instance to set background color, as shown in code below public class Wind...

Java Puzzle : Function parameters

Here is another Java puzzle, but this time you don’t need to find output of this program. Instead just complete the code snippet to get desired output. Here is the program : public class OPuzzle { public static void main(String[] args) { Object o = null; printMe(o); } private static void printMe(<Provide the declaration here> o){ if (o!=null){ System.out.println( "I am not null" ); } } } Desire output : I am not null Replace the <Provide the declaration here> with appropriate declaration to get above desired output. Leave your comments. Reference : http://google-opensource.blogspot.com/2011/04/geek-time-with-josh-bloch.html   No no no … don’t see reference before solving :).