基础语法
- Jdk:开发工具包。jre在Jdk内部,用于运行环境。jvm在jre内部,是Java核心——虚拟机。
- Jdk安装目录的空格要删除,且不要有中文,取消公共jre。
- 命令提示符cmd调用:win+R。其中dir=ls,cls=clear, exit退出。
- 编译:javac 文件名.java(dir看一下是否有文件名.class编译结果);运行:java 文件名(不要加.class)没有c就没有.class。
- Java源代码区分大小写,第三个单词与文件名要一致(报错:java:2指第二行有错)。
- path路径配置:此电脑>属性>高级系统设置>高级>环境变量;%SystemRoot%=c盘下的windows,%%是翻译,%JAVA_HOME%\bin。
- 类是Java当中所有代码的一个基本组织单位,类里面的第二行是固定写法,程序启动的起点。
- Java关键字完全由小写字母组成,且有特殊颜色。
- 常量:程序运行期间不可以发生改变的量。
1)字符串常量,凡是由双引号引起的,如下,可以有0-n个字符;
2)整数常量:100、200;
3)浮点常量:3.14、-2.5、0.0;
4)字符常量:有且仅有一个字符,由单引号引起;
5)布尔常量:只有两种取值——true、flase;
6)空常量:null。
public class DemoConst{
public static void main(String[] args) {
// 字符串常量
System.out.println("hi!");
// 整数常量
System.out.println(-200);
// 浮点常量
System.out.println(3.14);
// 字符常量
System.out.println('9');
System.out.println('A');
// 布尔常量
System.out.println(true);
System.out.println(false);
// 空常量不可以直接用于打印
System.out.println(null);//错误写法
}
}
- 变量:用一小块内存空间存放数据,内容可以发生改变。
- 使用变量的基本格式:
1)方案一:
1.数据类型 变量名称; //创建变量
2.变量名称 = 数据值;//等号代表赋值动作
2)方案二:
1.数据类型 变量名称 = 数据值; //创建的同时立刻赋值 - 标识符:给类、变量、包、方法等命名。规则:
1)只能由字符、下划线_、美元$组成;
2)不能以数字开头;
3)不能使用java关键字;
4)见名知意;
5)建议只用英文字母和数字;
6)a.类:每个单词首字母大写——大驼峰HelloWorld;
b.变量:第一个单词完全小写,后续更多单词首字母大写,如ageOfMyDog;
c.方法:与变量规则一样,getAge();
d.包:即文件夹,用于对类进行管理。全部小写,多级包用点隔开,公司域名的反写。
cn.itcast (=cn\itcast) - 数据类型
/*
java当中等数据类型分两种:
1、基本类型(除了这个8个单词,都是引用。每个类型占用等内存字节数不同(需牢记),float是科学计数法,省空间且数据表示范围广。)
整数:byte short int long
浮点: float(3.403E38=3.403乘以10的38次方) double
字符: char
布尔: boolean
2、引用类型:字符串、类、接口、数组、Lambda
*/
注意事项:
1)整数类型有4种,默认为int;
2)浮点类型有2种,默认为double;
3)定义一个long型数据,在数值后要用字母后缀L,如5200000L;
4)定义一个float型数据,在数值后要用字母后缀F,如3.14F;
5)字符char型,可以包含中文。
14. 对于byte、short、int类型等变量来说,只要右侧不超过左侧等范围即可。但对于long类型的变量来说,右侧直接写上就是一个int类型,所以一定要加L后缀。
public class Demo01DataType{
public static void main(String[] args) {
byte num1; // 创建一个byte类型等变量,名叫num1
num1 = 100;
System.out.println(num1);
num1 = 108;
System.out.println(num1);
//一个步骤完成赋值
byte num2 = 90;
System.out.println(num2);// 右侧赋值等数值不能超过左侧变量等类型范围,如 byte num3 = 128
long num4 = 42000000000L;// 不加L会报错“整数过大”,L是对类型的说明
float num5 = 3.14F;
double num6 = 2.5;
char word = '中'; // 一个字符
boolean var1 = true;
// 字符串变量如何使用(String 不是关键字)
String str1;
str1 = "hello";
String str2 = "hello";
}
}
- 变量注意事项:
1)创建等多个变量不能重名;
2)变量如果没有赋值,不能直接使用;
3)变量等作用域问题;
【作用域】变量定义在哪个大括号当中就只能在哪里使用。
4)可以提供一个步骤,同时定义多个类型相同等变量。
public class Demo02Notice{
public static void main(String[] args) {
int num1 = 100;
int num1 = 200; // num1不能重名
int num3; // 会报错
{
int num4 = 40;
System.out.println(num4);
}
System.out.println(num4); // 会报错,因为num4嵌套在内层。但是可以重新定义num4变量,无冲突。
int a, b, c;
a = 10;
b = 20;
c = 30;
// 一个步骤完成赋值
int x = 100, y = 200, z = 300;
}
}
- 算数运算符功能(运算符连起来多式子为表达式)
public class Demo03Operator{
public static void main(String[] args) {
int x = 10, int y = 20;
System.out.println(x - y); // 如果括号里是表达式,会首先进行表达式计算,其次再将结果打印
// 但除法例外
int a = 10, b = 3;
System.out.println(a / b);// Java中,整数除法只看商,不看余数,因此结果为3.
// 如果计算时有小数参与,那么结果必是小数
int m = 10;
double n = 3.0;
System.out.println(m + n); // 结果是13.0
}
}
/*
对于整数来说,“/”除法只看商不看余数;
如果看余数,则要用取模运算符:%。
*/
public class Demo04Operator{
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println(a / b);// 结果是3
System.out.println(a % b); // 结果是1
// 推荐只对整数运算进行取模
}
}
// 对于字符串来说,+不是相加,而是连接。
public class Demo04Operator{
public static void main(String[] args) {
String str1 = "hello";
String str2 = str1 + "world"
System.out.println(str2); // 结果是 helloworld
String str3 = str2 + "!"; //结果是 helloworld!
String str4 = "hello" + 30; //结果是 hello30,因为任何数据类型与字符串进行连接操作,结果都一定是字符串
String str5 = "hello" + 10 + 20; // 结果是 hello1020,因为字符串相连等顺序是从左至右
String str6 = "hello" + (10 + 20); // 结果是 hello30,因为小括号有绝对优先级
}
}
- 自增/减运算符
/*
自增运算符++:在原有变量基础上累加一个1(涨一个数);
自减运算符--:在原有变量基础上累减一个1(降一个数);
使用格式:用在变量前 ++num;用在变量后 num+。
使用方式:1.单独使用;2.混合使用:如果是前++,那么变量立刻+1;如果是后++,那么首先使用变量当前等数据。
注意事项:自增/减运算符只能用于变量,不能用于常量。
*/
public class Demo05Operator{
public static void main(String[] args) {
// 单独使用时,格式无区别
int num1 = 10;
++num1; // 结果是11
num1++; // 结果是12,因为前面已经加过一次了。
// 混合使用时,前++和后++有区别!
System.out.println("======="); // 分割线
int num2 = 20;
System.out.println(++num2); // 结果是21
System.out.println(num2); // 结果是21
int num3 = 30;
System.out.println(num3++); // 结果是30
System.out.println(num3); // 结果是31
//如果和赋值混合使用
int x = 10;
int y = 20;
int result1 = x++;
System.out.println(result1); // 结果是10
System.out.println(x); // 结果是11
System.out.println("======="); // 分割线
int a = 10;
int b = 20;
int reasult2 = ++a + b--;
System.out.println(reasult2);// 结果是31
System.out.println(a); // 11
System.out.println(b); // 19
// 总结:前++:先加后用;后++:先用后加。
}
}
- 赋值运算符
/*赋值运算
1. “=”为基本赋值运算符;
2. 复合运算符
+= a += 3 a = a + 3; 如a是10 a+=3是13
-= b -= 4 b = b - 4
*= c *= 5 c = c * 5
/= d /= 6 d = d / 6
%= e %= 7 e = e % 7
注意事项:
1. 赋值运算符左侧一定是变量,不能是常量,右侧无所谓。
*/
public class Demo06Operator{
public static void main(String[] args) {
int num1 = 20;
num1 += 5; // 25
int a = 10;
a %= 3; // 1
// 只要左边是变量,右侧常量变量都可以
int var1 = 100;
int var2 = var1;
}
}
- 比较运算符=关系运算符,常量和变量均可以比较,结果一定是个boolean值(true/false)。
- 逻辑运算符
/*
与(并且) & 全都是true才是true,否则就是false;
或 | 至少一个true就是true,全都是false才是false;
亦或 ^ 相同是false,不同才是true;
非(取反) ! 本来是true变成false,本来是false变成true。
注意事项:
1. 逻辑运算符通常用来连接多个boolean值;
2. 如果是多个布尔值连接:布尔值A & 布尔值B & 布尔值C;
3. 如果是取反运算符,只能用于一个布尔值前面:!布尔值。
*/
public class Demo07Operator{
public static void main(String[] args) {
System.out.println(false & false); // false
boolean a = true;
boolean b = false;
System.out.println(a & b); // false
System.out.println(4 > 3 & 10 < 20); // true
System.out.println(false ^ false); // false
System.out.println(true ^ false); // true
System.out.println(!false); // true
}
}
- 短路:如果左侧已经可以判断最终结果,右侧将不再继续执行。
/*
短路与:&&
短路或:||
1. 凡是用到与、或这两个逻辑运算的时候,都推荐使用双写,以提高代码执行效率;
2. 亦或、非不可以用双写。
*/
public class Demo07Operator{
public static void main(String[] args) {
System.out.println(3 < 2 & 10 < 20); // false
System.out.println(3 < 2 && 10 < 20); // false,左边可以判断,因此右边不会继续执行
int num1 = 100;
System.out.println(3 > 10 && ++num1 < 200); // false
System.out.println(num1); // 100
int num2 = 20;
System.out.println(3 < 10 || ++num2 > 3); // true
System.out.println(num2); //20
int num3 = 300;
System.out.println(3 > 10 || ++num3 < 1000); // true
System.out.println(num3); // 301
}
}
- Scanner引用类型:
导包:import>创建:数据类型 变量名称 = new 数据类型()>使用:变量名称.方法名()
import java.util.Scanner;
public class Demo08Scanner{
public static void main(String[] args){
//数据类型 变量名称 = new 数据类型()
Scanner sc = new Scanner(System.in); // System.in指从键盘输入
// 获取键盘输入的int数字
int num = sc.nexInt(); // 右侧键盘输入int,并将int存储在左侧num中
num += 20;
System.out.println("结果" + num);
// 增加一个提示信息
System.out.println("请输入第一个字符串:");
String str1 = sc.next(); // 右侧键盘输入字符串,并将字符串存储在左侧str中
System.out.println("字符串1" + Str1);
}
}
- 流程:指程序执行步骤的先后顺序。
1)顺序结构:从上到下、从前往后,顺序执行;
2)选择结构:执行路线分叉,也叫分支结构;
3)循环结构:重复做事。
import java.util.Scanner;
public class Demo09Scanner {
public static void main(String[] args){
Sacnner sc = new Scanner(System.in);
System.out.println("请输入一个整数");
/* 选择结构:if语句
if(boolean表达式){
语句体
}
if(boolean表达式){
语句体A
}else{
语句体B
}
if(条件判断1){
语句体1
}else if (条件判断2){
语句体2
}
else{
语句体N
}
*/
if(age >= 18){
System.out.println("=="); // 选择成立才会print
}
if(num % 2 == 0){
System.out.println("偶数");
}else {
System.out.println("奇数");
}
// 成绩档次划分,优秀:85-100;良好:70-85;及格:60-70;不及格:60以下
System.out.println("请输入成绩");
int score = sc.nexInt();
if(score >= 85 && score <= 100){
System.out.println("优秀");
} else if(score >= 70 && score < 85){
} else if (score >= 60 && score < 70){
} else if (score >= 0 && score < 60) // 边界设置 {
System.out.println("不及格");
} else {
System.out.println("数据有误");
}
}
}
- 比较最大值(例子较啰嗦,仅供参考,实际应用不要这么写)
import java.util.Scanner;
public class Demo10Scanner {
public static void main(String[] args){
Sacnner sc = new Scanner(System.in);
// 2个值中求最大
System.out.println("请输入第一个数字");
int a = sc.nextInt();
System.out.println("请输入第二个数字");
int b = sc.nextInt();
int max;
if (a > b) {
max = a;
System.out.println("最大值" + max);
} else {
max = b;
System.out.println("最大值" + max);
}
// 3个值中求最大
System.out.println("请输入第一个数字");
int a = sc.nextInt();
System.out.println("请输入第二个数字");
int b = sc.nextInt();
System.out.println("请输入第三个数字");
int c = sc.nextInt();
// 首先比较前两个
int temp;
if (a > b) {
temp = a;
} else {
temp = b;
}
// 将中间量与c比较
int max;
if (temp >c ) {
max = temp;
} else {
max = c;
}
System.out.println("最大值" + max);
}
}
- 循环结构:
1)初始化语句:只执行一次;
2)条件判断:如果成立则循环继续,如果不成立则循环退出;
3)循环体:每次循环都要将重复执行多代码
4)步进语句:每次循环体执行后,都会执行一次步进。
【for循环】
for(1.初始化语句;2.条件判断;4.步进语句){
3.循环体
}
执行顺序:1234>234>234…直到2不满足为止
public class Demo11for {
public static void main(String[] args){
// 格式for(;;){}
for (int i = 1; i <= 10; i++) {
System.out.println("love u" + i);
}
// 求1-100偶数和
int sum = 0; // 一定要定义在for循环外面,用来不断累加
for (int i = 1; i <= 100; i++) {
// 判断是不是偶数
if (i % 2 ==0) {
sum += i;
}
// 如果不是偶数,无影响,会继续判断后面的数字
}
System.out.println("偶数和" + sum);
}
}
【while】循环
1.初始化语句
while(2.条件判断){
3.循环体
4.步进语句
}
执行顺序:1234>234>234…直到2不满足为止
public class Demo12while {
public static void main(String[] args){
int i = 1; // 初始化表达式
while (i <= 10) {
System.out.println("love u 2" + i);
i++;
}
// 求1-100奇数和
sum = 0;
int i = 1;
while (i <= 100) {
if (i % 2 == 1) {
sum += i;
}
i++;
}
System.out.println("奇数和" + sum);
}
}
【for】和【while】循环的区别
1)for循环格式固定,控制次数更方便;while循环格式灵活,擅长只管条件不管次数;
2)for循环小括号内定义的变量,只能在循环内使用;while循环多初始化表达式不在循环内;
3)跳转语句continue效果不同。
public class Demo13ForVsWhile {
public static void main(String[] args){
for (int a = 1; a <= 5; a++) {
System.out.println(a); // 1-5都显示
}
// System.out.println(a); 会报错,因为不在for循环内
int b = 1;
while (b <= 5) {
System.out.println(b);
b++;
}
System.out.println(b); // 不会报错,会print 6,因为b++
}
}
- 跳转控制语句
【break】打断当前循环(只要在{}里,就是循环体)
【continue】只跳过当前次循环,马上开始下次循环
public class Demo14BreakContinue {
public static void main(String[] args){
for (int i = 1; i <= 5; i++) {
// 如果当前是第三次,循环停止
if (i == 4) {
break;
}
System.out.println("今晚吃什么?" + i);
}
for (int i = 1; i <= 10; i++) {
// 如果当前是4楼,跳过,继续到5楼
if (i == 4) {
continue; // 这行代码一旦执行,循环体当中剩余内容将被跳过,马上开始下一次循环
}
System.out.println(i + "楼到了。");
}
// while循环会出现死循环,因为System和i++都是循环体剩余内容
int i = 1;
while (i <= 10) {
if (i == 4) {
continue;
}
System.out.println(i + "楼到了。");
i++;
}
}
}
【break】与【continue】的区别
public class Demo15BreakVsContinue {
public static void main(String[] args){
for (int i = 1; i <= 5; i++) {
// 如果当前是第三次,循环停止
if (i == 4) {
break; // 一旦执行,整个循环立刻结束
}
System.out.println(i); // 结果123
}
for (int i = 1; i <= 5; i++) {
// 如果当前是第三次,循环停止
if (i == 4) {
contine; // 只跳过当前次
}
System.out.println(i); // 结果1235
}
}
- 死循环:永远不会停不下来的循环,也叫永真循环。
public class Demo16DeadLoop {
public static void main(String[] args){
// 标准格式
while (true) {
System.out.println("帅");
}
// 扩展格式
for (;;){
System.out.println("帅");
}
// 以上仅为例子,两个死循环在一起会报错
}
}
- 循环嵌套
public class Demo17Loop {
public static void main(String[] args){
int count = 0;
for (int i = 1; i <= 300; i++) { // 300个班
for (int j = 1; i <= 80; j++) { // 每班80人
count ++;
}
}
System.out.println("总人数" + count);
// 打印一天当中所有的分钟时刻
for (int hour = 0; i < 24; i++){ // 外层循环
for (int minute j = 0; j < 60; j++) { //内层循环
System.out.println(hour + "点" + minute + "分");
}
}
// 循环嵌套跳转语句
int count = 0;
for (int i = 1; i <= 10; i++) { // 外层循环10次
if (i == 3) { // 如果是3班,跳过该次循环。作用于外层的for循环,下面for{}循环体将被跳过
continue;
}
for (int j = 1; i <= 5; j++) { // 内层循环5次
if (j == 3){ // 如果内层循环出现学号3,跳过该次循环
continue;
}
count ++;
}
}
System.out.println("总次数"+count);
}
}
如果希望在内层跳转,但作用于外层循环,加label
public class Demo18Loop {
public static void main(String[] args){
int count = 0;
label: for (int i = 1; i <= 10; i++) {
for (int j = 1; i <= 5; j++) {
if (j == 3){
break label;
}
count ++;
}
}
}
}
- Eclipse:所有Java源代码都写在Src文件夹里。新建package文件名用.分割,文件名必须小写,不能用数字开头。在package里新建一个class,class名驼峰式。
【快捷键】
单行注释:ctrl+/
多行注释:ctrl+shift+/
快速复制:ctrl+Alt+向下/上
移动位置:Alt+向上/下
删除当前行:Ctrl+D
格式化:Ctrl+shift+F
智能提示:Alt+/
统一重命名:Alt+shift+R - 数组:是一种引用类型,可以存放多个数据类型统一的数据(可以存放基本类型或引用类型)。
【注意事项】
1)数据类型[] 数组名称;
2)变量如果定义好了,要想使用需要赋值;数组如果定义好了,需要初始化。
【初始化】在内存当中开辟数组的空间,并且赋予一些默认值。
1、动态初始化,指定数组长度(到底可以存放多少个数据);格式:数据类型[] 数组名称 = new 数据类型[数组长度]
2、静态初始化,指定数组内容。不会直接指定长度,而是直接指定具体的元素内容。格式:数据类型[] 数组名称 = new 数据类型[]{元素1, 元素2, 元素3, …}
【注意事项】
1)静态初始化可以根据指定元素推算出指定长度;
2)虽然简便格式可以不写new,但仍会开辟内层空间,且不会变少;
3)虽然直接指定了具体元素内容,但仍有默认值被替换的过程。
public class Demo18Loop {
public static void main(String[] args){
// 动态初始化格式1
// 数据类型[] 数组名称 = new 数据类型 [数组长度]
int [] array1 = new int[3];
// 动态初始化格式2
// 数据类型[] 数组名称;
// 数组名称 = new 数据类型 [数组长度]
int [] array2;
array2 = new int[3];
int [] arrayA = new int[3];
System.out.println(arrayA); // 打印地址值16进制的哈希值。10进制:0123456789;二进制:01;16进制:0123456789abcdef(10进制18=16进制0x12)。
// 如何访问数组中的元素?数组名称[索引]
// 数组编号从0开始,一直到长度-1为止
System.out.println(arrayA[0]); // 结果是0,因为动态初始化的时候,数组当中的元素会被赋予一个默认值。如果是int,默认0;如果是double,默认0.0;如果是String,默认'\u0000';布尔值默认false。
double [] arrayB = new double[3];
System.out.println(arrayB[1]); // 0.0
// 改变数组中的具体元素
arrayB[1] = 3.14;
System.out.println(arrayB[1]); // 3.14
// 或者赋值变量
double num = arrayB[1];
System.out.println('num变量的内容' + num); // 3.14
// 静态初始化格式1
int arrayB = new int [] {10, 20, 30};
System.out.println(arraryB);// 仍是地址值
System.out.println(arraryB[1]); // 20
// 静态初始化格式2
int arrayC;
arrayC = new int [] {15, 25, 35};
// 静态初始化简便格式,必须一个步骤完成
int[] arrayD = {50, 70, 90};
System.out.println(arraryD[1]); // 70
}
}
- 获取数组长度
【注意事项】
1)一个数组一旦在内存当中被创建了,那么数组的长度就不能发生改变,但可以用新数组去替换,那么数组地址不然发生改变。
2)如果访问的数组元素索引不存在,会发生“数组索引越界异常”,原因是访问的数组元素不存在。
public class Demo18Loop {
public static void main(String[] args){
int [] arrayA = {1, 2, 3, 5, 2, 8, 5};
// 数组名称.length
System.out.println(arraryA.length): // 7
int[] arrayD = {50, 70, 90};
System.out.println(arraryD[3]); // 编译不报错,但运行会报异常【exception】。
// 数组索引从0开始,arraryD[-1]也会报错
}
}
- Java内存分配
1)栈(stack):主要用来存放局部变量(如,int[] array);
2)堆(heap):凡是new出来的东西,都存放在堆里。堆当中的数据有默认值规则(如果是引用类型,默认值为null);
3)方法区(method area):存放.class相关的信息;
4)本地方法区(native method area):与操作系统相关;
5)寄存器(pc Register):与cpu相关,性能极高。 - 求数组当中的最大值、和
public class Demo18Loop {
public static void main(String[] args){
// 求数组当中的最大值
int[] arrayA = {5, 10, 15, 40, 30, 1000};
int max = arrayA[0];
// 逐一处理用循环,次数确定用for
for (int i = 1; i < arrayA.legth; i++){
if (array[i] > max) {
max = arrayA[i];
}
}
System.out.println("最大值" + max);
// 求数组当中的元素和
int[] arrayB = {5, 10, 15, 40, 30, 50};
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += arrayB[i];
}
System.out.println("总和" + sum);
}
}
- 定义方法
【参数】:进入方法的数据;
【返回值】:从方法中出来的数据;
【如何定义一个方法】:
修饰符 返回值类型 方法名称(参数类型 参数名称) {
方法体
return 返回值;
}
【修饰符】:固定写法,两个关键字,如 public static;
【返回值类型】:方法最终产生到数据是什么类型;
【方法名称】:自定义到名字,规则和变量一样;
【参数类型】:进入方法的数据是什么类型;
【参数名称】:进入方法的数据,对应的变量名称;
【方法体】:需要执行的若干行代码;
【return】:结束当前方法,把返回值交给调用处。
【注意事项】
1、返回值类型必须和返回值对应;
2、如果参数有多个,那么使用逗号进行分隔。
public class Demo19MethodDefine{
public static void main(String[] args){ // arguments,main方法是public static void main固定格式
}
// 定义一个方法,实现两个int数字相加和值的功能
public static int sum(int a, int b) {
int result = a + b;
return result;
/*
1. 多个方法之间定义的前后顺序无所谓,main方法和sum方法颠倒顺序也可以;
2. 不能在一个方法里嵌套另一个方法;
3. 方法定义之后不会执行,一定要调用。
*/
}
}
- 方法调用
public class Demo20MethodInvoke{
public static void main(String[] args){
// 单独调用:无法使用方法的返回值。
sum(10, 20);
// 打印调用:将返回值打印显示出来
System.out.println(sum(100, 200));
// 赋值调用:数据类型 变量名称 = 方法名称(参数值)
// 变量的数据类型必须与方法的返回值类型一致
int num = sum(15, 23);
System.out.println(num); // 38
num += 100;
System.out.println(num); // 138
}
public static int sum(int a, int b) {
System.out.println("方法执行");
int result = a + b;
return result;
}
}
36. 方法与变量名称可以一样;两个方法可以有同名变量,但虽然名称一样,却是两个不同的变量。
37. 方法的参数
public class Demo21MethodParam{
public static void main(String[] args){
System.out.println(methodThree(10, 20, 30); // 60
}
// 参数有多个
public static int methodThree(int a, int b, int c) {
int result = a + b + c;
return result;
// 参数有一个
public static int methodOne(int num) {
int result = num + 100;
return result;
}
// 没有参数的情况
public static int methodNone() {
int num = 10000;
return num;
}
}
- 定义方法最大值
【步骤】
1、键盘输入——Scanner(导包、创建、使用)
2、定义方法的三要素:返回值值类型 int、方法名称 getMax
3、调用方法,得到返回值
4、打印最终结果
【注意事项】
对于有返回值的方法,必须保证有且仅有一个return会被执行。
package cn.intcast.day05.demo01;
import java.utile.Scanner;
public class Demo22MethodParam{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("insert no.1");
int a = sc.nextInt();
System.out.println("insert no.2");
int b = sc.nextInt();
int max = getMax(a, b)
}
public static int getMax(int x, int y) {
int max;
if (x > y) {
max = x;
} else {
max = y;
}
return max;
if (x > y) {
return x;
} else {
return y;
}
/*
无返回值的方法定义格式:没有最终的数据结果要交还给调用处。返回值类型: void
修饰符 void 方法名称(参数类型 参数名称) {
方法体
return 返回值;
}
*/
public static void main(String [] args) { // 单独调用
printHelloWorld();
printHelloWorldCount(5);
// 不能打印调用!因为没有返回值!
System.out.println(printHelloWorld());
// 不能赋值调用
int num = printHelloWorld();
}
public static void printHelloWorld() {
for (int i = 1; i <= 10; i++) {
System.out.println("Hello World" + i);
}
}
public static void printHelloWorldCount(int count) { // 次数不确定的时候,把次数定义为参数写在方法里
for (int i = 1; i <= count; i++) {
System.out.println("Hello World" + i);
}
}
}
【无返回值方法调用注意事项】
1.返回值没有不代表不能有参数;
2.不能return一个具体的返回值;
3.如果return是最后一行,一般可以省略。
- 参数传递:
【形式参数】在定义方法到时候,写在小括号内到函数。如public class Demo01MethodParaBasic ()
【实际参数】在调用方法到时候,传入方法里到数据。如 int num = sum (10, 20); // 这里的10和20就是实际参数
int result = sum(x, y); // 这里的到x和y是调用方法时传入的,因此也是实际参数。
【注意事项】
1)对于基本数据类型(包含string)来说,形式参数的操作不会影响实际参数;
2)对于引用数据类型(除了string)来说,形式参数的操作会影响实际参数。
public class Demo23MethodParam{
public static void main(String[] args){
int a = 10; // 基本数据类型
int b = 20;
System.out.println(a) // 10
change(a, b); // 实际参数
System.out.println(a) // 10
// 引用数据类型,形式参数到操作会影响实际参数
int[] array = (10 ,20 ,30); // 静态初始化一个数组
System.out.println(array[0]) // 10
System.out.println(array[1]) // 20
change(array)
System.out.println(array[0]) // 100
System.out.println(array[1]) // 200
}
// 定义一个方法,将参数扩大十倍
public static void change(int x, int y) { // x, y 形式参数
x *= 10;
y *= 10;
}
public static void change(int[] arr) {
arr[0] *= 10;
arr[1] *= 10;
}
}
- 数据结构——栈:先进后出;重载overload:多个方法 到名称相同,但参数列表不同。
1)参数的个数不同;2)参数的类型不同;3)参数到多类型顺序不同;4)重载中与返回值无关;5)重载中与参数名称无关。
public class Demo23MethodParam{
public static void main(String[] args){
System.out.println(sum(10, 20)) // 30, 谁匹配得上就运行谁
System.out.println(sum(10, 20, 30, 40)) // 都不适用就会报错
}
public staitc int sum(int a, int b) {
return a + b;
}
public staitc int sum(int a, int b, int c) {
return a + b + c;
}
}
面向对象
- 面向对象三大特征:封装性、继承性、多态性。
1)类:抽象的,如一张手机设计图;2)对象:具体的,如一个真正的手机实体。3)根据类创造对象,即实例化一个对象。
import java.utile.Arrays;
public class Demo01PrintArray{
public static void main(String[] args){
int[] array = {10, 20, 30, 40, 50};
// 面向过程的细节如下
for (int i = 0, i < array.length; i++) {
if (i == array.length = 1) {
System.out.println(array[i]);
} else {
System.out.println(array[i]);
}
}
// 面向对象到思想,达到如上效果
// JDK当中有Arrays工具,可将数组转换成指定格式到字符串
String str = Arrays.toString(array);
System.out.println(Str); // 结果相同
System.out.println(Array.toString(array)); // 结果相同
}
}
- 类是用来模拟现实事物的代码手段,事物分成属性、行为两个部分,类当中也对应包含如下两个部分:
【成员变量】属性,将变量位置直接定义在类中,在方法外;
【成员方法】行为,将普通的方法去掉static关键字。
/*
定义一个类用来模拟“学生”
成员变量(属性):
String name;
int age;
成员方法(行为):
public void eat() {}
public void sleep() {}
public void study() {}
*/
public class Student {
String name;
int age;
public void eat() {
System.out.println("吃饭");
}
}
- 对象-方法:
如何使用创建好的对象?
【格式】对象名.成员变量名
如何使用对象当中的成员方法?
【格式】对象名.成员方法名(参数)
public class Demo01Student {
public static void main(String[] args) {
Student stu1 = new Student();
Student stu2 = new Student();
Student stu = new Student();
System.out.println(stu.name); // null,如果没有赋值,将会有一个默认值
// 改变成员变量的数据值
stu.name = "Shane";
stu.age = 22;
System.out.println(stu.name); // Shane
// 将对象当中的成员变量,交给name变量
String name = stu.name;
System.out.println(name); // Shane
}
public void eat() {
System.out.println("吃饭");
}
// 使用对象当中的成员方法
stu.eat(); // 调用吃饭的成员方法,由于成员方法是void类型,所以要单独调用
}
- 如何使用定义好的类?类就是一种引用数据类型,使用起来可以分成三个步骤。
1)导包:如果需要使用的目标类和当前类(含有main方法)位于同一个包下,可以省略导包;否则需要使用import导包。
2)创建:类名称 对象名 = new 类名称();
3)使用:
/*
定义一个类,用来模拟手机事物
成员变量:
String brand;
String price;
String colour;
成员方法:
public void call(String who) { } // 打电话
public void sendMessage() { }
*/
public class Phone {
String brand;
String price;
String colour;
public void call(String who) {
System.out.println("给" + who + "打电话");
}
}
public class Demo01PhoneOne {
public static void main(String[] args) {
Phone one = new Phone();
System.out.println(one.brand); // null
one.brand = "iphone";
System.out.println(one.brand); // iphone
one.call("Jobes");
}
}