常见java知识点1
发布人:shili8
发布时间:2025-01-31 14:44
阅读次数:0
**常见Java知识点**
Java是一种广泛使用的编程语言,尤其是在企业级应用开发中。以下是常见的Java知识点:
###1. Java基本数据类型Java有八个基本数据类型:
* `byte`:8位有符号整数* `short`:16位有符号整数* `int`:32位有符号整数* `long`:64位有符号整数* `float`:32位浮点数* `double`:64位浮点数* `boolean`:布尔值(true或false)
* `char`:16位Unicode字符
javapublic class BasicDataType { public static void main(String[] args) { // byte byte b =127; System.out.println("byte:" + b); // short short s =32767; System.out.println("short:" + s); // int int i =2147483647; System.out.println("int:" + i); // long long l =9223372036854775807L; System.out.println("long:" + l); // float float f =3.14f; System.out.println("float:" + f); // double double d =3.14159265358979323846; System.out.println("double:" + d); // boolean boolean bln = true; System.out.println("boolean:" + bln); // char char c = 'A'; System.out.println("char:" + c); } }
###2. Java运算符Java支持以下运算符:
* 算术运算符:`+、-、*、/、%、++、--`
* 关系运算符:`==、!=、>、<、>=、<=`
* 逻辑运算符:`&&、||、!`
* 赋值运算符:`=、+=、-=、*=、/=、%=、&=、|=、^=、<<=、>>=`
javapublic class Operator { public static void main(String[] args) { // 算术运算符 int a =10; int b =2; System.out.println("a + b:" + (a + b)); System.out.println("a - b:" + (a - b)); System.out.println("a * b:" + (a * b)); System.out.println("a / b:" + (a / b)); System.out.println("a % b:" + (a % b)); // 关系运算符 boolean bln1 = a == b; boolean bln2 = a != b; boolean bln3 = a > b; boolean bln4 = a < b; boolean bln5 = a >= b; boolean bln6 = a <= b; System.out.println("a == b:" + bln1); System.out.println("a != b:" + bln2); System.out.println("a > b:" + bln3); System.out.println("a < b:" + bln4); System.out.println("a >= b:" + bln5); System.out.println("a <= b:" + bln6); // 逻辑运算符 boolean bln7 = a >0 && b >0; boolean bln8 = a >0 || b < 0; boolean bln9 = !bln1; System.out.println("a >0 && b >0:" + bln7); System.out.println("a >0 || b < 0:" + bln8); System.out.println("!bln1:" + bln9); // 赋值运算符 int c = a; System.out.println("c:" + c); c +=10; System.out.println("c +=10:" + c); c -=5; System.out.println("c -=5:" + c); c *=2; System.out.println("c *=2:" + c); c /=3; System.out.println("c /=3:" + c); } }
###3. Java控制结构Java支持以下控制结构:
* 条件语句:`if、else if、else`
* 循环语句:`while、for、do-while`
* switch语句
javapublic class ControlStructure { public static void main(String[] args) { // 条件语句 int a =10; if (a >0) { System.out.println("a >0"); } else { System.out.println("a <=0"); } if (a ==5) { System.out.println("a ==5"); } else if (a ==10) { System.out.println("a ==10"); } else { System.out.println("a !=5 && a !=10"); } // 循环语句 int b =0; while (b < 5) { System.out.println("b:" + b); b++; } for (int c =0; c < 5; c++) { System.out.println("c:" + c); } do { System.out.println("d:"); int d =0; d++; } while (d < 5); // switch语句 char e = 'A'; switch (e) { case 'A': System.out.println("e == A"); break; case 'B': System.out.println("e == B"); break; default: System.out.println("e != A && e != B"); break; } } }
###4. Java数组Java支持以下类型的数组:
* 一维数组* 多维数组
javapublic class Array { public static void main(String[] args) { // 一维数组 int[] a = new int[5]; for (int i =0; i < 5; i++) { a[i] = i; } System.out.println("a:"); for (int j : a) { System.out.print(j + " "); } // 多维数组 int[][] b = new int[2][3]; for (int i =0; i < 2; i++) { for (int j =0; j < 3; j++) { b[i][j] = i *3 + j; } } System.out.println(" b:"); for (int[] c : b) { for (int d : c) { System.out.print(d + " "); } System.out.println(); } } }
###5. Java集合Java支持以下类型的集合:
* `List`
* `Set`
* `Map`
javaimport java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; public class Collection { public static void main(String[] args) { // List ArrayListlist = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); System.out.println("list:"); for (String str : list) { System.out.print(str + " "); } // Set HashSet set = new HashSet<>(); set.add("A"); set.add("B"); set.add("C"); System.out.println(" set:"); for (String str : set) { System.out.print(str + " "); } // Map HashMap map = new HashMap<>(); map.put("A",1); map.put("B",2); map.put("C",3); System.out.println(" map:"); for (String key : map.keySet()) { System.out.print(key + ": " + map.get(key) + " "); } } }
以上是常见的Java知识点,包括基本数据类型、运算符、控制结构、数组和集合。这些知识点对于理解Java编程语言至关重要。