Java介绍
| |
| -找到加密算法---》chat gpt,让它给你写---》转成python---》自己手动调 |
| |
| |
| -java se:java基础---》python中变量定义,函数,面向对象,文件处理,并发,网络编程 |
| -java me:早些年,做手机开发用的,现在弃用了,出了安卓操作系统后,谷歌公司出了安卓sdk,用java代码写 |
| -java ee:做java 的web 方向开发需要学的 |
| -spring框架,springboot,springcloud 基于javaee的标准 |
| -你们如果要做java开发,百分之95以上,都是java ee工程师 |
| |
| |
| |
| |
| -java sdk :jdk--》23版本---》用1.8 就是8---》现在公司里大量用java8,反编译回来的代码也是java8的代码 |
| -java 开发环境 |
| -jre java运行环境,只要运行java的 jar,war 需要安装这个,但是公司里一般,只允许java程序,也装sdk |
| -jvm java虚拟机,java是编译型,把源代码编译成字节码文件---》运行再jvm上 |
| -由于不同平台可以装不同平台的jvm虚拟机,于是,java跨平台 |
| |
| |
| |
| -正常来讲,编译型语言,是不能夸平台的 |
| -c,go(跨平台编译) ---》编译过后,就是当前平台的可执行文件 win:exe linux:可执行文件 mac:可执行文件--》不能夸平台 |
| -开发阶段需要装sdk,运行阶段,不需要装任何东西 |
| |
| -java是个特例---》java编译成中间态[不是可执行文件,字节码文件]---》不能直接运行再操作系统之上----》需要运行再jvm上---》所有运行java程序要有jvm |
| -java到底是不是编译型语言? |
| -是,因为有编译,只是没有编译成可执行 |
| -可以这么想象:java编译过后的字节码文件(jar,war包),运行再jvm上【像python解释器一样的东西】 |
| |
| -解释性语言:python,php,nodejs |
| -边解释边执行,不需要编译 |
| -无论是写代码还是运行代码,都需要装解释器(源码运行再解释器上) |
| |
| |
| |
| |
| java version "1.8.0_371" |
| Java(TM) SE Runtime Environment (build 1.8.0_371-b11) |
| Java HotSpot(TM) 64-Bit Server VM (build 25.371-b11, mixed mode) |
1 Java环境搭建
1.1 安装jdk配置环境变量
| |
| |
| 是否需要再选择安装一个额外的JRE。 |
| 这是可选的,因为之前安装的JDK中,已经包含了开发环境和JRE运行环境两部分,所以不必再安装一个JRE。 |
| |
| |
| |
| |
| |
| JAVA_HOME D:\Program Files\Java\jdk-1.8 |
| |
| |
| CLASSPATH .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar; |
| |
| |
| %JAVA_HOME%\bin |
| %JAVA_HOME%\jre\bin |
1.2 第一个helloworld
| |
| public class HelloWorld { |
| public static void main(String[] args) { |
| System.out.println("hello world"); |
| } |
| } |
| |
| |
| javac HelloWorld.java |
| |
| java HelloWorld |
1.2 编辑器
| |
| Eclipse是一个免费的开源Java IDE,提供了丰富的功能和插件扩展。它支持Java应用程序、Web应用程序和企业级应用程序的开发,并具有强大的代码编辑、调试和测试工具。 |
| |
| MyEclipse是一个专为Java开发而设计的集成开发环境(IDE),它是Eclipse IDE的一个商业化版本。MyEclipse提供了许多功能和工具,旨在提高Java开发人员的生产力和效率 |
| |
| |
| IntelliJ IDEA是一种商业化的Java IDE,也有免费的社区版可用。它提供了智能代码完成、代码检查和重构等功能,支持Java开发以及其他相关技术,如Spring和Hibernate,同属于Jetbrains系列,使用习惯跟Pycharm类似,我们选择此编辑器 |
1.2.1 安装-破解
| |
| |
| jihuo-tooll-2023.1\jetbra\scripts\install-current-user.vbs |
| |
| jihuo-tooll-2023.1\激活码.txt |
| |
1.2.2 新建项目

2 Java语法快速入门
2.1 程序的入口
| |
| public static void main(String[] args) { |
| System.out.println("hello world"); |
| } |
2.2 文件名
| public class Main { |
| public static void main(String[] args){ |
| System.out.println("hello world"); |
| Demo.add(3,4); |
| } |
| } |
| |
| class Demo{ |
| public static void add(int a,int b){ |
| System.out.println(a+b); |
| } |
| } |
2.3 类规范
| |
| 类名首字母大写且驼峰式命名 |
| 例如:Hello、UserInfo、PersonApplication |
| |
| |
| public、default(不写) |
| |
| |
| public、private、protected、default(不写) |
| -public:公有的,所有其他类中都可以使用 |
| -private:私有的,只有在当前类中可以使用 |
| -protected:受保护的,只有在当前包下的类中可以使用 |
| -default(不写):默认 |
| |
| |
| 无序实例化就可以指定调用 |
| |
| |
| |
| public class Main { |
| public static String name = "彭于晏"; |
| |
| public static void main(String[] args) { |
| |
| Demo.add(3, 4); |
| |
| System.out.println(name); |
| |
| System.out.println(Demo.age); |
| |
| Demo d = new Demo(); |
| d.test(); |
| } |
| } |
| |
| class Demo { |
| private static String name = "张三"; |
| public static int age = 19; |
| |
| public static void add(int a, int b) { |
| System.out.println(a + b); |
| System.out.println(name); |
| } |
| |
| public void test() { |
| System.out.println("我是test"); |
| } |
| } |
3 基本语法
3.1 注释
| |
| 以双斜杠“//”标识,只能注释一行内容,用在注释信息内容少的地方 |
| 快捷键:【注释 ctrl+/ 】 【 取消注释 ctrl+/ 】 |
| |
| |
| 包含在“/*”和“*/”之间,能注释很多行的内容,可以换行 |
| 快捷键:【注释 ctrl+shift+/ 】 【取消注释 ctrl+shift+\ 】 |
| |
| |
| 包含在“/**”和“*/”之间,也能注释多行内容,一般用在类、方法和变量上面,用来描述其作用。注释后,鼠标放在类和变量上面会自动显示出我们注释的内容 |
| /** |
| * |
| * @param a |
| * @param b |
| */ |
| public static void add(int a, int b) { |
| System.out.println(a + b); |
| System.out.println(name); |
| } |
3.2 变量和常量
| |
| String name="彭于晏"; |
| name = "李易峰"; |
| |
| int age = 19; |
| age = 20; |
| System.out.println(name); |
| System.out.println(age); |
| |
| |
| final String hobby="足球"; |
| hobby="篮球" |
| System.out.println(hobby); |
| |
| |
| 父类中的 final 方法可以被子类继承,但是不能被子类重写。 |
| 声明 final 方法的主要目的是防止该方法的内容被修改 |
| |
| |
| |
| final 类不能被继承,没有类能够继承 final 类的任何特性 |
3.3 输入输出
| |
| System.out.println("hello"); |
| System.out.print("world"); |
| System.out.printf("变量值是:%s%n",name); |
| |
| |
| import java.util.Scanner; |
| public class Hello { |
| public static void main(String[] args) { |
| System.out.print("请输入您的名字:"); |
| Scanner input = new Scanner(System.in); |
| String InputName = input.nextLine(); |
| System.out.printf("您输入的名字是:%s",InputName); |
| |
| } |
| } |
| |

3.4 条件语句
| import java.util.Scanner; |
| |
| public class Demo02 { |
| public static void main(String[] args) { |
| System.out.println("请输入您的分数:"); |
| Scanner scanner = new Scanner(System.in); |
| int score = Integer.valueOf(scanner.nextLine()); |
| |
| if (score >= 90 && score <= 100) { |
| System.out.println("优秀"); |
| } else if (score >= 70 && score < 90) { |
| System.out.println("良好"); |
| } else if (score >= 60 && score < 70) { |
| System.out.println("及格"); |
| } else { |
| System.out.println("不及格"); |
| } |
| } |
| } |
| |
| import java.util.Scanner; |
| |
| public class Demo02 { |
| public static void main(String[] args) { |
| System.out.println("请输入您的分数:"); |
| Scanner scanner = new Scanner(System.in); |
| int score = Integer.valueOf(scanner.nextLine()); |
| switch (score){ |
| case 99: |
| System.out.println("恭喜您99分"); |
| break; |
| case 88: |
| System.out.println("恭喜您88分"); |
| break; |
| default: |
| System.out.println("不知道多少分"); |
| |
| } |
| } |
| } |
3.5 循环语句
while循环
| |
| public class Demo03 { |
| public static void main(String[] args) { |
| int count = 0; |
| while (count < 10) { |
| System.out.println(count); |
| count += 1; |
| } |
| } |
| } |
do while循环(至少执行1次)
| public class Demo03 { |
| public static void main(String[] args) { |
| int count = 0; |
| do{ |
| System.out.println(count); |
| count++; |
| |
| }while (count<10); |
| } |
| } |
for循环
| public class Demo03 { |
| public static void main(String[] args) { |
| for(int i=0;i<10;i++){ |
| System.out.println(i); |
| } |
| } |
| } |
| |
| public class Demo03 { |
| public static void main(String[] args) { |
| String[] nameList = {"李易峰", "刘亦菲", "彭于晏", "古天乐"}; |
| for(int i=0;i<nameList.length;i++){ |
| System.out.println(nameList[i]); |
| } |
| } |
| } |
break/continue跟其他语言同理
4 数据类型
| |
| |
| byte 带符号字节型 8位 -128 ~ 127 |
| short 带符号字节型 16位 -32768 ~ 32767 |
| int 带符号字节型 32位 -231 -2147483648 ~ 2147483647 |
| long 带符号字节型 64位 -263 -9223372036854775808 ~ 9223372036854775807 |
| |
| float |
| double |
| |
| |
| |
| |
| |
4.1 字节类型
| |
| |
| |
| |
| |
| |
| public class Demo04 { |
| public static void main(String[] args) { |
| byte b='A'; |
| System.out.println(b); |
| System.out.println(Integer.toBinaryString(b)); |
| System.out.println(Integer.toHexString(b)); |
| |
| byte a=98; |
| System.out.println(a); |
| System.out.println(Integer.toBinaryString(a)); |
| System.out.println(Integer.toHexString(a)); |
| } |
| } |
| |
| |
| byte b=65; |
| byte a=66; |
| int c=a+b; |
| byte d=(byte)(a+b); |
| System.out.println(c); |
| System.out.println(d); |
| |
| |
python中字符串与字节
| |
| v1 = '彭于晏' |
| v2 = v1.encode('utf-8') |
| print(v2) |
| v3 = v1.encode('GBK') |
| print(v3) |
| |
| |
| v3 = [item for item in v2] |
| print(v3) |
| |
| |
| v4 = [bin(item) for item in v2] |
| print(v4) |
| |
| |
| v5 = [hex(item) for item in v2] |
| print(v5) |
| |
java中字符串与字节数组
| import java.util.Arrays; |
| |
| public class Demo04 { |
| public static void main(String[] args) throws Exception { |
| String v1 = "彭于晏"; |
| byte[] v2 = v1.getBytes(); |
| System.out.println(v2); |
| System.out.println(Arrays.toString(v2)); |
| |
| |
| byte[] v22 = v1.getBytes("GBK"); |
| System.out.println(v22); |
| System.out.println(Arrays.toString(v22)); |
| |
| } |
| } |
字节有无符号
| |
| Python字节无符号(不含负数): 0 ~ 255 |
| 0 1 2 3 4 5 ... 127 128 129 130 ... 255 |
| |
| Java字节是有符号(含有负数):-128 ~ 127 |
| 0 1 2 3 4 5 ...127 -128 -127 - 126 -125 .... -2 -1 |
| |
| |
| [229, 189, 173, 228, 186, 142, 230, 153, 143] |
| |
| [-27, -67, -83, -28, -70, -114, -26, -103, -113] |
| |
| |
| |
| 逻辑是:数字大于0,不操作,小于0,加上256 |
| |
| v1 = [-27, -67, -83, -28, -70, -114, -26, -103, -113] |
| def java_arr_to_python_str(v1): |
| num_list = bytearray() |
| for i in v1: |
| if i < 0: |
| i = i + 256 |
| num_list.append(i) |
| return num_list.decode('utf-8') |
| |
| |
| if __name__ == '__main__': |
| print(java_arr_to_python_str(v1)) |
4.2 整数类型
| |
| |
| |
| |
| |
| byte v1 = 32; |
| short v2 = 10000; |
| int v3 = 22221331; |
| long v4 = 554534353424L; |
| System.out.println(v1); |
| System.out.println(v2); |
| System.out.println(v3); |
| System.out.println(v4); |
4.3 小数类型
| |
| |
| float f=1.123456789012f; |
| System.out.println(f); |
| double d=1.12345678901234567890d; |
| System.out.println(d); |
4.4 字符类型
| char v1 = 'x'; |
| char v2 = '贾'; |
| String v3 = "彭于晏"; |
4.5 字符串类型
| String v1 = "彭于晏"; |
| String v2 = new String("彭于晏"); |
| |
| String v3 = new String(new byte[]{-27, -67, -83, -28, -70, -114, -26, -103, -113}); |
| String v9 = new String(new byte[]{-59, -19, -45, -38, -22, -52},"GBK"); |
| String v4 = new String(new char[]{'彭', '于', '晏'}); |
| |
| System.out.println(v1); |
| System.out.println(v2); |
| System.out.println(v3); |
| System.out.println(v4); |
| |
字符串常用方法
| String origin = "hello world 中国"; |
| |
| |
| char v1 = origin.charAt(13); |
| System.out.println(v1); |
| |
| |
| int len = origin.length(); |
| for (int i = 0; i < len; i++) { |
| char item = origin.charAt(i); |
| System.out.println(item); |
| } |
| |
| String v2 = origin.trim(); |
| System.out.println(v2); |
| |
| String v3 = origin.toLowerCase(); |
| System.out.println(v3); |
| String v4 = origin.toUpperCase(); |
| System.out.println(v4); |
| String[] v5 = origin.split(" "); |
| System.out.println(v5); |
| String v6 = origin.replace("h", "l"); |
| System.out.println(v6); |
| String v7 = origin.substring(2, 6); |
| System.out.println(v7); |
| boolean v8 = origin.equals("hello world"); |
| System.out.println(v8); |
| boolean v9 = origin.contains("中"); |
| System.out.println(v9); |
| boolean v10 = origin.startsWith("a"); |
| System.out.println(v10); |
| String v11 = origin.concat("哈哈哈"); |
| System.out.println(v11); |
字节数组和字符串相互转换
| |
| byte[] b = v4.getBytes(); |
| System.out.println(b); |
| System.out.println(Arrays.toString(b)); |
| |
| |
| |
| |
| |
| |
| |
| byte[] b1 = v4.getBytes("GBK"); |
| System.out.println(Arrays.toString(b1)); |
| |
| |
| |
| String v8 = new String(new byte[]{-27, -67, -83, -28, -70, -114, -26, -103, -113}); |
| String v9 = new String(new byte[]{-59, -19, -45, -38, -22, -52},"GBK"); |
| System.out.println(v8); |
| System.out.println(v9); |
字符数组和字符串相互转换
| |
| char[] c=new char[]{'彭','于','晏'}; |
| String v1 = new String(c); |
| System.out.println(v1); |
| |
| |
| String str1 = "彭于晏"; |
| char[] c=str1.toCharArray(); |
| System.out.println(c.length); |
| System.out.println(c[0]); |
字符串拼接
| |
| String s="hello"+"world"+"中国"; |
| |
| |
| StringBuilder sb = new StringBuilder(); // StringBuffer线程安全 |
| //StringBuffer sb = new StringBuffer(); // StringBuffer线程安全 |
| sb.append("hello"); |
| sb.append(" "); |
| sb.append("world"); |
| sb.append(" "); |
| sb.append("中"); |
| sb.append("国"); |
| System.out.println(sb.toString()); |
4.5 数组类型
| |
| |
| int[] numArray = new int[3]; |
| numArray[0] = 123; |
| |
| numArray[2] = 99; |
| System.out.println(numArray); |
| System.out.println(Arrays.toString(numArray)); |
| |
| |
| String[] names = new String[]{"彭于晏", "刘亦菲", "迪丽热巴"}; |
| System.out.println(Arrays.toString(names)); |
| |
| String[] nameArray = {"彭于晏", "刘亦菲", "迪丽热巴"}; |
| System.out.println(Arrays.toString(nameArray)); |
4.6 boolean类型
| boolean b=true; |
| b=false; |
| System.out.println(b); |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步