第 2 章 java 编程基础
2.1 Java 的基本语法
2.1.1 Java 代码的基本格式
修饰符 class 类名{
程序代码
}
注意点:
- Java 是严格区分大小写的
- 每条功能执行语句后都要用分号结束(与 C 语言类似)
- Java 程序中一个连续的字符串不能分开在两行中书写,但可以分开后用 + 连接
public class HelloWorld { public static void main(String[] args) { System.out.println("HelloWorld!"); } }
2.1.2 Java 中的注释
- 单行注释
int x = 1; //定义一个整型变量x
- 多行注释
/* int x = 1; int y = 2; */
- 文档注释
/** * Person实体类 */
2.1.3 Java 中的标识符
可以由字母、数字、美元符号和下划线组成,但不能以数字开头,也不可以使用关键字
命名规范:
- 类名和接口名首字母一律大写
- 包名一律小写
- 常量名一律大写
- 变量名和方法名第一个单词首字母小写,从第二个开始每个单词首字母大写
2.2.3 变量的类型转换
强制类型转换
package com.itheima.example; public class Example01 { public static void main(String[] args) { int i = 5; byte b = (byte) i; System.out.println(b); } }
2.5 选择语句结构
2.5.1 if 条件语句
package com.itheima.example; public class Example02 { public static void main(String[] args) { int month=1; if (month>=3 && month <= 5){ System.out.println("春季"); }else if (month>=6 && month <= 8){ System.out.println("夏季"); }else if (month>=9 && month <= 11){ System.out.println("秋季"); }else{ System.out.println("冬季"); } } }
2.5.2 switch 语句
package com.itheima.example; public class Example03 { public static void main(String[] args) { String country = "中国"; switch (country) { case "中国": System.out.println("你好: "+country); break; case "美国": System.out.println("hello: "+country); break; default: System.out.println("输入错误"); break; } } }
2.6 循环语句
2.6.1 while 循环语句
package com.itheima.example; public class Example04 { public static void main(String[] args) { int i=1; int sum=0; while (i <= 10) { sum += i; i++; } System.out.println("sum="+sum); } }
2.6.2 do-while 语句
package com.itheima.example; public class Example05 { public static void main(String[] args) { int i=1,sum=0; do { sum+=i; i++; } while (i<=10); System.out.println("sum="+sum); } }
注意区别:
while 和 do-while 的区别:
while:先判断再执行
do-while:先执行一次再判断
2.6.3 for 循环语句
package com.itheima.example; public class Example06 { public static void main(String[] args) { int sum=0; for (int i = 0; i <=10;i++){ sum += i; } System.out.println("sum="+sum); } }
2.6.4 循环嵌套
使用循环嵌套用#输出直角三角形
package com.itheima.example; public class Example07 { public static void main(String[] args) { for (int i = 1; i <=8;i++){ for (int j = 1; j <= i; j++) { System.out.print("#"); } System.out.print("\n"); } } }
此处有一个问题,在 java 中
System.out.print
System.out.println
这两个是不一样的,下面一个输出后进行一次换行
2.6.5 跳转语句
1.break 语句
出现在 switch 中时是终止某个 case 并向下执行
出现在循环中时是跳出当前循环,执行后面代码
package com.itheima.example; public class Example08 { public static void main(String[] args) { int x=1; while (x <=5){ System.out.println("x="+x); if (x==4){ break; } x++; } } }
2.continue 语句
终止本次循环,执行下一次循环
package com.itheima.example; /** * 对1-100之内的偶数进行求和 */ public class Example09 { public static void main(String[] args) { int sum=0; for (int i = 1; i <=100;i++){ if (i%2!=0){ continue; } sum+=i; } System.out.println("sum="+sum); } }
2.7 方法(其实就是函数)
2.7.1 方法的概念
package com.itheima.example; public class Example10 { public static void main(String[] args) { int area = getArea(4,5); System.out.println("矩形的面积为:"+area); } private static int getArea(int i, int j) { return i*j; } }
2.7.2 方法的重载
package com.itheima.example; public class Example11 { public static void main(String[] args) { int sum1=add(2,3); int sum2=add(2,3,4); double sum3 = add(2.5,3.5); System.out.println("sum1="+sum1); System.out.println("sum2="+sum2); System.out.println("sum3="+sum3); } private static double add(double d, double e) { return d+e; } private static int add(int i, int j, int k) { return i+j+k; } private static int add(int i, int j) { return i+j; } }
注意:方法的重载与返回值类型无关,只需要满足两个条件:1.方法名相同。2、参数个数或参数类型不相同
2.8 数组
2.8.1 数组的定义
int[] x= new int[10];
package com.itheima.example; public class Example12 { public static void main(String[] args) { int []arr=new int[4]; arr[0]=2; arr[1]=3; System.out.println("arr[0]="+arr[0]); System.out.println("arr[1]="+arr[1]); System.out.println("arr[2]="+arr[2]); System.out.println("arr[3]="+arr[3]); System.out.println("数组的长度是:"+arr.length); } }
2.8.2 数组的常见操作
1.数组遍历
package com.itheima.example; public class Example13 { public static void main(String[] args) { int[] arr = {3,5,1,2,6}; for (int i = 0; i < arr.length; i++){ System.out.println(arr[i]); } } }
2.数组最值
package com.itheima.example; public class Example14 { public static void main(String[] args) { int[] arr = {3,5,1,2,6}; int max=arr[0]; for (int i = 0; i < arr.length; i++){ if(arr[i]>max){ max = arr[i]; } } System.out.println("max="+max); } }
3.数组排序
package com.itheima.example; public class Example15 { public static void main(String[] args) { int[] arr = {4,8,3,9,7,5}; System.out.print("排序前:"); printArray(arr); bubbleSort(arr); System.out.print("排序后:"); printArray(arr); } private static void bubbleSort(int[] arr) { for (int i = 0; i < arr.length-1; i++){ for (int j = 0; j < arr.length-1-i; j++){ if(arr[j]>arr[j+1]){ int temp = arr[j]; arr[j] = arr[j + 1]; arr[j+1]=temp; } } System.out.print("第"+(i+1)+"轮排序后:"); printArray(arr); } } private static void printArray(int[] arr) { for (int i = 0; i < arr.length; i++){ System.out.print(arr[i]+" "); } System.out.print("\n"); } }
2.8.3 Arrays 工具类
Arrays 是一个专门操作数组的工具类,位于 java.util 包中
方法声明 | 功能描述 |
---|---|
static void sort(int[] a) | 对指定的 int 兴数组按数字升序进行排序 |
static int binarySearch(Object[] a, Object key) | 使用二分搜索法搜索指定数组,以获得指定对象 |
static int[] copyOfRange(int[] original,int from,int to) | 将指定数组的指定范围复制到一个新数组 |
static void fill(Object[] a,Object val) | 将指定的 Object 引用分配给指定 Object 数组的每个元素 |
static String toString(int[] arr) | 返回指定数组内容的字符串表示形式 |
package com.itheima.example; import java.util.Arrays; public class Example16 { public static void main(String[] args) { int[] arr={9,8,3,5,2}; int[] copied=Arrays.copyOfRange(arr, 1, 7); System.out.println("复制数组:"+Arrays.toString(copied)); Arrays.sort(arr); System.out.println("排序后:"+Arrays.toString(arr)); Arrays.fill(arr, 8); System.out.println("填充数组:"+Arrays.toString(arr)); } }
2.9 String 类和 StringBuffer 类
2.9.1 String 类
直接初始化:
String str1="abc";
使用构造方法:
String str1=new String(); String str2=new String)("abc");
方法声明 | 功能描述 |
---|---|
int indexOf(int ch) | 返回指定字符在此字符串中第一次出现处的索引 |
int indexOf(String str) | 返回指定子字符串在此字符串中第一次出现处的索引 |
char charAt(int index) | 返回字符串中 index 位置上的字符,其中 index 的取值范围是 0~ 字符串长度一 1 |
boolean ends With(String suffix) | 判断此字符串是否以指定的字符串结尾 |
int length | 返回此字符串的长度 |
boolean equals(Object anObject) | 将此字符串与指定的字符串比较,如果相等则返回 true,否则返回 false |
boolean isEmpty( | 当且仅当字符串长度为 0 时返回 true |
boolean startsWith(String prefix) | 判断此字符串是否以指定的字符串开始 |
boolean contains(CharSequence cs) | 判断此字符串中是否包含指定的字符序列 |
String toLowerCase( | 使用默认语言环境的规则将 String 中的所有字符转换为小写 |
String toUpperCase( | 使用默认语言环境的规则将 String 中的所有字符转换为大写 |
char[] toCharArray | 将此字符串转换为一个字符数组 |
String replace ( CharSequence oldstr,CharSequence newstr) | 返回一个新的字符串,它是通过利用 newstr 替换此字符串中出现的所有 oldstr 得到的 |
String[] split(String regex) | 根据参数 regex 将原来的字符串分割为若干个子字符串 |
String substring(int beginIndex) | 返回一个新字符串,它包含从指定的 beginIndex 处开始,直到此字符串末尾的所有字符 |
String substring ( int beginIndex, int endIndex) | 返回一个新字符串,它包含从指定的 beginIndex 处开始,直到索引 endIndex 一 1 处的所有字符 |
String trim | 返回一个新字符串,它去除了原字符串首尾的空格 |
package com.itheima.example; public class Example17 { public static void main(String[] args) { String s="ababcAbc,dedcba"; System.out.println("e第一次出现在:"+s.indexOf("e")); System.out.println("bc第一次出现在:"+s.indexOf("bc")); System.out.println("第8个位置上是:"+s.charAt(8)); System.out.println("字符串是否以a结尾:"+s.endsWith("a")); System.out.println("字符串是否以z结尾:"+s.endsWith("z")); System.out.println("字符串长度为:"+s.length()); System.out.println("字符串长度为0?:"+s.isEmpty()); System.out.println("字符串是否以a开始:"+s.startsWith("a")); System.out.println("字符串是否以z开始:"+s.startsWith("z")); System.out.println("字符串是否包含了ded:"+s.contains("ded")); System.out.println("转换为小写:"+s.toLowerCase()); System.out.println("转换为大写:"+s.toUpperCase()); System.out.println("将a全部替换为z:"+s.replace("a", "z")); System.out.println("从第6个字符到结束:"+s.substring(6)); System.out.println("从第5个字符到第10个字符:"+s.substring(5,10)); System.out.println("返回一个去除空格的字符串:"+s.trim()); } }
2.9.2 StringBuffer 类
方法声明 | 功能描述 |
---|---|
StringBuffer append(char c) | 添加参数到 StringBuffer 对象中 |
StringBuffer insert(int offset,String str) | 在字符串中的 offset 位置插入字符串 str |
StringBuffer delete(intstart,int end) | 删除 StringBuffer 对象中指定范围的字符或字符串序列 |
StringBuffer deleteCharAt(int index) | 移除此序列指定位置的字符 |
StringBuffer replace(int start, int end,String s) | 在 StringBuffer 对象中替换指定的字符或字符串序列 |
void setCharAt(int index,char ch) | 修改指定位置 index 处的字符序列 |
StringBuffer reverseO | 将此字符序列用其反转形式取代 |
String toString( | 返回 StringBuffer 缓冲区中的字符串 |
package com.itheima.example; public class Example18 { public static void main(String[] args) { System.out.println("**********1.添加**********"); add(); System.out.println("**********2.删除**********"); remove(); System.out.println("**********3.修改**********"); alert(); } public static void alert() { StringBuffer sb = new StringBuffer("abcdefg"); sb.setCharAt(2, 'h'); System.out.println("修改指定位置字符结果:"+sb); sb.replace(1, 3, "yy"); System.out.println("修改指定位置字符(串)结果:"+sb); System.out.println("字符串翻转结果:"+sb.reverse()); } public static void remove() { StringBuffer sb = new StringBuffer("abcdefg"); sb.delete(2, 4); System.out.println("删除指定位置结果:"+sb); sb.deleteCharAt(3); System.out.println("删除指定位置结果:"+sb); sb.delete(0,sb.length()); System.out.println("清空缓冲区结果:"+sb); } public static void add() { StringBuffer sb = new StringBuffer(); sb.append("abcdefg"); System.out.println("append添加结果:"+sb); sb.insert(3, "321"); System.out.println("insert结果:"+sb); } }
2.10 包装类
基本数据类型 | 对应的包装类 | 基本数据类型 | 对应的包装类 |
---|---|---|---|
char | Character | long | Long |
byte | Byte | float | Float |
int | Integer | double | Double |
short | Short | boolean | Boolean |
装箱:指将基本数据类型的值转为引用数据类型
拆箱:将引用数据类型的对象转为基本数据类型
END~~~~
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于