1、定义方法
修饰符 返回值类型 方法名(参数列表){ //方法体 }
例子: public class HelloWorld { //main method public static void main(String[] args){ int i = 5, j =3; int k = max(i, j); System.out.println("The maximum between " + i + " and " + j + " is " + k); } public static int max(int num1, int num2){ int result; result = (num1 > num2) ? num1 : num2; return result; }
}
2、传值与传引用
1)传值是值传递参数的时候传的是值的拷贝,对参数的操作不会改变原值
public class HelloWorld { public static void change(int age){ age = 30; } //main method public static void main(String[] args){ int test1 = 10; System.out.println("Before change, the age is " + test1); change(test1); System.out.println("Afther change, the age is " + test1); } }
输出结果:
Before change, the age is 10
Afther change, the age is 10
2)参数的引用传递:传递的是引用的地址,也就是变量所对应的内存空间的地址。
public class HelloWorld { public static void change(A a){ a.age = 30; }
//main method public static void main(String[] args){ A test1 = new A(); test1.age = 10; System.out.println("Before change, the age is " + test1.age); change(test1); System.out.println("Afther change, the age is " + test1.age); }
static class A{ public int age; } }
输出结果:
Before change, the age is 10
Afther change, the age is 30
3)如果传引用时,在处理函数如果操作的不是传递进来的引用所指向的内存空间,而是new出来的一个新的空内存,那么操作不会改变原来的值。
public class HelloWorld { public static void change(A a){ a = new A(); a.age = 30; }
//main method public static void main(String[] args){ A test1 = new A(); test1.age = 10; System.out.println("Before change, the age is " + test1.age); change(test1); System.out.println("Afther change, the age is " + test1.age); }
static class A{ public int age; }
}
输出结果:
Before change, the age is 10
Afther change, the age is 10
4)数组作为参数传递的时候,属于引用传递,函数对数组的改变会影响原来的数组
public class HelloWorld { public static void change(int[] age){ age[0] = 30; }
//main method public static void main(String[] args){ int[] test1 = new int[] {20,10}; System.out.println("Before change, the first number is " + test1[0]); change(test1); System.out.println("Afther change, the first number is " + test1[0]); } }
输出结果:
Before change, the first number is 20
Afther change, the first number is 30
5)String:String类型(引用)作为参数传递时传递的是引用,只是对String做出任何修改时有一个新的String对象会产生,原来的String对象的值不会做任何修改。
但是可以将新的对象的引用赋给原来的引用,这样给人的表面现象就是原来的对象变了,其实没有变,只是原来指向它的引用指向了新的对象
public class HelloWorld { public static void change(String src){ src = "Hello"; }
//main method public static void main(String[] args){ String test = "Good!"; System.out.println("Before change, the message is " + test); change(test); System.out.println("Afther change, the message is " + test); } }
输出结果:
Before change, the message is Good!
Afther change, the message is Good!
3、重载
具有相同的函数名字,但是函数参数列表不同的函数。不能根据不同的修饰符和返回值类型来重载方法。java编译器根据方法签名调用具体的方法
public static int max(int num1, int num2){ //函数体 }
public static double max(double num1, double num2){ //函数体 }
1)变量的作用域:变量在程序中可以引用的范围。方法中定义的变量称为局部变量,局部变量的范围从声明变量的地方开始,直到包含该变量的块结束。局部变量都必须在使用之前声明与赋值。
2)Math数学类方法 调用:Math.toDegrees(a); Math.sin(a);
三角函数类
public static double sin(double radius)
public static double cons(double radius)
public static double tan(double radius)
public static double toRadians(double degree)
public static double toDegrees(double radius) //弧度转成角度
public static double asin(double a)
public static double acos(double a)
public static double astan(double a)
指数函数类
public static double exp(double x) // e的x次方 public static double log(double x) //e为底 public static double log10(double x) //10为底 public static double pow(double a, double b) //a的b次方 public static double sqrt(double x) //开方
取整函数
public static double ceil(double a) //向上取整 public static double floor(double a) //向下取整 public static double rint(double a) //就近取整,如果距离两个整数一样近,返回就近的偶数 public static int round(float x) //四舍五入
min、max、abs方法
random方法
a + Math.random() * b --> 返回a ~ a+b之间的随机整数,不包括a+b
返回0~9之间的随机数: (int) (Math.random()*10) 返回50 ~ 99之间的随机数: 50 + (int (Math.random() * 50)) 返回随机的小写字母: (char)('a'+Math.random() *('z' - 'a' + 1)) 返回另个字符ch1,ch2之间的随机一个字符:(char)( 'ch1' + Math.random() * ( 'ch2' - 'ch1' + 1 ))
程序练习: 打印日历
package demo; import java.util.Scanner; /** * 打印日历 * Created by luts on 2015/11/26. */ public class PrintCalendar { public static void main(String[] args){ Scanner input = new Scanner(System.in); //提示用户输入年份 System.out.println("Enter full year (e.g., 2001)"); int year = input.nextInt(); //提示用户输入月份 System.out.println("Enter monrh in numer between 1 and 12"); int month = input.nextInt(); //打印日历 printMonth(year, month); } public static void printMonth(int year, int month){ //打印日历头 printMonthTitle(year, month); //打印日历主体 printMonthBody(year, month); } public static void printMonthTitle(int year, int month){ System.out.println(" " + getMonthName(month) + " " + year); System.out.println("---------------------------------"); System.out.println(" Sun Mon Tue Wed Thu Fri Sat"); } //获取月份英文名 public static String getMonthName(int month){ String Monthname = " "; switch (month){ case 1: Monthname = "Jannuary"; break; case 2: Monthname = "February"; break; case 3: Monthname = "March"; break; case 4: Monthname = "April";break; case 5: Monthname = "May"; break; case 6: Monthname = "June"; break; case 7: Monthname = "July"; break; case 8: Monthname = "August"; break; case 9: Monthname = "September"; break; case 10: Monthname = "Octorber"; break; case 11: Monthname = "November"; break; case 12: Monthname = "December"; } return Monthname; } //打印月份主体 public static void printMonthBody(int year, int month){ //获取月份第一天的星期 int startDay = getStartDay(year, month); //计算月份总共的天数 int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month); //打印空格 int i = 0; for (i = 0; i < startDay; i++) System.out.print(" "); for (i = 1; i <= numberOfDaysInMonth; i++){ System.out.printf("%4d",i); if ((i + startDay) % 7 == 0) System.out.println(); } System.out.println(); } public static int getStartDay(int year, int month){ final int START_DAY_FOR_JAN_1_1800 = 3; //计算1800/1/1到输入日期的天数 int totalNumberOfDays = getTotalNumberOfDays(year, month); //返回输入日期的星期 return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7; } //获取总天数 public static int getTotalNumberOfDays(int year, int month){ int total = 0; for (int i = 1800; i < year; i++){ if (isLeapYear(i)) total += 366; else total += 365; } for (int i = 1; i < month; i++){ total = total + getNumberOfDaysInMonth(year , i); } return total; } public static int getNumberOfDaysInMonth(int year, int month){ if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) return 31; if (month == 4 || month == 6 || month == 9 || month == 11) return 30; if (month == 2) return isLeapYear(year) ? 29 : 28; return 0; } public static boolean isLeapYear(int year){ return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); } }
每次调用一个方法的时候,系统都将参数和局部变量存储在一个被称为堆栈(stack)的区域中。当一个方法调用另外一个方法的时候,调用者的堆栈空间保持不动,开辟新的空间处理新方法的调用。一个方法完成它的工作之后返回到它的调用者,就释放其相应的空间