views:

368

answers:

5

I saw this sentence in some matrials:

"In Java, simple data types such as int and char operate just as in C."

I am wondering that actually they are different in Java & C++?

In C++, simple variables like the primitives in Java are assigned a memory address as well, so these primitive types in C++ can have a pointer as well. However primitives in Java are not assigned a memory address like Objects are.

Am I correct?

Thanks!

+7  A: 

Almost.

In java primitives are assigned memory as well, but this happens internally and you cannot get the reference.

Thre reason was to provide security on memory management.

OscarRyz
A: 

Primitives are stored onto the Stack and are passed by value.

int x = 5;
int y = x;

y++;

// y = 6
// x = 5

Objects are stored onto the heap and are passed by reference.

Object a = new Object();
Object b = a;

b.someAction();

// A and B point to the same object and both have had the 'someAction()' performed

I've no idea if this is what your asking, but I'm bored and wish to post something

Richie_W
Don't you have this backwards? Primitives are allocated on the stack I thought.
bradtgmurray
Lol, oops! Yeah. Editted.
Richie_W
Objects are not passed by reference. Object references are passed by value. See http://stackoverflow.com/questions/333151. Also, primitives are only stored on the stack if they're local variables.
Jon Skeet
What you have is an "object reference" not the object it self. And from the reference you get passed the value ( of the reference ) :-/ We had a heated discussion about this lately. http://stackoverflow.com/questions/333151. "By ref" may have a different meaning depending in your background
OscarRyz
Oscar: Darned comment races ;) (I would argue that "by reference" only actually has one genuine meaning, but it's sometimes *misused* by those without a CS background.)
Jon Skeet
he he he @Jon: You "would" argue, but you're not. And "without CS" still means "different ... background" :)
OscarRyz
But there isn't an alternative, well-defined and accepted meaning of "by reference". There's just an abuse of it / misunderstanding. If a bunch of people thought that a Java int was 16 bits not 32, would that just be an alternative point of view, too? No - it's just plain *wrong*.
Jon Skeet
Children Behave!
martinatime
@martinatime: Good call. Oscar, I apologise for the hostility. This is one of my "buttons" - I'm a terminology pedant. I view getting terminology right as *incredibly* important to effective communication, which is why I went off on one. It was still impolite, however. I apologise.
Jon Skeet
@matinatime: But, but.. he started! ( just kidding ) @Jon Skeet: Not a problem. I use to be like that too until my wife told m.... Well that's another story. Apologizes accepted. I'm sorry too.
OscarRyz
A: 

There are a few differences between Java and C++ when it comes to simple types. The memory allocation is different as Java hides the concept of explicit pointers and references. You just create the simple type and assign a value. Another difference is that Java uses standardized memory footprints for each simple type check Java Simple Types. C++ on the other hand is dependent on the platform that it is compiled on. Another point (and I don't know if it is a difference or not) is that assignment into simple types is Thread safe. I've not done any multi-threaded programming in C++ so I'm not certain if this is a consideration.

martinatime
Assignment to long or double may not be atomic unless volatile (and even then the implementation is probably broke). The change is not necessarily automatically visible to other threads (unless volatile agains). Operations such a ++ are not atomic - use AtomicInteger and friends.
Tom Hawtin - tackline
A: 

For most basic stuff, they work the same, but there is a ton of detail differences.

  • There are no unsigned numerical types in Java (except char, which technically but not semantically numerical)
  • Java's "primitive types" can only live on the stack (as local variables) or inside objects on the heap, but not directly on the heap.
  • The char type is very different - in C, it's defined to contain one byte (the byte may or may not have 8 bits), whereas in Java, it's defined to be 16 bit.
Michael Borgwardt