How do pointers work with primitive types in Java? -
i reading what nullpointerexception, , how fix it?, , in accepted answer, read did not quite understand:
int x; x = 10;in example variable x int , java initialize 0 you. when assign 10 in second line value 10 written memory location pointed x.
i thought primitive types, variable memory address of actual value; complex types, variable merely memory address of pointer actual value. quoted answer above tells me wrong. says "the memory location pointed x."
so if x pointing memory address stores actual value, how primitive type different complex type? did not know primitive types had pointers. how pointers work primitive types?
a primitive type , complex type different each other in way data stored. you're looking @ differences between primitive type , class type
1. every variable stored location in computer memory.
the above statement applies both primitive types , class types.
the differences:
2. primitive type: value of variable stored in memory location assigned variable.
that means if assigned
int x = 10, value ofxstored in value of10stored, i.e memory location. means when "look" @ x, '10' stored there. maybe think of more "assignment" command x equal 10.3. class type: stores memory address of the object stores value. not directly hold object itself.
integer x = 10 have memory address points object of type int, hold value of 10. known reference. think of directory tells go shelf retrieve value.
also
class types known reference types, or object types, mean object of class (be integer class, or myperson class).
primitive types not reference types because they not hold references (memory addresses).
this distinction reason "wrapper classes" in daily use, , types such integer seen wrapper class int, allow data manipulation such storing integers in data structure such arraylist. because ints primitive data type, not object, while integer is. since primitive types not objects, have put them class in order add them lists, dictionaries etc. way have list of objects (which, point primitive types) not naked primitive datatype itself. see this question further info
additional reading on difference between primitive , non-primitive (aka class/reference/object type) detailed here. have nice diagram illustrating too.
Comments
Post a Comment