A simple design question.
Sample code:
Integer int1 = new Integer(20);
System.out.println(Integer.toBinaryString(int1));
Why JDK design is not something like the following? so, toBinaryString function returns the the desired result?
System.out.println(int1.toBinaryString());
Aside for wide usability of a static function, what are the other reasons for this design approach? Are they using any particular design pattern? If it is then, which pattern?
Your sample code creates an instance of
Integerand then unboxes it. There’s no need for that:If it were an instance method, you’d be forced to create an instance of
Integersolely to convert anintto its binary representation, which would be annoying.In other words: to avoid creating objects unnecessarily.