Questions tagged [wrapper]

A wrapper is an OOP technique where an object encapsulates (wraps) another object, resource (dynamically allocated memory, OS file/widow handle, socket, thread mutex, etc) or a set of subroutines, hiding/protecting it and providing another (possibly easier to use) interface. When using this tag on implementation heavy questions - tag the code language the implementation is written in.

A Wrapper is an Object which contains another data type, Object, resources, or subroutines for the purpose of encapsulation. The corresponding class of that Wrapper is the wrapper class.

Often when we refer to a wrapper class we often refer to a primitive wrapper class, where instances of that class encapsulate a primitive type.

Many object-oriented languages, such as Java, differentiate between primitive types (such as char) and Objects (such as String), for example that primitive values are passed on by value and are the simplest data types, whereas Objects are passed on by reference and that it includes their own fields and methods. The names of primitive data types are also usually keywords, as opposed to Objects.

In some cases, you may need an instance of a wrapper class for these primitives to perform boxing, treat these primitives as Objects due to OOP reasons, converting a primitive to another Object type, or when using an otherwise primitive value when polymorphism is required. The name of the corresponding primitive wrapper class for a given primitive data type are written in full and follow naming conventions for classes (such as beginning with an uppercase in Java).

Java wrapper classes:

Java offers these wrapper classes which you can use without importing anything. Note that their names begin with an uppercase, and are written in full.

  • Boolean for booleans
  • Character for char
  • Integer for int
  • Long for 64-bit int
  • Double for boudle
  • Byte for byte
  • Short for short
  • Float for float

Tags related to wrapper operations:

Link to Wikipedia page

3619 questions
48
votes
10 answers

How to safely wrap `console.log`?

Suppose I want to include some calls to console.log for some legitimate production reason, say for something like a unit test harness. Obviously I would not want this to throw a premature exception if the browser doesn't have a console, or if no…
Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
48
votes
2 answers

Creating simple c++.net wrapper. Step-by-step

I've a c++ project. I admit that I'm a complete ZERO in c++. But still I need to write a c++.net wrapper so I could work with an unmanaged c++ library using it. So what I have: 1) unmanaged project's header files. 2) unmanaged project's libraries…
Evgeny007
  • 642
  • 2
  • 6
  • 8
47
votes
5 answers

How to convert Integer[] to int[] array in Java?

Is there a fancy way to cast an Integer array to an int array? (I don't want to iterate over each element; I'm looking for an elegant and quick way to write it) The other way around I'm using…
Michael Brenndoerfer
  • 3,483
  • 2
  • 39
  • 50
45
votes
3 answers

What's the relative order with which Windows search for executable files in PATH?

If I have a.com, a.cmd, a.bat, and a.exe files in my %PATH%, which one would Windows pick if I invoke just the command a? Is this officially spec-ed somewhere by Microsoft? I just wanted to wrap my gvim.exe executable with -n, but my gvim.bat…
Jeenu
  • 2,109
  • 2
  • 20
  • 27
40
votes
3 answers

Exposing `defaultdict` as a regular `dict`

I am using defaultdict(set) to populate an internal mapping in a very large data structure. After it's populated, the whole structure (including the mapping) is exposed to the client code. At that point, I don't want anyone modifying the mapping.…
max
  • 49,282
  • 56
  • 208
  • 355
39
votes
7 answers

How do I wrap a function in Javascript?

I'm writing a global error handling "module" for one of my applications. One of the features I want to have is to be able to easily wrap a function with a try{} catch{} block, so that all calls to that function will automatically have the error…
Daniel Magliola
  • 30,898
  • 61
  • 164
  • 243
38
votes
2 answers

What is the difference between a wrapper, a binding, and a port?

In a software portability context, what is the difference between these three concepts? For example, I want to use the ncurses library, the original ncurses library is written in C, but my application is being written in C++, and then I found…
user478249
  • 698
  • 1
  • 10
  • 15
38
votes
8 answers

Simple way to get wrapper class type in Java

I have a piece of code where I need to pass the class of a field in a method. Because of the mechanics of my code I can only handle reference objects and not primitives. I want an easy way of determining if a Field's type is primitive and swap it…
Savvas Dalkitsis
  • 11,476
  • 16
  • 65
  • 104
36
votes
2 answers

Parent div is smaller than child div?

here's my question, i want to fill the content div with white. BUT when i do it , its not filling the whole div. The child div are more bigger and they go over the parent div (content). I want that the content div cover all the child div. Here's my…
user1440480
  • 369
  • 1
  • 5
  • 7
32
votes
3 answers

How to wrap a function using varargin and varargout?

mini-example: function varargout = wrapper(varargin) varargout = someFunction(varargin); That's how I'd do it first. But for example if someFunction = ndgrid this yields a not defined for cell arrays error, so the next try was using…
Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221
32
votes
2 answers

Java: Is there a difference between L and l (lowercase L) when specifying a long?

When I specify a number to be a long with a constant value 400, is there any difference between using 400L and 400l? Does it have some relationship to the wrapper type? Is L used to get a wrapper Long and l for the primitive data type long?
Lippstadtfreak
  • 321
  • 1
  • 3
  • 3
30
votes
3 answers

wrapper printf function that filters according to user preferences

My program writes to a log and to stdout. Every message, however, has a certain priority and the user specifies in Preferences which priorities go to which stream (log or stdout). unsigned short PRIO_HIGH = 0x0001; unsigned short PRIO_NORMAL =…
wsd
  • 1,246
  • 4
  • 16
  • 22
30
votes
3 answers

How to wrap every method of a class?

I'd like to wrap every method of a particular class in python, and I'd like to do so by editing the code of the class minimally. How should I go about this?
rjkaplan
  • 3,138
  • 5
  • 27
  • 33
28
votes
2 answers

Does C++11 have wrappers for dynamically-allocated arrays like Boost's scoped_array?

I often need to deal with dynamically-allocated arrays in C++, and hence rely on Boost for scoped_array, shared_array, and the like. After reading through Stroustrup's C++11 FAQ and the C++11 Reference Wiki, I could not find a suitable replacement…
void-pointer
  • 14,247
  • 11
  • 43
  • 61
28
votes
6 answers

What is the radix parameter in Java, and how does it work?

I understand that radix for the function Integer.parseInt() is the base to convert the string into. Shouldn't 11 base 10 converted with a radix/base 16 be a B instead of 17? The following code prints 17 according to the textbook: public class Test…
Minh Tran
  • 783
  • 1
  • 9
  • 20