2

As I am slowly progressing through the java tutorials, I came across java helper classes. The code is

public static void main(String args[]) {
char myLittleChar = 'b';
char myBigChar = Character.toUpperCase(myLittleChar);
System.out.println(myBigChar);

However, it worked the same even when assigning directly to a helper class.

Character c = 'a';
System.out.println(c.toUpperCase(c));

and the same applied for short or float as well.

However, I didn't come across this much in any tutorial, or any sample code, and is declared as int, short etc. Is it considered a bad practice? If so, why?

Thank you.

nohup
  • 2,945
  • 3
  • 23
  • 48
  • 1
    I assume you mean the wrapper classes for primitives when you speak about `Helper classes`. Using them is not per-se bad, but you should be aware that you create objects (even if yo do not call an explicit constructor) via [`autoboxing`](https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html). Since this is typically a big performance hit, one tries to avoid it. – Turing85 Oct 29 '15 at 13:27
  • 5
    c.toUpperCase(c) is a bad practice because it's a call to a static method and has to be done on Class not on an instance! – rlm Oct 29 '15 at 13:28
  • @Turing85 The video tutorial I followed was saying as `Helper class`, and not `wrapper class` and hence I was unaware and am not sure which is the correct terminology. The performance part makes sense. Thank you for the kind explanation. – nohup Oct 29 '15 at 13:31
  • @rlm Thank you for the kind explanation. I was looking for the reason to understand why is a bad practice. I guess, as Turing85 said, it is the performance hit. – nohup Oct 29 '15 at 13:35
  • 1
    We don't talk about the same thing : Turing talk about using 'char' instead of 'Character', I talk about using an instance to call a static method... See this thread to demonstrate what I said : http://stackoverflow.com/questions/7884004/is-calling-static-methods-via-an-object-bad-form-why – rlm Oct 29 '15 at 13:46

2 Answers2

3

Character wraps the primitive two-byte char in an object. That is superfluous. You could write

char myBigChar = Character.toUpperCase('b');

So it is a matter of efficiency.

By the way String already is an object, and does not have this verbosity.

Joop Eggen
  • 102,262
  • 7
  • 78
  • 129
3

If I understand what you're asking, you want to know the utility ot wrapper Object classes versus their primitive counterparts.

There are many advantages to wrapper classes:

  • They can be used as generic types
  • They are nullable
  • They provide utility methods on their wrapped primitive value and other static methods
  • etc.

On the other side of your question, there's a concept called boxing / unboxing.

When you: Character c = 'a';, you are autoboxing primitive value 'a' into its wrapper Character class.

If you follow that assignment with something like: char a = c;, you are automatically unboxing the wrapper's value into its primitive counterpart.

Mena
  • 46,817
  • 11
  • 84
  • 103