0

I found a code like given below

public static final Object[][] TABLE_COLUMNS = {
            { "HistoryDate", new Integer(Types.TIMESTAMP) }, //java.sql.Type


            { "id", new Integer(Types.VARCHAR) },                 

            { "SessionId", new Integer(Types.VARCHAR) }};

is above code effective and how? May be I am misunderstanding above code
Type.VARCHAR=12. Can't we write Integer.valueOf(12);.

AmitG
  • 10,365
  • 5
  • 31
  • 52

5 Answers5

4

Yes, you could just use the value of 12, but that would be a magic number and they are generally considered bad practice.

What is a magic number, and why is it bad?

Community
  • 1
  • 1
Colin D
  • 5,641
  • 1
  • 23
  • 35
3

This array seems to store column names and their data types (the use of Object[][] is questionable but effective):

You could write Integer.valueOf(12) but the readability would be completely gone. (Strictly speaking you could write

{ "HistoryDate", Types.TIMESTAMP }

and let autoboxing do the rest.)

Simon Hellinger
  • 1,309
  • 12
  • 19
  • 2
    autoboxing is NOT recomended here, since there is a internal Integer cache for values between 0 and 128, it would be the same object ( for 12 here) if you are going to transfer it into a map. – Martin Seeler Apr 02 '13 at 12:17
  • 1
    @Chasmo And since we're talking about a fixed set of elements, how would that be a bad thing? We want them to be the same, right? I would suggest a Map or at least an object holding those two values, anyways. – Simon Hellinger Apr 02 '13 at 12:19
  • 1
    @Chasmo What you are trying to say is not very clear... Yes all `12` will be the same `Integer` instance but how is that an issue? – assylias Apr 02 '13 at 12:25
  • 1
    Well, i think there is no issue with that. It was more my hasty thought of the general definition of sets, where no repeated values are allowed, although it is allowed in the java sets and maps through the different keys. So feel free to autobox the much you like ;-) – Martin Seeler Apr 02 '13 at 12:36
  • 1
    @Chasmo Let's hope nobody takes your last sentence out of context :-D – Simon Hellinger Apr 02 '13 at 12:39
2

Of course you can write new Integer(12), but that would decrease the readability a lot. If someone other gets this code in his hands, he won't have any idea what that means. In this case, he und you knows that it references to the constant of type VARCHAR in MySQL types.

Martin Seeler
  • 6,874
  • 3
  • 33
  • 45
2

Why is your proposal an improvement? I think using the Types is far better documentation. It makes perfectly clear what's going on in a way that "12" never could.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • so then we could have written like `Types.VARCHAR` instead of `new Integer(Types.VARCHAR)` – AmitG Apr 02 '13 at 12:28
1

Yes you can, but that's the purpose of constants, right? If someone decides to changes the value of VARCHAR to 1337, you won't have to change your code...

Vincent van der Weele
  • 12,927
  • 1
  • 33
  • 61