第一行代码 java-源代码-第2章【课程代码】java基础语法
第二章:Java基础语法
2.1 Java的注释
范例2-1,单行注释。
public class TestDemo {
public static void main(String[] args) {
// 此处为注释,编译代码时不编译
System.out.println("Hello MLDN .");
}
}
范例2-2,多行注释。
public class TestDemo {
public static void main(String[] args) {
/*
* 此处为多行注释,编译代码时不编译
* 如果要学习Java高端课程,可以登录,www.mldnjava.cn
*/
System.out.println("Hello MLDN .");
}
}
范例2-3,使用文档注释。
/**
* 此处为文档注释
* @author MLDN李兴华
*/
public class TestDemo {
public static void main(String[] args) {
System.out.println("Hello MLDN .");
}
}
第(1)页 共(19)页
范例2-4,利用中文定义标识符。 public class 你好 { // 类名称
public static void main(String args[]) {
int 年龄 = 20 ; // 变量名称
System.out.println(年龄) ; // 输出内容
}
}
2.3 数据类型划分
范例2-5,定义int型变量。
public class TestDemo {
public static void main(String args[]) {
// 为变量设置内容使用如下格式,数据类型 变量名称 = 常量 ;
int num = 10 ; // 10是常量,常量的默认类型是int
int result = num * 2 ; // 利用num变量的内容乘以2,并且将其赋值给result
System..println(result) ; // 输出result变量 out
}
}
范例2-6,观察变量与常量的区别。 public class TestDemo {
public static void main(String args[]) {
// 所有的变量名称在同一块代码中只允许声明一次
int num = 10 ; // 10是常量,常量的默认类型是int
// 取出num变量的内容乘以2,并且将其设置给num变量
num = num * 2 ;
System.out.println(num) ;
}
}
范例2-7,如果超过了int的最大值或最小值的结果。
public class TestDemo {
public static void main(String args[]) {
int max = Integer.MAX_VALUE ; // 取出最大值
int min = Integer.MIN_VALUE ; // 取出最小值
System.out.println(max) ; // 2147483647
System.out.println(min) ; // -2147483648
? // int变量 int型常量 = int型数据
System.out.println(max + 1) ; // 最大值加1,-2147483648
第(2)页 共(19)页
System.out.println(min - 1) ; // 最小值减1,2147483647
System.out.println(min - 2) ; // 最小值减2,2147483646
}
}
范例2-8,扩大数据类型。
public class TestDemo {
public static void main(String args[]) {
int max = Integer.MAX_VALUE; // 取出最大值
int min = Integer.MIN_VALUE; // 取出最小值
? // int变量 long型常量 = long型数据
System.out.println(max + 1L); // 最大值加1,2147483648
System.out.println(min - (long) 1); // 最小值减1,-2147483649
? // long变量 int型常量 = long型数据
System.out.println((long) min - 2); // 最小值减2,-2147483650
}
}
范例2-9,将范围大的数据类型变为范围小的数据类型。
public class TestDemo {
public static void main(String args[]) {
long num = 1000 ; // 1000常量是int型,使用long接受,发生了向大范围转型
int x = (int) num ; // 把long变为int
System.out.println(x) ;
}
}
范例2-10,观察发生溢出的转换问题。 public class TestDemo {
public static void main(String args[]) {
long num = 2147483650L ; // 该数据已经超过了int数据范围
int x = (int) num ; // 把long变为int
System.out.println(x) ;
}
}
范例2-11,观察byte转换。
public class TestDemo {
public static void main(String args[]) {
int num = 130 ; // 此范围超过了byte定义
byte x = (byte) num ; // 由int变为byte
System.out.println(x) ;
第(3)页 共(19)页
}
}
范例2-12,观察byte自动转型的操作。
public class TestDemo {
public static void main(String args[]) {
byte num = 100 ; // 100没有超过byte的保存范围
System.out.println(num) ; // 输出byte变量的内容
}
}
范例2-13,定义变量时不设置内容,使用变量前设置内容。
public class TestDemo {
public static void main(String args[]) {
int num; // 没有默认值
num = 0; // 在使用此变量之前设置内容
System.out.println(num);
}
}
范例2-14,定义小数。
public class TestDemo {
public static void main(String args[]) {
double num = 10.2 ; // 10.2是一个小数所以属于double型
// double型 * int型,转化为double,2.0, = double型
System.out.println(num * 2) ;
}
}
范例2-15,使用float型。
public class TestDemo {
public static void main(String args[]) {
float f1 = 10.2F ; // 小数都是double型,所以需要强制转换为float型
float f2 = (float)10.2 ; // 小数都是double型,所以需要强制转换为float型
System.out.println(f1 * f2) ; // float类型 * float类型 = float类型
}
}
范例2-16,关于除法的问题。
public class TestDemo {
第(4)页 共(19)页
public static void main(String args[]) {
int x = 9; // 声明整型变量
int y = 5; // 声明整型变量
? System.out.println(x / y); // int型 int型 = int型
}
}
范例2-17,解决除法计算精度。 public class TestDemo {
public static void main(String args[]) {
int x = 9; // 声明整型变量
int y = 5; // 声明整型变量
System.out.println(x / (double) y); // 将其中一个int类型变量转换为double类型
}
}
范例2-18,定义字符。
public class TestDemo {
public static void main(String args[]) {
char c = 'A' ; // 字符
int num = c ; // 字符可以和int型互相转换,以编码的形式出现,
System.out.println(c) ;
System.out.println(num) ;
}
}
范例2-19,实现字母大小写转换 public class TestDemo {
public static void main(String args[]) {
char c = 'A'; // 大写字母
int num = c; // 需要将字符变为int型才可以使用加法计算
num = num + 32; // 变为小写字母的编码
c = (char) num; // 将int变为char型
System.out.println(c);
}
}
范例2-20,利用字符变量保存中文。 public class TestDemo {
public static void main(String args[]) {
char c = '王'; // 是大写字母
int num = c; // 需要将字符变为int型才可以使用加法计算
第(5)页 共(19)页
System.out.println(num);
}
}
范例2-21,观察boolean。
public class TestDemo {
public static void main(String args[]) {
boolean flag = false ; // 布尔只有两种取值,true、false
if (!flag) { // if(布尔值) {满足条件的操作}
System.out.println("Hello World .") ;
}
}
}
范例2-22,观察String操作
public class TestDemo {
public static void main(String args[]) {
String str = "Hello World !"; // 字符串变量
System.out.println(str); // 输出字符串变量
System.out.println("Hello World !"); // 输出字符串常量
}
}
范例2-23,字符串连接。
public class TestDemo {
public static void main(String args[]) {
String str = "Hello";
str = str + " World "; // 字符串连接
str += "!!!"; // 字符串连接
System.out.println(str);
}
}
范例2-24,字符串连接与加法操作一起出现。
public class TestDemo {
public static void main(String args[]) {
int numA = 100; // int型变量
double numB = 99.0; // int型变量
String str = "加法计算," + numA + numB; // String型变量
System.out.println(str);
}
}
第(6)页 共(19)页
范例2-25,改变运算优先级。 public class TestDemo {
public static void main(String args[]) {
int numA = 100; // int型变量
double numB = 99.0; // int型变量
String str = "加法计算," + (numA + numB); // String型变量
System.out.println(str);
}
}
范例2-26,转义字符。
public class TestDemo {
public static void main(String args[]) {
String str = "Hello \"World\" \n\tHello MLDN";
System..println(str); out
}
}
2.4 运算符
基本运算符 范例2-27,不建议使用的代码。 public class TestDemo {
public static void main(String args[]) {
int numA = 10 ;
int numB = 20 ;
// 如此复杂的代码,一定会大量损害你的脑细胞
int result = numA * 2 - --numB * numA ++ + numB - numA -- + numB ;
System.out.println(result) ;
}
}
范例2-28,使用关系运算符。
public class TestDemo {
public static void main(String args[]) {
System.out.println("3 > 1 = " + (3 > 1)); // 使用大于号
System.out.println("3 < 1 = " + (3 < 1)); // 使用小于号
System.out.println("3 >= 1 = " + (3 >= 1)); // 使用大于等于号
System.out.println("3 <= 1 = " + (3 <= 1)); // 使用小于等于号
System.out.println("3 == 1 = " + (3 == 1)); // 使用等于号
System.out.println("3 != 1 = " + (3 != 1)); // 使用不等于号
}
第(7)页 共(19)页
}
范例2-29,四则运算。
public class TestDemo {
public static void main(String args[]) {
int numA = 10;
int numB = 20;
System.out.println("加法计算," + (numA + numB));
System.out.println("减法计算," + (numA - numB));
System.out.println("乘法计算," + (numA * numB));
System.out.println("除法计算," + (numA / (double) numB));
}
}
范例2-30,求模计算。
public class TestDemo {
public static void main(String args[]) {
int numA = 10;
int numB = 3;
System.out.println(numA % numB);
}
}
范例2-31,奇、偶数判断方法。 public class TestDemo {
public static void main(String args[]) {
int numA = 10;
int numB = 3;
System.out.println(numA % 2);
System.out.println(numB % 2);
}
}
范例2-32,判断某一个数字是奇数还是偶数。
public class TestDemo {
public static void main(String args[]) {
int num = 10; // 声明变量保存数字
if (num % 2 == 0) { // 判断该数字是奇数还是偶数
System.out.println(num + "是偶数。");
} else {
System.out.println(num + "是奇数。");
}
第(8)页 共(19)页
}
}
范例2-33,使用简化运算符。 public class TestDemo {
public static void main(String args[]) {
int num = 10;
num *= 2; // 等价,num = num * 2 ;
System.out.println(num);
}
}
范例2-34,观察自增。
public class TestDemo {
public static void main(String args[]) {
int numA = 10; // 定义整型变量
int numB = 20; // 定义整型变量
“” // ++写在变量前面,
示先对numA的变量内容加1
// 使用处理后的numA变量的内容 + numB变量的内容
int result = ++numA + numB;
System.out.println("numA = " + numA);
System.out.println("result = " + result);
}
}
范例2-35,观察自增。
public class TestDemo {
public static void main(String args[]) {
int numA = 10; // 定义整型变量
int numB = 20; // 定义整型变量
“” // ++写在后面,表示先使用numA的内容进行加法计算
// 加法计算完成之后在对numA的内容进行自增
int result = numA++ + numB;
System.out.println("numA = " + numA);
System.out.println("result = " + result);
}
}
范例2-36,实现赋值。
public class TestDemo {
public static void main(String args[]) {
int numA = 10; // 定义int型变量
第(9)页 共(19)页
int numB = 20; // 定义int型变量
// 如果numA大于numB,返回true,则将numA的内容赋值给max
// 如果numA小于numB,返回false,则将numB的内容赋值给max
int max = numA > numB ? numA : numB;
System.out.println(max);
}
}
范例2-37,利用判断语句实现三目运算的功能。
public class TestDemo {
public static void main(String args[]) {
int numA = 10; // 定义int型变量
int numB = 20; // 定义int型变量
int max = 0 ;
// 用if语句替代,int max = numA > numB ? numA : numB;
if (numA > numB) { // 如果numA的内容大于numB
max = numA ; // max变量的内容为numA的内容
} else { // 如果numA的内容小于numB
max = numB ; // max变量的内容为numB的内容
}
System..println(max); out
}
}
范例2-38,非就是针对布尔结果进行求反。 public class TestDemo {
public static void main(String args[]) {
boolean flag = true; // 定义布尔型变量
System.out.println(!flag); // 对变量结果进行非操作
}
}
范例2-39,观察普通与“&”。 public class TestDemo {
public static void main(String args[]) {
if ((1 == 2) & (10 / 0 == 0)) { // 使用普通与判断多个条件
System.out.println("Hello World !");
}
}
}
第(10)页 共(19)页
范例2-40,使用短路与(&&)。
public class TestDemo {
public static void main(String args[]) {
if ((1 == 2) && (10 / 0 == 0)) {
System.out.println("Hello World !");
}
}
}
范例2-41,观察普通或操作(|)。
public class TestDemo {
public static void main(String args[]) {
if ((1 == 1) | (10 / 0 == 0)) {
System.out.println("Hello World !");
}
}
}
范例2-42,观察短路或操作(||)。
public class TestDemo {
public static void main(String args[]) {
if ((1 == 1) || (10 / 0 == 0)) {
System.out.println("Hello World !") ;
}
}
}
范例2-42,实现位与操作。
public class TestDemo {
public static void main(String args[]) {
int numA = 9; // 定义整型变量
int numB = 11; // 定义整型变量
System.out.println(numA & numB); // 位与操作
}
}
范例2-43,向左边移位2位实现功能。 public class TestDemo {
public static void main(String args[]) {
int x = 2;
第(11)页 共(19)页
System.out.println(x << 2);// 向左移2位
}
}
2.5 程序逻辑控制
范例2-44,使用if语句进行判断。
public class TestDemo {
public static void main(String args[]) {
double score = 90.0; // 定义变量
if (score > 60.0) { // 设置判断条件
System.out.println("及格了,");
}
}
}
范例2-45,使用if语句进行判断。 public class TestDemo {
public static void main(String args[]) {
double score = 90.0; // 定义变量
if (score > 60.0) // 设置判断条件
System.out.println("及格了,");
}
}
范例2-46,使用if…else判断。
public class TestDemo {
public static void main(String args[]) {
double score = 30.0; // 定义变量
if (score > 60.0) { // 条件判断满足
System.out.println("及格了,");
} else { // 条件判断不满足
System.out.println("小白的成绩,");
}
}
}
范例2-47,使用if…else if…else判断。
public class TestDemo {
public static void main(String args[]) {
double score = 91.0; // 定义变量
第(12)页 共(19)页
if (score < 60.0) { // 条件判断
System.out.println("小白的成绩,") ;
} else if (score >= 60 && score <= 90) { // 条件判断
System.out.println("中等成绩") ;
} else if (score > 90 && score <= 100) { // 条件判断
System.out.println("优秀成绩") ;
} else { // 条件判断都不满足
System.out.println("你家的考试成绩这么怪异,") ;
}
}
}
范例2-48,使用switch判断。
public class TestDemo {
public static void main(String args[]) {
int ch = 1;
switch (ch) { // 判断的是数字
case 2: { // 判断内容是否是2
System.out.println("内容是2");
break;
}
case 1: { // 判断内容是否是1
System.out.println("内容是1");
break;
}
case 3: { // 判断内容是否是3
System.out.println("内容是3");
break;
}
default: { // 判断都不满足
System.out.println("没有匹配内容");
break;
}
}
}
}
范例2-49,不加入break时的操作。 public class TestDemo {
public static void main(String args[]) {
int ch = 1;
switch (ch) { // 判断的是数字
case 2: { // 判断内容是否是2
System.out.println("内容是2");
}
case 1: { // 判断内容是否是1
第(13)页 共(19)页
System.out.println("内容是1");
}
case 3: { // 判断内容是否是3
System.out.println("内容是3");
}
default: { // 判断都不满足
System..println("没有匹配内容"); out
}
}
}
}
范例2-50,使用字符串判断。
public class TestDemo {
public static void main(String args[]) {
String str = "HELLO";
switch (str) { // 判断的是字符串
case "HELLO": {
System.out.println("内容是HELLO");
break;
}
case "hello": {
System.out.println("内容是hello");
break;
}
case "mldn": {
System.out.println("内容是mldn");
break;
}
default: {
System.out.println("没有匹配内容");
break;
}
}
}
}
范例2-51,实现1,100的累加——使用while循环。
public class TestDemo {
public static void main(String args[]) {
int sum = 0; // 保存总和
int current = 1; // 循环的初始化条件
while (current <= 100) { // 循环结束条件
sum += current; // 累加
current++; // 改变循环条件
}
第(14)页 共(19)页
System.out.println(sum);
}
}
范例2-52,使用do…while循环。
public class TestDemo {
public static void main(String args[]) {
int sum = 0; // 保存总和
int current = 1; // 循环的初始化条件
do { // 循环结束条件
sum += current; // 累加
current++; // 改变循环条件
} while (current <= 100); // 循环结束判断
System.out.println(sum);
}
}
范例2-53,使用for循环实现1,100累加。
public class TestDemo {
public static void main(String args[]) {
int sum = 0; // 保存总和
// 设置循环初始化条件current,同时此变量作为累加操作使用
// 每次执行循环体前都要进行循环判断,current <= 100,
// 循环体执行完毕后会自动执行“current++”改变循环条件
for (int current = 1; current <= 100; current++) {
sum += current; // 循环体中实现累加操作
}
System.out.println(sum);
}
}
范例2-54,另一种for循环写法。 public class TestDemo {
public static void main(String args[]) {
int sum = 0; // 保存累加的结果
int current = 1; // 初始值
for (; current <= 100;) { // for循环
sum += current; // 累加计算
current ++; // 循环条件修改
}
System.out.println(sum);
}
}
第(15)页 共(19)页
范例2-55,输出乘法口诀表。 public class TestDemo {
public static void main(String args[]) {
for (int x = 1; x <= 9; x++) { // 控制循环的行数
for (int y = 1; y <= x; y++) { // 控制列数
System..print(x + "*" + y + "=" + (x * y) + "\t"); out
}
System.out.println(); // 换行
}
}
}
范例2-56,观察continue。 public class TestDemo {
public static void main(String args[]) {
for (int x = 0; x < 10; x++) {
if (x == 3) {
continue; // 之后的代码不执行,直接结束本次循环
}
System.out.print("x = " + x + "、");
}
}
}
范例2-57,观察break。 public class TestDemo {
public static void main(String args[]) {
for (int x = 0; x < 10; x++) {
if (x == 3) {
break; // 退出整个循环
}
System.out.print("x = " + x + "、");
}
}
}
2.6 方法的定义及使用
范例2-58,定义一个没有参数没有返回值的方法。
public class TestDemo {
public static void main(String args[]) {
第(16)页 共(19)页
printInfo(); // 直接调用方法
printInfo(); // 直接调用方法
}
/**
* 信息输出操作
*/
public static void printInfo() { // 定义没有参数,没有返回值的方法
System.out.println("*********************");
System.out.println("* www.yootk.com *");
System.out.println("*********************");
}
}
范例2-59,定义一个有参数无返回值的方法 public class TestDemo {
public static void main(String args[]) {
pay(10.0); // 调用方法
pay(-10.0); // 调用方法
}
/**
* 定义一个支付的操作方法,如果支付金额大于0则正常支付,否则会输出错误提示信息
* @param money 要支付的金额
*/
public static void pay(double money) { // 购买支付操作
if (money > 0.0) { // 现在已经给钱
System.out.println("可以进行支付,");
} else { // 不能够支付
System.out.println("你穷疯了,没钱还买东西,");
}
}
}
范例2-60,定义有返回值有参数的方法。 public class TestDemo {
public static void main(String args[]) {
int result = add(10, 20); // 方法的返回值可以进行接收
System.out.println("计算结果," + result);
System.out.println("计算结果," + add(50, 60)); // 也可以直接将方法返回值进行输出
}
/**
* 实现数据的加法操作
* @param x 操作数字一
* @param y 操作数字二
* @return 返回两个数字的加法计算结果
*/
public static int add(int x, int y) { // 有参数有返回值的方法
第(17)页 共(19)页
return x + y; // 返回加法计算结果
}
}
范例2-61,利用return结束方法调用。 Public class TestDemo {
public static void main(String args[]) {
set(100); // 正常执行输出
set(3); // 满足方法判断条件,会中断输出操作
set(10); // 正常执行输出
}
/**
* 定义一个设置数据的操作方法,如果该数据为3将无法设置
* @param x 要设置的数据内容
*/
public static void set(int x) { // 方法声明为void
if (x == 3) { // 判断语句
return; // 方法后面的内容不执行了
}
System.out.println("x = " + x);
}
}
范例2-62,观察方法重载。
public class TestDemo {
public static void main(String args[]) {
// 方法重载之后执行语句时会根据传入参数的类型或个数的不同调用不同的方法体
System.out.println("两个整型参数," + add(10, 20));
System.out.println("三个整型参数," + add(10, 20, 30));
System.out.println("两个浮点型参数," + add(10.2, 20.3));
}
/**
* 实现两个整型数字的加法计算操作
* @param x 操作数字一
* @param y 操作数字二
* @return 两个整型数据的加法计算结果
*/
public static int add(int x, int y) { // add()方法一共被重载三次
return x + y;
}
/**
* 实现三个整型数字的加法计算操作
* @param x 操作数字一
* @param y 操作数字二
* @param z 操作数字三
* @return 三个整型数据的加法计算结果
第(18)页 共(19)页
*/
public static int add(int x, int y, int z) { // 与之前的add()方法的参数个数不一样
return x + y + z;
}
/**
* 实现两个小数的加法计算操作
* @param x 操作数字一
* @param y 操作数字二
* @return 两个小数的加法计算结果
*/
public static double add(double x, double y) { // 与之前的add()方法的参数类型不一样
return x + y;
}
}
范例2-63,输出信息。
public class TestDemo {
public static void main(String args[]) {
System.out.println("hello"); // 输出String
System.out.println(1); // 输出int
System..println(10.2); // 输出double out
System..println('A'); // 输出char out
System.out.println(false); // 输出boolean
}
}
范例2-63,递归调用。
public class TestDemo {
public static void main(String args[]) {
System.out.println(sum(100)); // 1 - 100累加
}
/**
* 数据的累加操作,传入一个数据累加操作的最大值,而后每次进行数据的递减,一直累加到计算数据为1
* @param num 要进行累加的操作
* @return 数据的累加结果
*/
public static int sum(int num) { // 最大的内容
if (num == 1) { // 递归的结束调用
return 1; // 最终的结果返回了1
}
return num + sum(num - 1); // 递归调用
}
}
第(19)页 共(19)页