note:
栈:存放 局部变量(方法内、方法形参、构造器、构造器形参、代码块内)
堆:存放new出来的结构:对象(非static成员变量)、数组
public class OverLoadTest{
//下面四种方法构成重载
public void getSum(int i1,int i2){
System.ot.println(i1 + i2);
}
public void getSum(double d1,double d2){
System.ot.println(d1 + d2);
}
public void getSum(String s,double d){
System.ot.println(s + d);
}
public void getSum(double d,String s){
System.ot.println(s + d);
}
}
public class Test{
public static void main(){
mOL m1 = new mOL();
m1.mOL(3);
}
//如下的三个方法构成重载
public void mOL(int i){
System.out.println(i*i);
}
public void mOL(int i,int j){
System.out.println(i * j);
}
public void mOL(String s){
System.out.println(s);
}
//如下的三个方法构成重载
public int getMax(int a,int b){
int max = (a > b) ? a : b;
return max;
}
public double getMax(double a,double b){
double max = (a > b) ? a : b;
return max;
}
public double getMax(double a,double b,double c){
double maxmid = (a > b) ? a : b;
return max = (maxmid > c) ? maxmid : c;
}
}
JavaSE 5.0中提供了Varargs(variable number 0f arguments)机制,允许直接定义能和多个实参相匹配的形参。从而,可以用一种更简单的方式,来传递个数可变的形参。
public class ArgsTest{
public static void main(String[] args){
ArgsTest arg = new ArgsTest();
System.out.println(arg.show(5));
System.out.println(arg.show("hello"));
// String h = arg.show("hello");
// System.out.println(arg.show("hello","world","hello"));
arg.show("hello","world","hello");//或arg.show(new String[]{"hello","world","hello"})
// String[] s = arg.show("hello","world","hello");//输出数组的地址
}
public int show(int i){
return i;
}
public String show(String s){
return s;
}
// public String[] show(String...strs){//相当于public String[] show(String[] strs)
// return strs;
public void show(String...strs){
for(int i = 0;i < strs.length;i++){
System.out.println(strs[i]);
}
}
// 可变个数形参在方法的形参中,最多只能声明一个,且位置只能在末尾。
//错误演示如下;
public void show(String...strs,int i){//报错:Vararg parameter must be the last in the list
}
//正确演示如下;
public void show(int i,String...strs){//报错:Vararg parameter must be the last in the list
}
}
public class ValueTransferTest{
public static void main(String[] args){
System.out.println("******基本数据类型*******");
int m = 10;
int n = m;
System.out.println("m=" + m + ",n=" + n);//m = 10, n = 10
n = 20;
System.out.println("m=" + m + ",n=" + n);//m = 10, n = 20
System.out.println("******引用数据类型*******");
Order o1 = new Order();
o1.orderId = 1001;
Order o2 = o1;//o1存的是new出来的对象在堆空间中的地址值,赋值后,o1和o2地址值相同,都指向了堆空间中同一个对象实体
System.out.println("o1.orderId=" + o1.orderId + ",o2.orderId=" + o1.orderId)//都是1001
o2.orderId = 1002;
System.out.println("o1.orderId=" + o1.orderId + ",o2.orderId=" + o1.orderId)//都是1002
}
}
class Order(){
int orderId;
}
1、方法:必须由其所在类或对象调用才有意义,若方法含有参数:
2、值传递机制:
1、基本数据类型
public class ValueTransferTest1{
public static void main(String[] args){
int m = 10;
int n = 20;
System.out.println("m=" + m + ",n=" + n);//m = 10, n = 20
//交换两个变量的值的操作
// int temp = m;
// m = n;
// n = temp;
// System.out.println("m=" + m + ",n=" + n);//交换,m=20,n=10
ValueTransferTest1 test = new ValueTransferTest1();
test.swap(m,n);//只是交换了swap方法中m和n的值,未交换main方法中m和n的值
System.out.println("m=" + m + ",n=" + n);//未交换,m=10,n=20
}
//交换两个变量值的方法
public void swap(int m, int n){
int temp = m;
m = n;
n = temp;
}
}
public class ValueTransferTest2{
public static void main(String[] args){
Data data = new Data();
data.m = 10;
data.n = 20;
ValueTransferTest2 test = new ValueTransferTest2();
test.swap(data);
System.out.println("m=" + data.m + ",n=" + data.n);//交换成功,m = 20, n = 10
}
public void swap(Data data){
int temp = data.m;
data.m = data.n;
data.n = temp;
}
}
class Data{
int m;
int n;
}
public class ValueTransferTest {
public static void main(String[] args) {
PassObject p = new PassObject();
p.printAreas(new Circle(),5);
// Circle c = new Circle1();
// p.printAreas(c,5);
System.out.println("now radius is " + c.radius);
}
}
class Circle {
double radius;//圆的半径
//圆面积
public double findArea(){
return Math.PI * radius * radius;
}
}
class PassObject{
public void printAreas(Circle c,int times){
System.out.println("Radius\t\tAreas");
for(int i = 1;i <= times;i++){
c.radius = i;
System.out.println(c.radius + "\t\t" + c.findArea() );
}
c.radius = times + 1;
}
}
递归方法:一个方法体内调用它自身。
例子:计算100以内的数的的和
package ValueTransferTest;
//例1:计算100以内的数的的和
public class RecursionTest {
public static void main(String[] args) {
RecursionTest test = new RecursionTest();
test.getSum(100);
}
public int getSum(int n){
if(n == 1){
return 1;
}else{
return n + getSum(n-1);
// return 3 + getSum(2);
// return 3 + 2 + getSum(1);
// return 3 + 2 + 1;
}
}
//例2:计算1~n之间所有自然数的乘积(n!)
public int getSum1(int n){
if(n == 1){
return 1;
}else{
return n * getSum1(n-1);
// return 3 + getSum(2);
// return 3 + 2 + getSum(1);
// return 3 + 2 + 1;
}
}
//例3:已知一个数列:f(0)=1,f(1)=4,f(n)=2*f(n-1)+f(n-2),其中n是大于0的整数。
public int getSum2(int n){
if(n == 0){
return 1;
}else if(n == 1){
return 4;
} else{
return 2 * getSum2(n-1) +getSum2(n-2);
}
}
}