Java是当今世界最重要、使用最广泛的计算机语言之一,全球每天有超百万的开发者在用Java进行开发。
1 Java概述
Java概述
Java SE,平台标准版,提供了基础。
Java EE,平台企业版,基础上构建。
Java ME,平台微型版,移动和嵌入式设备。
Java各种集成开发工具 > JDK开发工具包 > JRE运行时类库 > JVM(Java虚拟机)。
1-1 注释 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 public class Test001 { public static void main (String[] args) { System.out.println("注释" ); } }
1-2 常量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 public class Test002 { public static final double PI = 3.14 ; static final double X = 3.12 ; final double Y = 3.13 ; public static void main (String[] args) { final double Z = 3.11 ; System.out.println(Z); System.out.println(Test002.X); Test002 myObject = new Test002 (); double value = myObject.Y; System.out.println(value); System.out.println(Test002.PI); } }
1-3 变量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public class Test003 { public static void main (String[] args) { String username1 = "刘一" ; String username2; username2 = "陈二" ; System.out.println(username1); System.out.println(username2); String username3, username4, username5; username3="张三" ; username4="李四" ; username5="王五" ; String username6="赵六" , username7="孙七" ; System.out.println(username3); System.out.println(username4); System.out.println(username5); System.out.println(username6); System.out.println(username7); } }
(1) 成员变量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class Test004 { String name1="刘一" ; static final String name2="陈二" ; public static void main (String[] args) { Test004 myClass=new Test004 (); System.out.println(myClass.name1); System.out.println(Test004.name2); } }
(2) 局部变量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 public class Test005 { public static void testFun (int c) { System.out.println("c=" + c); } public static void testExcept () { try { System.out.println("Exception!" ); } catch (Exception e) { e.printStackTrace(); } } public static void main (String[] args) { int a = 7 ; if (a > 5 ) { int b = 3 ; System.out.println("b=" + b); System.out.println("a=" + a); } System.out.println("a=" + a); testFun(9 ); testExcept(); } }
2 数据类型 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 public class Test006 { public static void main (String[] args) { byte a = 20 ; short b = 10 ; int c = 30 ; long d = 40 ; long sum = a + b + c + d; System.out.println(sum); char e = 'X' ; char f = '@' ; char g = '9' ; System.out.println(e + f + g); double h = 111.1d ; float i = 222.2f ; float total = (float ) (h + i); System.out.println(total); boolean j = false ; System.out.println(j); } }
3 运算符操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 public class Test007 { public static void main (String[] args) { int a = 1 ; System.out.println("a++=" + (a++)); System.out.println("++a=" + (++a)); System.out.println("a--=" + (a--)); System.out.println("--a=" + (--a)); System.out.println("-a=" + (-a)); int b = 9 , c = 2 , d = 3 ; int e = b * (c + d) % c - b; System.out.println("e=" + e); a += b; System.out.println("a=" + a); System.out.println(a > 9 && a <= 11 ); System.out.println(a != b); System.out.println(c <= d); System.out.println("~:" + (~10010 )); System.out.println("|:" + (100100 | 101000 )); System.out.println("&:" + (1001000 & 1010000 )); System.out.println("^:" + (10010000 ^ 10100000 )); System.out.println(a &= 7 ); System.out.println(a |= 5 ); System.out.println(a ^= 3 ); System.out.println(a -= 1 ); System.out.println(a >>= 1 ); System.out.println(a <<= 1 ); int x = 1 , y = 2 , z = 3 ; System.out.println(z > x ? y : z); } }
4 流程的控制
流程的控制
顺序结构:表达式语句、空语句、复合语句。
选择结构:if语句、if…else语句、switch语句。
循环结构:while语句、for语句、foreach语句(for循环的变形)。
4-1 选择结构 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 import java.util.Calendar;public class Test008 { public static void main (String[] args) { String today = "周六" ; String weather = "晴" ; if (today.equals("周六" )) { if (weather.equals("晴" )) { System.out.println("户外出行" ); } else { System.out.println("室内游戏" ); } } else if (today.equals("周日" )) { System.out.println("室内看剧" ); } else { System.out.println("公司上班" ); } String weekDate = "" ; Calendar calendar = Calendar.getInstance(); int week = calendar.get(Calendar.DAY_OF_WEEK) - 1 ; switch ((week)) { case 0 : weekDate = "周日" ; break ; case 1 : weekDate = "周一" ; break ; case 2 : weekDate = "周二" ; break ; case 3 : weekDate = "周三" ; break ; case 4 : weekDate = "周四" ; break ; case 5 : weekDate = "周五" ; break ; case 6 : weekDate = "周六" ; break ; default : break ; } System.out.println("今天" + weekDate); } }
4-2 循环结构 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class Test009 { public static void main (String[] args) { int a = 1 , b = 1 ; while (a <= 5 ) { b = b * a; a++; } System.out.println("5的阶乘结果:" + b); int c = 1 , d = 1 ; do { d *= c; c++; } while (c <= 5 ); System.out.println("5的阶乘结果:" + d); int e = 1 , f = 1 ; for (; e <= 5 ; e++) { f *= e; } System.out.println("5的阶乘结果:" + f); } }
(1) for循环嵌套 1 2 3 4 5 6 7 8 9 10 11 12 public class Test010 { public static void main (String[] args) { System.out.println("九九乘法表" ); for (int x = 1 ; x <= 9 ; x++) { for (int y = 1 ; y <= x; y++) { System.out.print(y + "*" + x + "=" + y * x + "\t" ); } System.out.println(); } } }
(2) foreach语句 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class Test011 { public static void main (String[] args) { int [] numbers = { 43 , 32 , 53 , 54 , 75 , 73 , 10 }; System.out.println("*****for*****" ); for (int x = 0 ; x < numbers.length; x++) { System.out.println("Count is: " + numbers[x]); } System.out.println("***foreach***" ); for (int item : numbers) { System.out.println("Count is: " + item); } } }
4-3 break语句 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class Test012 { public static void main (String[] args) { for (int x = 0 ; x < 5 ; x++) { System.out.print("外" + (x + 1 ) + ":" ); for (int y = 0 ; y < 10 ; y++) { if (y == 3 ) { break ; } System.out.print("内" + (y + 1 )); } System.out.println(); } label: for (int i = 0 ; i < 10 ; i++) { for (int j = 0 ; j < 10 ; j++) { System.out.println(j); if (j % 2 != 0 ) { break label; } } } } }
4-4 return语句 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import java.util.Scanner;public class Test013 { public static double sum (double i, double j) { double sum = i + j; return sum; } public static void main (String[] args) { Scanner input = new Scanner (System.in); System.out.print("请输入操作数(1):" ); double num1 = input.nextDouble(); System.out.print("请输入操作数(2):" ); double num2 = input.nextDouble(); double s = sum(num1, num2); System.out.println(num1 + " + " + num2 + " = " + s); } }
4-5 continue语句 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public class Test014 { public static void main (String[] args) { int [] num = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }; for (int i = 0 ; i < num.length; i++) { if (i == 3 ) { continue ; } System.out.println("Count is:" + i); } label: for (int x = 0 ; x < 3 ; x++) { for (int y = 3 ; y > 0 ; y--) { if (y == x) { continue label; } System.out.println(x + ", " + y); } } System.out.println("Game Over!" ); } }
4-6 打印杨辉三角 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 import java.util.Scanner;public class Test015 { public static int num (int x, int y) { if (y == 1 || y == x) { return 1 ; } int c = num(x - 1 , y - 1 ) + num(x - 1 , y); return c; } public static void calculate (int row) { for (int i = 1 ; i <= row; i++) { for (int j = 1 ; j <= row - i; j++) { System.out.print(" " ); } for (int j = 1 ; j <= i; j++) { System.out.print(num(i, j) + " " ); } System.out.println(); } } public static void main (String[] args) { Scanner scan = new Scanner (System.in); System.out.print("打印行数:" ); int row = scan.nextInt(); calculate(row); } }
5 字符串处理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 public class Test016 { public static void main (String[] args) { String str1 = "Hi" ; System.out.println(str1); String str2 = new String ("Aha" ); System.out.println(str2); char a[] = { 'W' , 'o' , 'w' }; String str3 = new String (a); System.out.println(str3); String str4 = new String (a, 0 , 3 ); System.out.println(str4); String str5 = "123" ; int n = 0 ; n = Integer.parseInt(str5); System.out.println(n); n = Integer.valueOf(str5).intValue(); System.out.println(n); int num = 345 ; String str6 = String.valueOf(num); System.out.println(str6); String str7 = Integer.toString(num); System.out.println(str7); String str8 = num + "" ; System.out.println(str8); } }
5-1 拼接 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 public class Test017 { public static void main (String[] args) { String str1 = new String ("老龙恼怒闹老农" ); String str2 = new String ("老农恼怒闹老龙" ); System.out.println(str1 + "," + str2 + "。" ); String str3 = "农怒龙恼农更怒" ; String str4 = "龙恼农怒龙怕农" ; String str5 = str3.concat("," ).concat(str4).concat("。" ); System.out.println(str5); String str6 = "我有一本《水浒传》" ; float time = 0.5f ; String str7 = str6 + ",每天阅读" + time + "小时。" ; System.out.println(str7); System.out.println(str7 + "字符串长度:" + str7.length()); String str8 = "ABCDEFG、hijklmn、OpQ、RsT、UVW、xyz" ; System.out.println(str8.toLowerCase()); System.out.println(str8.toUpperCase()); String str9 = " H e l l o " ; System.out.println("去除字符串首尾两侧的空格:" + str9.trim()); System.out.println("用于替换字符串中的字母:" + str9.replace(" " , "a" )); } }
5-2 提取 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 import java.util.Arrays;public class Test018 { public static void main (String[] args) { String str1 = "Peter Piper picked a peck of pickled peppers." ; System.out.println(str1.substring(19 )); System.out.println(str1.substring(19 , 45 )); String str2 = "abcd,efg,hijk,lmn,opq,rst,uvw,xyz" ; String[] str3 = str2.split("," ); System.out.println(Arrays.toString(str3)); String[] str4 = str2.split("," , 8 ); System.out.println(Arrays.toString(str4)); String str5 = "4*apple+pear=3*banana and 1*cherry or 2*strawberry" ; String[] str6 = str5.split("and | or" ); System.out.println(Arrays.toString(str6)); System.out.println(str1.replace("P" , "T" )); System.out.println(str1.replaceFirst("Peter" , "Nancy" )); System.out.println(str1.replaceAll("pi" , "ar" )); } }
5-3 比较 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 public class Test019 { public static void main (String[] args) { String str1 = "abc" ; String str2 = new String ("abc" ); String str3 = "ABC" ; System.out.println(str1.equals(str2)); System.out.println(str1.equals(str3)); System.out.println("-----" ); System.out.println(str1.equalsIgnoreCase(str3)); System.out.println("-----" ); System.out.println(str1 == str2); System.out.println(str1.equals(str2)); System.out.println("-----" ); System.out.println(str1.compareTo(str2)); System.out.println(str1.compareTo(str3)); System.out.println(str1.compareTo("abcd" )); } }
5-4 查找 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class Test020 { public static void main (String[] args) { String str = "A peck of pickled peppers Peter Piper picked." ; System.out.println(str.indexOf("peck" )); System.out.println(str.indexOf("p" , 3 )); System.out.println(str.lastIndexOf("pe" )); System.out.println(str.lastIndexOf("pe" , 20 )); System.out.println(str.charAt(5 )); } }
5-5 StringBuffer
StringBuffer
Java提供了两个可变字符串类StringBuffer和StringBuilder,即字符串缓冲区。
StringBuffer线程安全,StringBuilder线程不安全,性能略高,功能基本相似。
一般情况下,相对速度从快到慢依次为:StringBuilder>StringBuffer>String。
少量数据用String,单线程大量数据用StringBuilder,多线程大量数据用StringBuffer。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 public class Test021 { public static void main (String[] args) { StringBuffer str1 = new StringBuffer (); System.out.println(str1.capacity()); StringBuffer str2 = new StringBuffer (18 ); System.out.println(str2.capacity()); StringBuffer str3 = new StringBuffer ("学海无涯" ); System.out.println(str3.capacity()); StringBuffer str4 = new StringBuffer ("Hay" ); System.out.println(str4); str4.setCharAt(1 , 'e' ); System.out.println(str4); str4.reverse(); System.out.println(str4); str4.reverse(); String str5 = " Baby" ; System.out.println(str4.append(str5).substring(0 )); str4.append(str5).deleteCharAt(8 ); System.out.println(str4); str4.append(str5).delete(12 , 17 ); System.out.println(str4); } }
5-6 正则表达式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public class Test022 { public static void main (String[] args) { String str = "Hello, this is Mike speaking." ; System.out.println(str.replaceFirst("\\w*" , "·" )); System.out.println(str.replaceFirst("\\w*?" , "·" )); } }
(1) regex类库 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test023 { public static void main (String[] args) { String str = "联系13500006666,电话号码13611125565,联系15899903312" ; Matcher m = Pattern.compile("((13\\d)|(15\\d))\\d{8}" ).matcher(str); while (m.find()) { System.out.println(m.group()); } String regStr = "Java Easy!" ; Matcher n = Pattern.compile("\\w+" ).matcher(regStr); while (n.find()) { System.out.println(n.group() + "---起:" + n.start() + ",止:" + n.end()); } String[] mails = { "Test626@outlook.com" , "Test626@hotmail.com" , "Test626@crazyit.org" , "Test626@foxmail.xxx" }; String mailRegEx = "\\w{3,20}@\\w+\\.(com|org|cn|net|gov)" ; Pattern mailPattern = Pattern.compile(mailRegEx); Matcher matcher = null ; for (String mail : mails) { if (matcher == null ) { matcher = mailPattern.matcher(mail); } else { matcher.reset(mail); } String result = mail + (matcher.matches() ? "---有效" : "---无效" ); System.out.println(result); } } }
(2) 验证IP地址 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import java.util.Scanner;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test024 { private static final String IP_REGEX = "^((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)\\.){3}" + "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)$" ; private static final Pattern IP_PATTERN = Pattern.compile(IP_REGEX); public static boolean isValidIP (String ip) { if (ip == null || ip.isEmpty()) { return false ; } Matcher matcher = IP_PATTERN.matcher(ip); return matcher.matches(); } public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.println("请输入IP地址:" ); String input = scanner.nextLine(); System.out.println( input + "---" + (isValidIP(input) ? "格式正确!" : "格式错误!" ) ); scanner.close(); } }
(3) 验证座机号 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import java.util.Scanner;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test025 { public static void main (String[] args) { String answer = "Y" ; String regex = "0\\d{2,3}[-]?\\d{7,8}|0\\d{2,3}\\s?\\d{7,8}|" + "13[0-9]\\d{8}|15[1089]\\d{8}" ; do { System.out.print("请输入座机号:" ); Scanner scanner = new Scanner (System.in); String phone = scanner.next(); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(phone); boolean bool = matcher.matches(); if (bool) { System.out.println("您输入的座机号格式正确。" ); } else { System.out.println("您输入的座机号格式错误!" ); } System.out.print("是否继续输入(Y/N or y/n)?" ); answer = scanner.next(); } while (answer.equalsIgnoreCase("Y" )); } }
6 数字与日期
数字与日期
Math类:提供基本的数学操作,如指数等。
Random类:提供了丰富的随机数生成方法。
BigInteger类:针对整型大数字的处理类。
BigDecimal类:是指针对大小数的处理类。
Date类:系统特定的时间戳,可精确到毫秒。
Calendar类:为特定瞬间的转换提供了方法。
DateFormat类:是日期、时间格式化子类的抽象类。
SimpleDateFormat类:格式化和解析日期的具体类。
6-1 Math类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class Test026 { public static void main (String[] args) { System.out.println(Math.E); System.out.println(Math.max(10 , -50 )); System.out.println(Math.min(70 , 10 )); System.out.println(Math.abs(-50 )); System.out.println(Math.round(18.153 )); System.out.println(Math.ceil(18.153 )); System.out.println(Math.floor(18.153 )); System.out.println(Math.rint(18.153 )); System.out.println(Math.sin(Math.PI / 2 )); System.out.println(Math.cos(0 )); System.out.println(Math.atan(1 )); System.out.println(Math.toRadians(120 )); System.out.println(Math.pow(4 , 2 )); System.out.println(Math.sqrt(100 )); System.out.println(Math.log10(16 )); } }
6-2 生成随机数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import java.util.Random;public class Test027 { public static void main (String[] args) { System.out.println((int ) (2 + Math.random() * 100 )); Random r = new Random (); System.out.println(r.nextBoolean()); System.out.println(r.nextInt()); System.out.println(r.nextFloat()); System.out.println(r.nextDouble()); System.out.println(r.nextLong()); } }
6-3 数字格式化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 import java.util.Scanner;import java.text.DecimalFormat;public class Test028 { public static void main (String[] args) { DecimalFormat df1 = new DecimalFormat ("0.0" ); DecimalFormat df2 = new DecimalFormat ("#.#" ); DecimalFormat df3 = new DecimalFormat ("000.000" ); DecimalFormat df4 = new DecimalFormat ("###.###" ); Scanner scan = new Scanner (System.in); System.out.print("请输入一个float类型的数字:" ); float f = scan.nextFloat(); System.out.println("0.0 格式:" + df1.format(f)); System.out.println("#.# 格式:" + df2.format(f)); System.out.println("000.000 格式:" + df3.format(f)); System.out.println("###.### 格式:" + df4.format(f)); } }
6-4 大数字运算 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import java.math.BigDecimal;import java.math.BigInteger;public class Test029 { public static void main (String[] args) { BigInteger bi = new BigInteger ("10" ); System.out.println(bi); System.out.println(bi.add(new BigInteger (("70" )))); BigDecimal bd = new BigDecimal (10 ); System.out.println(bd); System.out.println(bd.subtract(new BigDecimal ((-11 )))); } }
6-5 时间的处理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import java.util.Date;import java.util.Calendar;public class Test030 { public static void main (String[] args) { Date date = new Date (); System.out.println(date); Calendar c = Calendar.getInstance(); c.setTime(new Date ()); System.out.println(c.getTime()); System.out.println(c.get(Calendar.YEAR)); } }
6-6 日期格式化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 import java.util.Date;import java.util.Locale;import java.text.DateFormat;import java.text.SimpleDateFormat;public class Test031 { public static void main (String[] args) { DateFormat dfdate = DateFormat.getDateInstance(DateFormat.FULL, Locale.CHINA); DateFormat dftime = DateFormat.getTimeInstance(DateFormat.FULL, Locale.CHINA); String date = dfdate.format(new Date ()); String time = dftime.format(new Date ()); System.out.println(date + " " + time); Date now = new Date (); SimpleDateFormat f = new SimpleDateFormat ("yyyy年M月d日 E HH点mm分ss秒 SSS毫秒" ); System.out.println(f.format(now)); } }