Java基础入门
2004年9月, 推出 Java 5
版本 2014年3月18日, 推出 Java 8
版本 2021年9月14日, 推出 Java 17
版本
Java发展史 1 2 3 4 5 6 7 8 9 2004 年9 月,推出 Java 5 版本2014 年3 月18 日,推出 Java 8 版本2021 年9 月14 日,推出 Java 17 版本Java17版本应用 Spring Framework 6 Srpring Boot 3 Elasticsearch 8. X Kafka 4.0
简单java
文件运行原理 1 2 3 4 5 6 jdk 安装目录 D:\soft_position\Java\jdk1.8.0_191\bin 这个下面值java相关的一些命令 #javac.exe ## 编译器编译java的代码,输出到 out 下面 对应的 xxx.class 文件 #java.exe ## 启动JVM
逻辑运算符 与或 1 2 3 4 5 6 7 8 9 10 11 12 13 int i = 10 ;boolean resA = (i < 5 ) & (i < 20 );int n = 10 ;boolean resB = (n < 5 ) | (n > 20 );
短路与或 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 int m = 10 ;int n = 20 ;boolean resA = (m > 10 ) && (++n > 30 );System.out.println(n); boolean resB = (m > 5 ) && (++n > 30 );System.out.println(n); int x = 10 ;int y = 20 ;boolean resA = (x == 10 ) || (++y > 30 );System.out.println(y); boolean resB = (x == 50 ) || (++y > 30 );System.out.println(y);
面向对象 1 2 3 4 5 6 7 class Book { private String name; private String author; private String type; private int price; }
构造方法 1 2 3 4 5 6 class Book { Book() { System.out.println("我是构造方法,自动执行" ); } }
多态 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 class Person { void testPerson () { System.out.println("输出Person" ); } } class Boy extends Person { void testBoy () { System.out.println("输出boy" ); } } class Girl extends Person { void testGirl () { System.out.println("输出girl" ); } } Person p = new Person(); p.testPerson(); Person p1 = new Boy(); p1.testPerson(); Person p2 = new Girl(); p2.testPerson(); Boy boy = new Boy(); boy.testBoy();
递归 斐波那契 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class Fei01_HelloWorld { public static void main (String[] args) { for (int i = 0 ; i < 10 ; i++) { System.out.println(funFoo(i)); } } public static int funFoo (int n) { if (n == 0 || n == 1 ) { return 1 ; }else { return funFoo(n - 1 ) + funFoo(n - 2 ); } } }
枚举类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class Fei01_HelloWorld { public static void main (String[] args) { System.out.println(City.BEIJING.name); System.out.println(City.SHANGHAI.code); } } enum City { BEIJING("北京" , 1001 ), SHANGHAI("上海" , 1002 ); public final String name; public final int code; City(String name, int code) { this .name = name; this .code = code; } }
匿名类 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 Fei01_HelloWorld { public static void main (String[] args) { Fei fei = new Fei(); fei.sayHello(new Person() { public String name () { return "张三" ; } }); fei.sayHello(new Person() { public String name () { return "李四" ; } }); } } abstract class Person { public abstract String name () ; } class Fei { public void sayHello (Person person) { System.out.println("hello" + person.name()); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class Fei01_HelloWorld { public static void main (String[] args) { Person p = new Person() { public void eat () { System.out.println("eat something" ); } }; p.eat(); } } interface Person { public void eat () ; }
Bean类
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 public class Fei01_HelloWorld { public static void main (String[] args) { UserBean userBean = new UserBean(); userBean.setAccount("dafei" ); userBean.setPassword("123" ); boolean isLogin = login(userBean); if (isLogin) { System.out.println("登录成功" ); } else { System.out.println("登录失败" ); } } public static boolean login (UserBean userBean) { if (userBean.getAccount().equals("dafei" ) && userBean.getPassword().equals("123" ) ) { return true ; } else { return false ; } } } class UserBean { private String account; private String password; public String getAccount () { return account; } public void setAccount (String account) { this .account = account; } public String getPassword () { return password; } public void setPassword (String password) { this .password = password; } }
常见类和对象-Object 创建一个类后,使用快捷键Ctrl + o
,可以看到复写方法
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 public class Fei01_HelloWorld { public static void main (String[] args) { Person person = new Person(); String s = person.toString(); System.out.println("十六进制内存地址" + s); int i = person.hashCode(); System.out.println("十进制内存地址" + i); String s1 = Integer.toHexString(i); System.out.println("十六进制内存地址" + s1); Person otherPerson = new Person(); int n = otherPerson.hashCode(); System.out.println("十六进制内存地址" + n); System.out.println(person.equals(otherPerson)); } } class Person { @Override public boolean equals (Object obj) { return true ; } }
常见类和对象-数组 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class Fei01_HelloWorld { public static void main (String[] args) { String[] names = new String[3 ]; names[0 ] = "张三" ; names[1 ] = "李四" ; names[2 ] = "王五" ; for (int i = 0 ; i < names.length; i++) { System.out.println(names[i]); } for (String name : names) { System.out.println(name); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class Fei01_HelloWorld { public static void main (String[] args) { User[] users = new User[3 ]; for (int i = 0 ; i < users.length; i++) { users[i] = new User(); } for (int i = 0 ; i < users.length; i++) { users[i].test(); } for (User user : users) { user.test(); } } } class User { public void test () { System.out.println("输出内容test" ); } }
底部 没有了