object - Primitive Data and Objects The programmer computes...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Primitive Data and Objects The programmer computes on data. Data in Java is divided into primitive data and non-primitive data. Primitive data (usually) has operations supported in the hardware. int is primitive, String is not. double is primitive, arrays are not. (Wrapper classes allow primitive data to be treated like objects. The advantage of using wrapper classes is that all data can be treated uniformly. The disadvantage is some extra overhead.)
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Primitive Data and Reference Data Usual characteristics of primitive data: I small size, all values fit in the machine’s word size I uncomplicated (no substructure) I operations on the data are supported in the hardware The majority of data the programmer wants is not primitive: images, music, graphs, sequences, etc. Java supports this vast, endless variety of data by allowing programmer to define new data types and by implementing some in the libraries. User-defined data types are created using classes. Instances of classes are allocated in a managed storage area called the heap and variables in the program refer indirectly to the instances. We call these instances objects , but referenced data might be a less overused term.
Image of page 2
Storage During execution the entire program is laid out in (virtual) storage, which we can envisioned as a gigantic array of words indexed by a (virtual) address. All the data appears somewhere in storage. . . . 0x402A 0x4029 0x4028 . . .
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Primitive Data and Objects Primitive data values are stored directly (“unboxed”) and objects are stored indirectly (“boxed”). For example, int i = 123; String s = "Hello" ; 00 00 2F 87 0x4029 String s . . . 00 00 00 7B 0x3D2A int i "Hello"
Image of page 4
The space for objects is allocated in what is known as the “heap.” Of course, the heap must be found somewhere in storage. 0x403A 00 00 2F 87 0x4029 String s 0x4028 . . . 00 00 00 05 0x2F87 (length) 48 65 6C 6C 0x2F86 ' H ' ' e ' ' l ' ' l ' 6F 00 00 00 0x2F85 ' o ' 0x2F84
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Other Languages Other languages, like Haskell and C# do not require the programmer to distinguish between boxed and unboxed data. But both languages have boxed and unboxed values, integers, for instance. But in these languages the programmers only have one data type for integers. Unboxed is more efficient for computation and boxed is more uniform. Haskell and C# go back and forth between the implementation automatically. And, Java too, goes back and forth automatically. But there are two distinct types in the language: int and Integer .
Image of page 6
The Integer Wrapper Class 00 00 00 7B 0x3B05 int i 00 00 28 A0 0x3B04 Integer j 0x3B03 . . . 00 00 00 7B 0x28A0 int value 0x289F
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Creating Objects When you declare a variable to have a primitive data type, an address is assigned which can hold a value of the primitive type (int, etc.) When you declare a variable of object type, a box is created which can hold a reference to an instance of that type. No object/instance is created. An object can only be created by new .
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern