16

In Java we have primitive data types and bunch of wrapper classes for them. My question is that when to use what? I know that when we need to create Collections, we will need to use the wrapper classes, but other than that are there other specific cases where one should use wrapper classes?

Also, is it that one should always use the primitive data types unless absolutely necessary?

For example if I am creating a Class having an integer and a boolean properties:

Class MyClass {
    ...
    private Integer x;
    private Boolean y;
    ...
}

OR

Class MyClass {
    ...
    private int x;
    private boolean y;
    ...
}

Which of them should be used more often? And in what scenarios the other one should be used?

Amar
  • 11,480
  • 5
  • 48
  • 72

5 Answers5

24

Use the primitive type unless you have no other choice. The fact that it's not nullable will prevent many bugs. And they're also faster.

Besides collections, wrapper types are typically used to represent a nullable value (for example, coming from a database nullable column).

JB Nizet
  • 633,450
  • 80
  • 1,108
  • 1,174
5

this article talks about wrapper classes, they said:

The wrapper classes in the Java API serve two primary purposes:

1- To provide a mechanism to “wrap” primitive values in an object so that the primitives can be included in activities reserved for objects, like as being added to Collections, or returned from a method with an object return value.

2- To provide an assortment of utility functions for primitives. Most of these functions are related to various conversions: converting primitives to and from String objects, and converting primitives and String objects to and from different bases (or radix), such as binary, octal, and hexadecimal.

Community
  • 1
  • 1
Sawan
  • 6,233
  • 9
  • 49
  • 98
  • 1
    Hey, sorry for the confusion, have edited the question :) For C# I should create another question. – Amar Dec 16 '12 at 14:54
  • I see, but you can read the article of the link "Wrapper Classes" "http://www.go4expert.com/forums/showthread.php?t=22183" to get more information about this in Java – Sawan Dec 16 '12 at 14:58
  • You may want to answer this: http://stackoverflow.com/questions/13902506/best-practices-what-to-use-net-class-or-short-name – Amar Dec 16 '12 at 15:00
  • hey edit your answer to remove about C# :) – Amar Dec 16 '12 at 15:22
  • how does this answer the question ? – NikkyD Jun 18 '14 at 14:17
3

The wrapper classes are immutable, and therefore have no setter methods.Every time you want to change its value a new object would have to be created. This would create unnecessary objects. The int primitive just represents the bits of the number. You are not creating any new object. Therefore I would suggest using the primitive when not using collections.

rabz100
  • 617
  • 1
  • 5
  • 12
1

The wrapper classes can be quite helpful when your system produces a level of uncertainty that must be accounted for. For example using Boolean in a packet capture program such as wireshark, would allow you to pass along a null value if you were unable to determine if a packet was modified from the sender. Specifically in the case when you are able to determine other packet manipulations.

Woot4Moo
  • 22,887
  • 13
  • 86
  • 143
0

When using primitive types in c# I prefer to use the c# alias in declarations and .NET name when accessing static members. It's just a personal preference. The both names are absolutely equivalent.

int x = Int32.Parse("123");
Olivier Jacot-Descombes
  • 86,431
  • 10
  • 121
  • 160
  • He changes his question, he is asking about java. – Sawan Dec 16 '12 at 14:57
  • I'm so sorry for the confusion I created by mixing both. here is the question for C#: http://stackoverflow.com/questions/13902506/best-practices-what-to-use-net-class-or-short-name – Amar Dec 16 '12 at 15:02