public class IntegerTest
{
int x;
int y;
public IntegerTest(int x,int y)
{
this.x=x;
this.y=y;
}
public int sum()
{
return this.x+this.y;
}
public int minus()
{
return this.x-this.y;
}
public int multiply()
{
return this.x*this.y;
}
public int chu()
{
return this.x/this.y;
}
public int mod()
{
return this.x%this.y;
}
public int max()
{
if(this.x>=this.y)
{
return this.x;
}
return this.y;
}
public int min()
{
if(this.x<=this.y)
{
return this.x;
}
return this.y;
}
public static void main(String args[])
{
IntegerTest i=new IntegerTest(2,3);
System.out.println("x+y="+i.sum()+"\nx-y="+i.minus()+"\nx*y="+i.multiply()+"\nx/y="+i.chu()+"\nx%y="+i.mod()+"\n最大数为:"+i.max()+"\n最小数为:"+i.min());
}
}
关于内存计算的代码:
public class MemoryDemo{
public static long getTime(){
long time = System.currentTimeMillis();
return time;
}
public static long memoryUsed(){
Runtime r = Runtime.getRuntime();
long memory;
memory= r.freeMemory();
return memory;
}
public static void main(String[] args) {
Runtime r = Runtime.getRuntime();
long memory1;
long memory2;
Integer[] someInts = new Integer[10000];
System.out.println("Totle Memory is :" + r.totalMemory());
memory1= r.freeMemory();
System.out.println("Initial Free Memory is :" + memory1);
r.gc();
memory1= r.freeMemory();
System.out.println("Free Memory After Garbage Collection is :" + memory1);
for(int i =0 ;i<10000 ; i++){
someInts[i]=new Integer(i);
}
memory2= r.freeMemory();
System.out.println("Free Memory After Allocation is :" +memory2);
System.out.println("Memory Used By Allocation is :" +(memory1-memory2));
for(int i =0 ;i<10000 ; i++){
someInts[i]=null;
}
r.gc();
memory2= r.freeMemory();
System.out.println("Free Memory After Allocation discarded Integer" +memory2);
}
}
运行结果:
Totle Memory is :2031616
Initial Free Memory is :1854752
Free Memory After Garbage Collection is :1896568
Free Memory After Allocation is :1736296
Memory Used By Allocation is :160272
Free Memory After Allocation discarded Integer1896568
运行结果因机器不同而不同。