object

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

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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.) 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. 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 . . . 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" 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 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 . 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 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 ....
View Full Document

This note was uploaded on 02/10/2012 for the course CSE 1002 taught by Professor Marico during the Spring '08 term at FIT.

Page1 / 56

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

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online