Monday, June 26, 2017

MyInteger Wrapper Class

Tags

Following Code define a Class MyInteger Which acts as a Wrapper class for integer :


Code:

class MyInteger {
int i;
MyInteger(int i){
this.i=i;
}
int intValue(){
return i;
}

public String toString() {
return String.valueOf(i);
}

}
/*class MyInteger1 extends Integer {
//error because Integer is a final class
}*/
public class MyIntegerDemo {
public static void main(String[] nt) {
MyInteger mi=new MyInteger(10);
int j = mi.intValue();
System.out.println(mi);
System.out.println(j);
}
}

Output: