close
Showing posts with label certification. Show all posts
Showing posts with label certification. Show all posts

Monday, 23 May 2011

Primitive Wrapper Classes and Their Factory Methods

The java SDK primitive wrapper classes such as Integer, Boolean and Long come with a whole bunch of often ignored factory methods that can be used to improve performance. Indeed, the best advice when converting a "true" or "false" is to use

      val = Boolean.valueOf("false");

rather than

Tuesday, 17 May 2011

Are finalize() Methods Useful?

Under normal circumstances, the finalize method is not of much use when programming everyday java code. Indeed, Joshua Bloch states, in his book Effective Java, that "finalizers are unpredictable, often dangerous, and generally unnecessary"; given this view, it's not surprising that the section of the book I'm referring to is called 'Item 6: Avoid finalizers' (see Page 20).

Saturday, 7 May 2011

Java Primitive Sizes

The sizes of Java primitives are:

TypeMin ValueMax ValueBits
byte-1281278
char06553516
short-327683276716
int-2147483648214748364732
long-9223372036854775808922337203685477580764

Tuesday, 26 April 2011

The Difference Between Method Overloading and Method Overriding

When taking the Oracle Certified Professional, Java SE 6 Programmer exam, one of the tricks used to try to catch you out is to try to confuse you over the meanings of overriding and overloading methods and I guess that this is because they have similar sounding names. This blog clarifies all that - take a look at the code...

Monday, 25 April 2011

hashCode() Tips.

Did you know that a hash code MUST be a positive value and that String uses the hashCode() method inherited from Object.

Sunday, 24 April 2011

Exception Ordering

Exceptions caught in a catch block must be caught in order of precedence, starting with the most specific and ending with the most general. The following code will compile:

Saturday, 23 April 2011

The Evolving switch Statement

One thing about working in programming is that things change; you can count on it, new things are written, tools become in vogue and techniques go in and out of fashion. One thing that changes somewhat less often is the Java language itself, though it is evolving and for the better.

Consider the humble switch statement: at the time of J2SE 1.4 you could make a choice based upon an int value:

Friday, 22 April 2011

chars and shorts: useful stuff to know?

It seems logical that the compiler should be able to automatically convert a char into a short, after-all they’re both 16 bits wide...

Wednesday, 20 April 2011

Floating Point and Divide By Zero

Did you know that dividing a double by zero will not throw an ArithmeticException? This seems rather inconsistent to me as dividing an int by zero will.

Friday, 8 April 2011

Double.NaN Definition

The Double class defines a little understood value of NaN - or ‘Not A Number’. This sounds pretty simple at first glance, after-all an alphanumeric String is not a number - isn’t it?

Thursday, 7 April 2011

Comparing StringTokenizer and String.split(...)

People often say that String.split(...) and the older StringTokenizer are interchangeable and that these days you should use Sting.split(...) in preference to StringTokenizer. But, are they as interchangeable as they’re made out to be or are there some input conditions where there results different? The code below tests this theory using four input strings:

Wednesday, 6 April 2011

Different Ways to Create Objects

There are at least four ways to create a new object:

1) Using new

Friday, 25 February 2011

Modulo and Negative Numbers

Did you know that the modulo of a negative number is negative? In the presumed words of Micheal Caine it seems that “not a lot of people know that”. For example its obvious that:

5 % 2 = 1

but it isn’t so obvious that:

Thursday, 17 February 2011

Exceptions and Overriding

When you extend a class and override a method, the Java compiler insists that all exception classes thrown by your overridden method must be the same as, or subclasses of, the exception classes thrown by the original method in the base class. The code below demonstrates a base class being overridden by various subclasses.

Saturday, 12 February 2011

Implementing the equals() hashCode() Contract

The Java documentation for the Object.equals() method states that: "it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes." This is rather vague, is it necessary, or just a good idea to implement hashCode() when you override equals()?

Friday, 11 February 2011

String and the StringBuilder Usage

One common mistake you often see is code that relies on the repeated concatenation of strings using the String class. String concatenation using the String class okay when all you need to do is concatenate one string to another, but when it comes down to concatenating multiple strings, this method generates serious time penalties.

Thursday, 10 February 2011

Arithmetic Precedence

The order for arithmetic precedence is:

* / % + -

Wednesday, 9 February 2011

Floating Point Numbers

Floating point numbers have the following range:
  • Double.NEGATIVE_INFINITY or Float.NEGATIVE_INFINITY
  • Negative Values
  • -0.0
  • 0.0
  • Positive Numbers
  • Double.POSITIVE_INFINITY or Float.POSITIVE_INFINITY

Monday, 7 February 2011

Using java.util.Collections - 2

Yesterday’s blog, contained a quick example of using Collections.reverse(), which, from experience, is something that you don’t have to do that often; so today’s post demonstrates a more useful example. Now, one thing you often need to do is to write a method that returns a Collection (eg a List, Set, Map etc.) to its caller.

Sunday, 6 February 2011

Using java.util.Collections - 1

One of the most useful classes is the java.util.Collections consisting of static methods that operate on or return collections. So if you need to reverse or sort a list, for example, then don't write the code yourself just look here!