Tricky example with polymorphism | Blinded by the lights
Can you predict the output of the following code? class SubTest extends Test { public int aNumber; public SubTest() { aNumber = 17; } public void doubleANumber() { System.out.println(“Inside SubTest.doubleANumber()”); aNumber *= 2; } } public class Test { public int aNumber; public Test() { aNumber = 6; } public void doubleANumber() { System.out.println(“Inside Test.doubleANumber()”);…
java.util.IllegalFormatConversionException when using System.out.format() | Blinded by the lights
A few weeks ago I described how to format output using System.out.printl() and System.out.println() methods… Try to run the code below: double avgAge = 245 / 34; System.out.format(“Average age is %d.”, avgAge); Without doubts you’ll get an exception thrown at runtime: Average age is Exception in thread “main” java.util.IllegalFormatConversionException: d != java.lang.Double at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)…
Collections – sorting and searching | Blinded by the lights
In order to sort a Collection, one should use java.util.Collections class as follows (assuming ArrayList will be used): List another object the drawback: there’s only one way of sorting Example of the class implementing Comparable interface: class Book implements Comparable { private String title; public String getTitle() { return title; } public void setTitle(String title_p)…
Generics | Blinded by the lights
Mixing generics with legacy code The above code won’t compile since all instances of a generic class have the same runtime class, regardless of their actual type parameters. Instead, you should do as below: This is also worth a look: Polymorphism and generic methods It’s the same with methods: The above method only accepts List<Superclass>,…
Java: == might work the same as equals() | Blinded by the lights
It’s a rule that == is used to compare references, not values. To compare values for equality equals() method should be used. However, as of Java 5, you can also use == to check if two objects’ values are the same… But it works only for the following wrapper objects, if their primitive counterparts’ values…
Formatting output | Blinded by the lights
Usually to print some ouptut you’d use System.out.printl() or System.out.println() method. It’s easy to use but you can face some difficulties in providing output formatted in a specific way. To format output you can use either of the following methods (they work the same) System.out.format(String format, Object… args) System.out.format(Locale l, String format, Object… args) System.out.printf(String…
GlassFish Training Offer | Blinded by the lights
For a long time I haven’t written a word here… The main reason is I was moved to a new project and am so busy I simply don’t have time to describe solutions to problems I’ve been facing. This project is again not Java based – .Net. To be honest, I haven’t written anything serious…
Unreachable catch block | Blinded by the lights
Always contruct try-catch blocks in a way that assumes catching Exceptions from more detailed to more generic. Example try { String [] tab = null; System.out.println(tab[3]); } catch (Exception e) { System.out.println(“Exception”); } catch (NullPointerException e) { System.out.println(“NullPointerException”); } The above presented snippet will cause compilation error: Unreachable catch block for NullPointerException. It is already…
Sun Certified Java Programmer | Blinded by the lights
Okay, I’ve been working with Java for a few years now, starting from academia and ending with business solutions. So yeah, I think I can say I know Java and some of those technologies like J2SE, J2EE or J2ME, with their own standards and solutions. Therefore, I’ve decided I’d take Sun Certified Java Programmer (SCJP)…
What if you extend an abstract class and implement an interface when both define a method with the same name? | Blinded by the lights
Imagine you need to implement a class that extends an abstract class and implements an interface when both define a method with the same name – test(). Example interface Implementable { public void test(); } abstract class Superclass { public abstract void test(); } public class Test extends Superclass implements Implementable { /* definition of…
- 1
- 2