Skip to main content

Nonprimitive Data Types in Java

Nonprimitive data types in Java are reference variables (object references), arrays and enums. All nonprimitive data types are references to memory where the objects live.


[objects]


References provide access to objects. The declaration syntax is just the same as with primitives.



Decryptor decryptor;

The above example shows that we are creating a reference to a Decryptor object. Take note that no real object is created yet.


The object is created through the new operator.



decryptor = new Decryptor();

The reference decryptor now points to a Decryptor object in the heap.


[arrays]


Arrays are objects used to hold a collection of primitive or nonprimitive data of the same type. Take note that even if an array holds primitive data, it is always an object.


Steps for creating an array:


1. Declaration of an array variable (reference)



char[] charArray; //or
char charArray[];

2. Instantiation of an array of a certain size



charArray = new char[8];

3. Initialization of each array element.



charArray[0] = 'k';
charArray[1] = 'a';
charArray[2] = 'r';
charArray[3] = 'e';
charArray[4] = 'n';
charArray[5] = 'e';
charArray[6] = 'v';
charArray[7] = 'e';

Once a size is given to an array, it cannot be changed later.


[enums]


The data type enum is used to store a predetermined set of values or constants. Examples are the months in a year, the days in a week, etc.


Steps for creating an enum:


1. Define the enum type with unique named values



enum ShortWeek {MON, TUE, WED, THU, FRI, SAT, SUN};

2. Assign a reference to the enum type



ShortWeek monday = ShortWeek.MON;

You can only create X instances of an enum type, where X is the number of elements that the enum type holds.

Comments

Popular posts from this blog

Architecture Complexity

Here are the items to consider: Coding to an interface Service Oriented Architecture Automated Testing Domain Driven Design Custom Data Access Layer Layered architecture Complexity is relatively equal the number of lines of code. Note that complexity is not bad. It must be justified.

Moving on

So I bought my self a 2.5″ hard drive enclosure for about 750 bucks. I wish I have my own tools for constructing a device as such. Instead of going for a 2GB USB flash drive, I chose to salvage the 30GB hard drive of an incapacitated laptop. Cool isn’t it? Though relatively slower than a typical USB flash drive, the storage capacity outweighs it. [To God] Thanks for being there always.

Bin to hex converter in 16-bit DOS assembly

Just a quick code like putting your keyboard where your brain is. There should be another way of doing this. I hate this code. start: xor al, al xor bl, bl xor cl, cl mov dl, 4 ; use dl as counter input: mov ah, 00 int 16h cmp ah, 1ch je exit cmp al, '0' jb input cmp al, '1' ja input convert: mov cl, dl dec cl ; dl - 1 sub al, 30h ; get original value shl al, cl ; place in the appropriate bit position or bl, al ; save in bl dec dl ; prepare for the next bit jnz input ; must be 4 bits cmp bl, 9 ja letter jbe number letter: add bl, 37h ; because 'A' - 37h is 0Ah jmp here number: add bl, 30h ; because '0' - 30h is 00h here: call display jmp start display: mov ah, 02h ...