Tuesday, March 27, 2007

SNIPPET: java.lang.Integer

SNIPPET: java.lang.Integer

The integer class is a final class, i.e it can’t be subclassed by any class. It’s superclass is the Number class.

The Integer class wraps the value of the primitive type int in an object and contains a single field with type int. This class has several methods for casting int to String and vice versa, as well as other useful methods for playing with int.

In the example that follows, since ages are less than 100 we’ll be working with int values that correspond to byte values. This examples assumes you already understand a little of two’s complement binary representation of numerical values.

package api_package;

public class Lagosian {

//the fields in the class

String name, sex;

int age;

//Class constructor, without parameters

Lagosian(){

this.name = "jejecy";

this.sex = "dummy";

this.age = 120;

}

//Constructor with parameters

Lagosian(String arg, String orientation, int ages){

this.name = arg;

this.sex = orientation;

this.age = ages;

}

public static void main(String[] args) {

//the first object we met in Lagos was called tunde

Lagosian firstobject = new Lagosian("tunde", "male",40);

System.out.println("Tunde: My name is "+

firstobject.name+".");

//The second was a woman, Ngozi

Lagosian secondobject = new Lagosian("ngozi","female",25);

System.out.println("Ngozi: My name is "+secondobject.name);

//we met a third person who looked like lagbaja

Lagosian thirdperson = new Lagosian();

System.out.println("What is the name of the third

person?");

System.out.println("I answered: his name is

"+thirdperson.name+" and i think he looks like a "+thirdperson.sex);

//we'll be working with the interger values from here

//let's take Ngozi's age. What's the two's complement

//binary representation? 11001

System.out.print("Ngozi's age in binary is: ");

System.out.println(Integer.toBinaryString(secondobject.age)

);

//the bits that are on are 3. that's the population

//count.

System.out.print("The population count is: ");

System.out.print(Integer.bitCount(secondobject.age));

System.out.print(". Which as a double is written as: ");

System.out.println(((Integer)secondobject.age).doubleValue(

));

//A little on bits. What is the highest bit on? 2 raised 4.

System.out.print("The highest one bit is: ");

System.out.print(Integer.highestOneBit(secondobject.age));

System.out.print(". While the lowest is: "); //2 raised 0

System.out.println(Integer.lowestOneBit(secondobject.age));

//concept of leading zeros. remember int is 32 bits.

System.out.print("The leading zeros which you didn't see

are: ");

System.out.println(Integer.numberOfLeadingZeros(secondobjec

t.age));

//reversing the bits and then Ngozi will be a Pterosaurus

System.out.print("Reversing the bits Ngozi is now ");

System.out.println(Integer.reverse(secondobject.age)+"

years old.");

//if you know your bits and bytes, shift Ngozi's bits

//by one to the left and she becomes twice older. Does

//she like it?

System.out.print("Twice older today, Ngozi is now ");

System.out.print(Integer.rotateLeft(secondobject.age, 1)+"

years older.");

}

}

The essence of the exercises are that by doing you’ll get used to the java environment and you’ll be confident of exploring new ways of using the language.

No comments: