Tuesday, March 27, 2007

TYPES, VALUES AND VARIABLES(4)

TYPES, VALUES AND VARIABLES(4)

REFERENCE TYPES AND VALUES:

There are 3 kind of reference types:

  1. Class types
  2. Interface types
  3. Array types

Reference types may also be parameterized with type arguments.

A class or interface type consists of:

1. A type declaration specifier that consists of either a type name, or a class type followed by a period(.) and then an identifier or an interface type followed by a period (.) and then an identifier. Note: if denoted as T.id where T is class type or interface type and id is identifier, this signifies that id is an accessible member of type T.

2. Type arguments, if a parameterized type or no arguments if not.

OBJECTS:

An object is a class instance or an array.

Imagine: let’s imagine that the senate approves a bill that makes the following stipulation on approved houses on Aso Rock road in Abuja:

  1. the house should have a blinking red light in front of it.
  2. A bust of OBJ should be to the right of the red light.
  3. A signlet: “Long Live State of Commotion” at the left of it.

This bill states an “approvedAsoRock” class of house and every house fulfilling the contract is a class instance of this class or object of the class.

The reference values are pointers to objects, not objects themselves, and the special null reference value refers to no object.

See the examples below for creating classes and arrays.

package api_package;

public class CharacterObjects {

//Implicitly creating an array with array initializers

static char[] slimman = {'Y','A','R','\'','D','U','A'};

static char[] fatman = {'A','T','I','K','U'};

public static void main(String[] args) {

//Explicitly creating a class instance

String slimstring = new String(slimman);

String fatstring = new String(fatman);

//we'll play with U like before.let's pick the letter

char container = slimstring.charAt(6);

//Implicitly creating a class instance

System.out.println("We picked "+container);

//i was wrong. i forgot this is index 0.

//let's ask the all knowing machine for index

System.out.println(slimstring.indexOf("U"));//5

//so let's do it again

container = slimstring.charAt(5);

System.out.println("We picked "+container);

//let's join the two strings. Implicity creating a

//String instance

System.out.print("Joining the two strings gives ");

String newstring = slimstring + " " + fatstring;

System.out.println(newstring);

//we'll create an array of char from the joined string

char[] newchar = newstring.toCharArray();

System.out.print("The new character array is: ");

System.out.println(newchar);

}

}

No comments: