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
242
votes
17 answers

What is a wrapper class?

What is a wrapper class? How are such classes useful?
Bhaskar
  • 4,189
  • 4
  • 26
  • 20
212
votes
12 answers

How to use C++ in Go

In the new Go language, how do I call C++ code? In other words, how can I wrap my C++ classes and use them in Go?
Frank
  • 64,140
  • 93
  • 237
  • 324
196
votes
7 answers

Java: Integer equals vs. ==

As of Java 1.5, you can pretty much interchange Integer with int in many situations. However, I found a potential defect in my code that surprised me a bit. The following code: Integer cdiCt = ...; Integer cdsCt = ...; ... if (cdiCt != null && cdsCt…
Jeremy Goodell
  • 18,225
  • 5
  • 35
  • 52
148
votes
7 answers

Cannot add task 'wrapper' as a task with that name already exists

When installing 'react-native init AwesomeProject' I get this error when I run react-native run-android: Could not determine java version from '11.0.1'. A quick google suggests I need to update the distributionUrl in the Gradle-wrapper. Having…
Ben Kemp
  • 1,501
  • 2
  • 7
  • 9
119
votes
11 answers

When to use wrapper class and primitive type

When I should go for wrapper class over primitive types? Or On what circumstance I should choose between wrapper / Primitive types?
Gopi
  • 5,656
  • 22
  • 80
  • 146
99
votes
5 answers

How to decorate all functions of a class without typing it over and over for each method?

Lets say my class has many methods, and I want to apply my decorator on each one of them, later when I add new methods, I want the same decorator to be applied, but I don't want to write @mydecorator above the method declaration all the time. If I…
rapadura
  • 5,242
  • 7
  • 39
  • 57
96
votes
6 answers

Developing C wrapper API for Object-Oriented C++ code

I'm looking to develop a set of C APIs that will wrap around our existing C++ APIs to access our core logic (written in object-oriented C++). This will essentially be a glue API that allows our C++ logic to be usable by other languages. What are…
theactiveactor
  • 7,314
  • 15
  • 52
  • 60
89
votes
4 answers

Vue - how to pass down slots inside wrapper component?

So I've created a simple wrapper component with template like: using $attrs and $listeners to pass down props and events. Works fine, but how can the wrapper proxy the…
user3599803
  • 6,435
  • 17
  • 69
  • 130
81
votes
12 answers

Can you alter a Javascript function after declaring it?

Let's say I have var a = function() { return 1; }. Is it possible to alter a so that a() returns 2? Perhaps by editing a property of the a object, since every function is an object? Update: Wow, thanks for all the responses. However, I'm afraid I…
pr1001
  • 21,727
  • 17
  • 79
  • 125
78
votes
14 answers

Making decorators with optional arguments

from functools import wraps def foo_register(method_name=None): """Does stuff.""" def decorator(method): if method_name is None: method.gw_method = method.__name__ else: method.gw_method =…
orokusaki
  • 55,146
  • 59
  • 179
  • 257
75
votes
10 answers

General decorator to wrap try except in python?

I'd interacting with a lot of deeply nested json I didn't write, and would like to make my python script more 'forgiving' to invalid input. I find myself writing involved try-except blocks, and would rather just wrap the dubious function up. I…
Mittenchops
  • 18,633
  • 33
  • 128
  • 246
74
votes
3 answers

How to test an internal class in a class library?

I would like to write a class library which creates for me a complex object but should only be exposed as little as possible. I want it to be included into other projects and there I only have one call to this library which e.g. returns me an object…
Thomas Mondel
  • 1,944
  • 3
  • 20
  • 25
67
votes
8 answers

Using == operator in Java to compare wrapper objects

I'm reading SCJP Java 6 by Kathy Sierra and Bert Bates and this book is confusing me so much. On page 245 they state that the following code below. Integer i1 = 1000; Integer i2 = 1000; if(i1 != i2) System.out.println("different objects"); //Prints…
dido
  • 3,347
  • 11
  • 34
  • 42
59
votes
5 answers

How many objects are created by using the Integer wrapper class?

Integer i = 3; i = i + 1; Integer j = i; j = i + j; How many objects are created as a result of the statements in the sample code above and why? Is there any IDE in which we can see how many objects are created (maybe in a debug mode)?
Syamesh K
  • 826
  • 8
  • 18
51
votes
5 answers

What is the meaning of a C++ Wrapper Class?

I have a little trouble in understanding a wrapper class. It would be great if some one could help providing apt examples. What is a C++ Wrapper Class and what are the circumstances of writing it ? What is it's use any way ? Thanks.
Mahesh
  • 34,573
  • 20
  • 89
  • 115
1
2 3
99 100