Home      Discussion      Topics      Dictionary      Almanac
Signup       Login
Object type

Object type

Overview
In computer science
Computer science
Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems. It is frequently described as the systematic study of algorithmic processes that create, describe and transform...

, an object type (a.k.a. wrapping object) is a datatype which is used in object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm that uses "objects" – data structures consisting of datafields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as information hiding, data...

 to wrap a non-object type to make it look like a dynamic
Reference type
In programming language theory, a reference type is a data type that can only be accessed by references. Unlike objects of value types, objects of reference types cannot be directly embedded into composite objects and are always dynamically allocated...

 object
Object (computer science)
In computer science, an object, in the domain of object-oriented programming, usually means a compilation of attributes and behaviors encapsulating an entity....

.

Some object-oriented programming language
Object-oriented programming language
This is a list of object-oriented programming languages.-Languages with object-oriented features:*ABAP*Ada 95*AmigaE**PortablE*BETA*Blue*Boo*C++*C#*COBOL*Cobra*ColdFusion*Common Lisp*COOL*CorbaScript*Clarion*CLU*Curl*D*Delphi...

s make a distinction between reference
Reference type
In programming language theory, a reference type is a data type that can only be accessed by references. Unlike objects of value types, objects of reference types cannot be directly embedded into composite objects and are always dynamically allocated...

 and value type
Value type
In object-oriented programming, a value type is a data type that can exist outside dynamic memory allocation. Unlike reference types, value types can be directly embedded into composite objects.Primitive types are always value types....

s, often referred to as objects and non-objects on platforms where complex value types don't exist, for reasons such as runtime efficiency and syntax or semantic issues. For example, Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 has primitive wrapper class
Primitive wrapper class
A primitive wrapper class in the Java programming language is one of eight classes provided in the package to provide object methods for the eight primitive types. All of the primitive wrapper classes in Java are immutable...

es corresponding to each primitive type
Primitive type
In computer science, primitive data type can refer to either of the following concepts:* a basic type is a data type provided by a programming language as a basic building block...

: Integer and int, Character and char, Float and float, etc.
Discussion
Ask a question about 'Object type'
Start a new discussion about 'Object type'
Answer questions from other users
Full Discussion Forum
 
Encyclopedia
In computer science
Computer science
Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems. It is frequently described as the systematic study of algorithmic processes that create, describe and transform...

, an object type (a.k.a. wrapping object) is a datatype which is used in object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm that uses "objects" – data structures consisting of datafields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as information hiding, data...

 to wrap a non-object type to make it look like a dynamic
Reference type
In programming language theory, a reference type is a data type that can only be accessed by references. Unlike objects of value types, objects of reference types cannot be directly embedded into composite objects and are always dynamically allocated...

 object
Object (computer science)
In computer science, an object, in the domain of object-oriented programming, usually means a compilation of attributes and behaviors encapsulating an entity....

.

Some object-oriented programming language
Object-oriented programming language
This is a list of object-oriented programming languages.-Languages with object-oriented features:*ABAP*Ada 95*AmigaE**PortablE*BETA*Blue*Boo*C++*C#*COBOL*Cobra*ColdFusion*Common Lisp*COOL*CorbaScript*Clarion*CLU*Curl*D*Delphi...

s make a distinction between reference
Reference type
In programming language theory, a reference type is a data type that can only be accessed by references. Unlike objects of value types, objects of reference types cannot be directly embedded into composite objects and are always dynamically allocated...

 and value type
Value type
In object-oriented programming, a value type is a data type that can exist outside dynamic memory allocation. Unlike reference types, value types can be directly embedded into composite objects.Primitive types are always value types....

s, often referred to as objects and non-objects on platforms where complex value types don't exist, for reasons such as runtime efficiency and syntax or semantic issues. For example, Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 has primitive wrapper class
Primitive wrapper class
A primitive wrapper class in the Java programming language is one of eight classes provided in the package to provide object methods for the eight primitive types. All of the primitive wrapper classes in Java are immutable...

es corresponding to each primitive type
Primitive type
In computer science, primitive data type can refer to either of the following concepts:* a basic type is a data type provided by a programming language as a basic building block...

: Integer and int, Character and char, Float and float, etc. Languages like C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level and low-level language features...

 have little or no notion of reference type; thus, the use of object type is of little interest.

Boxing


Boxing is to place a value within an object so that the value can be used as a reference object. For example, lists may have certain methods
Method (computer science)
In object-oriented programming, a method is a subroutine that is exclusively associated either with a class or with an object...

 which arrays
Array data type
In computer science, an array type is a data type that is meant to describe a collection of elements , each selected by one or more indices that can be computed at run time by the program. Such a collection is usually called an array variable, array value, or simply array...

 might not, but the list might also require that all of its members be dynamic objects. In this case, the added functionality of the list might be unavailable to a simple array of numbers.

For a more concrete example, in Java, a can change its size, but an array must have a fixed size. One might desire to have a LinkedList of ints, but the LinkedList class only lists references to dynamic objects — it cannot list primitive types, which are value types.

To get around this, ints can be boxed into Integers, which are dynamic objects, and then added to a LinkedList of Integers. (Using generic
Generic programming
Generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters...

 parametrized types introduced in J2SE
Java Platform, Standard Edition
Java Platform, Standard Edition or Java SE is a widely used platform for programming in the Java language. It is the Java Platform used to deploy portable applications for general use...

 5.0, this type is represented as LinkedList.)

On the other hand, C# has no primitive wrapper classes, but allows boxing of any value type, returning a generic Object reference.

The boxed object is always a copy of the value object, and is usually immutable
Immutable object
In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created...

. Unboxing the object also returns a copy of the stored value. Note that repeated boxing and unboxing of objects can have a severe performance impact, since it dynamically allocates
Dynamic memory allocation
In computer science, dynamic memory allocation is the allocation of memory storage for use in a computer program during the runtime of that program...

 new objects and then makes them eligible for Garbage collection
Garbage collection (computer science)
In computer science, garbage collection is a form of automatic memory management. It is a special case of resource management, in which the limited resource being managed is memory. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no...

.

Autoboxing


Autoboxing is the term for treating a value type as a reference type without any extra source code. The compiler automatically supplies the extra code needed to perform the type conversion
Type conversion
In computer science, type conversion or typecasting refers to changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies...

.

For example J2SE 5.0 allows the programmer to create a LinkedList of ints. This does not contradict what was said above: the LinkedList still only lists references to dynamic objects, and it cannot list primitive types. But now, when Java expects a reference but receives a primitive type, it immediately converts that primitive type to a dynamic object. Note that the declaration List is illegal in Java, but List is not, and autoboxing will allow adding of primitive ints to the collection.

This action is called autoboxing, because it is done automatically and implicitly instead of requiring the programmer to do so manually.

For example, in versions of Java prior to J2SE 5.0, the following code did not compile:



Integer i = new Integer(9);
Integer j = new Integer(13);
int k = 9 + 13; // always OK
Integer l = i + j; // error in versions prior to 5.0!


Compilers prior to 5.0 would not accept the last line. Integers are reference objects, on the surface no different from List, Object, and so forth; mathematical operators such as + were not meaningfully defined for references. As of J2SE 5.0, the Integers i and j are unboxed into ints, the two are added, and then the sum is autoboxed into a new Integer. http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html

Unboxing


Unboxing refers to a boxed value type which has been broken down and the value type retrieved for a process of some kind such as a mathematical operation.

For example, in versions of Java prior to J2SE 5.0, the following code did not compile:


int i = 4;
int j = 5;
Integer k = new Integer(i + j); // always OK
Integer l = i + j; // would have been an error, but okay now - equivalent to previous line


C# does not support automatic unboxing. A boxed object must be explicitly unboxed with a typecasting
Type conversion
In computer science, type conversion or typecasting refers to changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies...

operator:


int i = 42;
object o = i; //box
int j = (int)o; //unbox
Console.Writeline(j); //outputs "42"