Click here to Skip to main content
13,249,087 members (42,701 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 22 Mar 2010

Re boxed primitives in Java and .NET

, 23 Mar 2010
Rate this:
Please Sign up or sign in to vote.
Why "i = i++" expression returns different results in C++ and Java/.NET?

One of my colleagues posted the following question once “why <i = i++> expression returns different results in C++ and Java/.NET?”. For instance, try this:


int i = 1;
i = i++; 
printf("%d\n", i);
result is 2


int i = 1;
i = i++; 
result is 1

Many C++ developers will argue that <i = i++> is left undefined in C++ (i.e. it is up to compiler implementers to return whatever they think it is most appropriate). However, at a more practical level, <i = i++> against a primitive int type, in C/C++, is a simple (optimized ASM code, avoiding all the formalities with moving to/from registers):

mov i, i
inc i

at the same "address location" (in a simplistic way). So the result is 2.

Now, let's look at what C++ suggests about operator++(int). It suggests making a copy of the current instance, increasing current instance and returning the copy. Following this rule, the result is:

class MyInt {
	int i;
	MyInt(int iVal) { i = iVal; };
	int val() const { return i; };
	MyInt(const MyInt& t) { i = t.val(); };

	MyInt& operator=(const MyInt& t) {
		i = t.val();
		return *this;

	MyInt operator++(int) {
		MyInt t = *this;
		return t;

MyInt func() {
	MyInt i = MyInt(1);
	i = i++;
	return i;

int _tmain(int argc, _TCHAR* argv[])
	int i = 1;
	i = i++;
	_tprintf(_T("%d\n"), i);

	MyInt t = func();
	_tprintf(_T("%d\n"), t.val());
	return 0;
Result is

But it is exactly what Java/.NET returns. From this, it is logical to conclude that Java/.NET primitives are boxed (which seems to be logical, otherwise it is hard to imagine how to support platform independence in Java/.NET, for example replacing the above class with a structure like "struct MyInt { int i : 32; };" in order to support 32 bits integers). Also this means that C/C++ works faster with primitives :)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) Snappli Ltd.
United Kingdom United Kingdom
My name is Ruslan Ciurca. Currently I am a Software Developer at

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Reaboi Artur5-May-10 20:50
memberReaboi Artur5-May-10 20:50 
GeneralRe: My vote of 1 Pin
rtybase6-May-10 0:51
memberrtybase6-May-10 0:51 
GeneralRe: My vote of 1 Pin
Reaboi Artur7-May-10 5:53
memberReaboi Artur7-May-10 5:53 
GeneralRe: My vote of 1 Pin
rtybase8-May-10 1:22
memberrtybase8-May-10 1:22 
GeneralRe: My vote of 1 Pin
Reaboi Artur9-May-10 20:13
memberReaboi Artur9-May-10 20:13 
GeneralRe: My vote of 1 [modified] Pin
rtybase9-May-10 23:18
memberrtybase9-May-10 23:18 
Generali++ vs. ++i Pin
Dennis Dykstra30-Mar-10 11:11
memberDennis Dykstra30-Mar-10 11:11 
GeneralRe: i++ vs. ++i Pin
rtybase31-Mar-10 7:02
memberrtybase31-Mar-10 7:02 
GeneralRe: i++ vs. ++i Pin
Dennis Dykstra31-Mar-10 19:40
memberDennis Dykstra31-Mar-10 19:40 
GeneralRe: i++ vs. ++i Pin
rtybase1-Apr-10 7:24
memberrtybase1-Apr-10 7:24 
GeneralReal Story Pin
Jonathan C Dickinson29-Mar-10 22:26
memberJonathan C Dickinson29-Mar-10 22:26 
GeneralRe: Real Story Pin
rtybase31-Mar-10 7:16
memberrtybase31-Mar-10 7:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 23 Mar 2010
Article Copyright 2010 by rtybase
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid