Java入门基础
1. 强制类型转换
package com. itheima. demo01 ;
public class HelloWorld {
public static void main ( String [ ] args) {
short s = 10 ;
s = ( short ) ( s + 3 ) ;
s += 3 ;
}
}
2. 输入输出
package com. itheima. demo01 ;
import java. util. Scanner ;
public class HelloWorld {
public static void main ( String [ ] args) {
System . out. println ( "请录入您的年龄" ) ;
Scanner sc = new Scanner ( System . in) ;
int age = sc. nextInt ( ) ;
System . out. println ( "age: " + age) ;
}
}
3. 三目运算符
package com. itheima. demo01 ;
import java. util. Scanner ;
public class HelloWorld {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "第一个身高" ) ;
int h1 = sc. nextInt ( ) ;
System . out. println ( "第一个身高" ) ;
int h2 = sc. nextInt ( ) ;
System . out. println ( "第一个身高" ) ;
int h3 = sc. nextInt ( ) ;
int temp = h1 < h2 ? h2 : h1;
int max = temp < h3 ? h3 : temp;
System . out. println ( "max: " + max) ;
}
}
4. if else语句
package com. itheima. demo01 ;
public class HelloWorld {
public static void main ( String [ ] args) {
int time = 10 ;
if ( time > 0 && time <= 8 ) {
System . out. println ( "早上好" ) ;
} else if ( time > 8 && time <= 17 ) {
System . out. println ( "下午好" ) ;
} else {
System . out. println ( "晚上好" ) ;
}
}
}
5. for循环
package com. itheima. demo01 ;
public class HelloWorld {
public static void main ( String [ ] args) {
for ( int i= 1 ; i <= 5 ; i++ ) {
System . out. println ( i) ;
}
}
}
6. while循环
package com. itheima. demo01 ;
public class HelloWorld {
public static void main ( String [ ] args) {
int i = 0 ;
while ( i< 5 ) {
System . out. println ( "hello world" ) ;
i++ ;
}
}
}
7. 生成随机数
package com. itheima. demo01 ;
public class HelloWorld {
public static void main ( String [ ] args) {
int num;
for ( int i = 1 ; i <= 5 ; i++ ) {
num = ( int ) ( Math . random ( ) * 10 + 1 ) ;
System . out. println ( num) ;
}
}
}
8. 数组的定义方法
package com. itheima. demo01 ;
public class HelloWorld {
public static void main ( String [ ] args) {
int [ ] arr1 = new int [ 3 ] ;
int arr2[ ] = new int [ 3 ] ;
int [ ] arr3 = new int [ ] { 11 , 22 , 33 } ;
int [ ] arr4 = { 11 , 22 , 33 } ;
}
}
9. 数组的基本用法
package com. itheima. demo01 ;
public class HelloWorld {
public static void main ( String [ ] args) {
int [ ] arr1 = { 11 , 22 , 33 } ;
System . out. println ( arr1[ 1 ] ) ;
arr1[ 1 ] = 222 ;
System . out. println ( arr1[ 1 ] ) ;
int [ ] arr = new int [ 5 ] ;
System . out. println ( arr[ 2 ] ) ;
System . out. println ( arr. length) ;
}
}
10. 数组易错地方 越界或访问空指针
package com. itheima. demo01 ;
public class HelloWorld {
public static void main ( String [ ] args) {
int [ ] arr = { 11 , 22 } ;
arr = null ;
System . out. println ( arr[ 0 ] ) ;
}
}
11. 遍历数组获取最大值
package com. itheima. demo01 ;
public class HelloWorld {
public static void main ( String [ ] args) {
int [ ] arr = { 5 , 15 , 2000 , 10000 , 100 , 4000 } ;
int max = arr[ 0 ] ;
for ( int i = 0 ; i < arr. length; i++ ) {
if ( max < arr[ i] ) {
max = arr[ i] ;
}
}
System . out. println ( max) ;
}
}
12. 类方法定义
package com. itheima. demo03_method ;
public class Demo02 {
public static void main ( String [ ] args) {
int [ ] arr = { 11 , 22 , 33 , 44 , 55 } ;
arr = null ;
printArray ( arr) ;
System . out. println ( "-----------------" ) ;
int [ ] arr2 = { 3 , 2 , 1 } ;
printArray ( arr2) ;
}
public static void printArray ( int [ ] arr) {
if ( arr== null || arr. length== 0 ) {
System . out. println ( "[]" ) ;
return ;
}
for ( int i= 0 ; i< arr. length; i++ ) {
System . out. println ( arr[ i] ) ;
}
}
}
13. 类属性的定义
13.1 定义Phone类
package com. itheima. demo04_oop ;
public class Phone {
String brand;
private double price;
String color;
public void setPrice ( double price) {
if ( price > 0 )
this . price = price;
}
public double getPrice ( ) {
return price;
}
public void call ( String name) {
System . out. println ( price) ;
System . out. println ( "给" + name + "打电话" ) ;
}
public void sendMessage ( String name) {
System . out. println ( "给" + name + "发信息" ) ;
}
}
13.2 调用类
package com. itheima. demo04_oop ;
import javax. swing. plaf. synth. SynthLookAndFeel ;
public class Demo01 {
public static void main ( String [ ] args) {
Phone p = new Phone ( ) ;
System . out. println ( p. brand) ;
System . out. println ( p. getPrice ( ) ) ;
System . out. println ( p. color) ;
System . out. println ( "---------------" ) ;
p. brand = "华为P50" ;
p. setPrice ( 3000 ) ;
p. color = "黑色" ;
System . out. println ( p. brand) ;
System . out. println ( p. getPrice ( ) ) ;
System . out. println ( p. color) ;
System . out. println ( "---------------" ) ;
p. call ( "刘亦菲" ) ;
p. sendMessage ( "高圆圆" ) ;
}
}
14. 继承
package com. itheima. demo07_extends;
public class Father {
int age = 30 ;
}
package com. itheima. demo07_extends ;
public class Son extends Father {
}
package com. itheima. demo07_extends ;
public class FatherTest {
public static void main ( String [ ] args) {
Son s = new Son ( ) ;
System . out. println ( s. age) ;
}
}
15. 类方法重写
package com. itheima. demo07_extends ;
public class Animal {
private String name;
private int age;
int num = 30 ;
public Animal ( ) {
}
public Animal ( String name, int age) {
this . name = name;
this . age = age;
}
public String getName ( ) { return name; }
public void setName ( String name) { this . name = name; }
public int getAge ( ) { return age; }
public void setAge ( int age) { this . age= age; }
public void eat ( ) {
System . out. println ( "动物会吃!" ) ;
}
}
package com. itheima. demo07_extends ;
public class Cat extends Animal {
public Cat ( ) {
super ( ) ;
}
int num = 20 ;
public void show ( ) {
int num = 10 ;
System . out. println ( num) ;
System . out. println ( this . num) ;
System . out. println ( super . num) ;
}
@Override
public void eat ( ) {
System . out. println ( "猫吃鱼!" ) ;
}
}
package com. itheima. demo07_extends ;
public class Demo1 {
public static void main ( String [ ] args) {
Cat c = new Cat ( ) ;
c. setName ( "加菲猫" ) ;
c. setAge ( 13 ) ;
System . out. println ( c. getName ( ) + ", " + c. getAge ( ) ) ;
c. eat ( ) ;
c. show ( ) ;
System . out. println ( "----------------" ) ;
}
}
16. 多态
package com. itheima. demo08_multi ;
public class Animal {
int age = 30 ;
public void eat ( ) {
System . out. println ( "动物会吃!" ) ;
}
}
package com. itheima. demo08_multi ;
public class Cat extends Animal {
int age = 20 ;
@Override
public void eat ( ) {
System . out. println ( "猫吃鱼" ) ;
}
public void catchMouse ( ) {
System . out. println ( "捉老鼠" ) ;
}
}
package com. itheima. demo08_multi ;
public class Dog extends Animal {
@Override
public void eat ( ) {
System . out. println ( "狗吃肉!" ) ;
}
}
package com. itheima. demo08_multi ;
public class Demo1 {
public static void main ( String [ ] args) {
Animal an = new Cat ( ) ;
System . out. println ( an. age) ;
an. eat ( ) ;
System . out. println ( "---------------" ) ;
Cat c = ( Cat ) an;
c. catchMouse ( ) ;
System . out. println ( c. age) ;
c. eat ( ) ;
}
}
package com. itheima. demo08_multi ;
public class Demo2 {
public static void main ( String [ ] args) {
Cat c = new Cat ( ) ;
printAnimal ( c) ;
Dog d = new Dog ( ) ;
printAnimal ( d) ;
}
public static void printAnimal ( Animal an) {
an. eat ( ) ;
}
}
17. 静态属性和final关键词
package com. itheima. demo09_final_static ;
public class Student {
String name;
static String graduateFrom = "黑马" ;
public static void show ( ) {
System . out. println ( graduateFrom) ;
}
}
package com. itheima. demo09_final_static ;
public class Demo1 {
public static void main ( String [ ] args) {
final int MAX_VALUE = 30 ;
System . out. println ( MAX_VALUE) ;
}
}
package com. itheima. demo09_final_static ;
public class Demo2 {
public static void main ( String [ ] args) {
Student . graduateFrom = "北影" ;
Student s1 = new Student ( ) ;
Student s2 = new Student ( ) ;
System . out. println ( s1. graduateFrom) ;
s1. graduateFrom = "传智播客" ;
System . out. println ( s1. graduateFrom) ;
System . out. println ( s2. graduateFrom) ;
}
}
18. 抽象类
package com. itheima. demo10_abstract ;
public abstract class Animal {
public abstract void eat ( ) ;
}
package com. itheima. demo10_abstract ;
public class Cat extends Animal {
@Override
public void eat ( ) {
System . out. println ( "猫吃鱼" ) ;
}
}
package com. itheima. demo10_abstract ;
public abstract class Dog extends Animal {
}
package com. itheima. demo10_abstract ;
public class Demo1 {
public static void main ( String [ ] args) {
Animal an = new Cat ( ) ;
an. eat ( ) ;
}
}
19. 接口,比抽象类更加抽象
package com. itheima. demo11_interface ;
public interface Flying {
public static final int age = 30 ;
public abstract void eat ( ) ;
}
package com. itheima. demo11_interface ;
public class Cat implements Flying {
@Override
public void eat ( ) {
}
}
package com. itheima. demo11_interface ;
public class Demo01 {
public static void main ( String [ ] args) {
System . out. println ( Flying . age) ;
Flying f = new Cat ( ) ;
}
}
20. 重写类的打印方法以及比较方法
package com. demo02_object ;
public class Student {
private String name;
private int age;
public Student ( ) {
}
public Student ( String name, int age) {
this . name = name;
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
@Override
public String toString ( ) {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}' ;
}
@Override
public boolean equals ( Object obj) {
if ( this == obj) return true ;
if ( obj== null || getClass ( ) != obj. getClass ( ) ) return false ;
Student student = ( Student ) obj;
if ( age != student. age) return false ;
return name != null ? name. equals ( student. name) : student. name== null ;
}
}
package com. demo02_object ;
public class Demo01 {
public static void main ( String [ ] args) {
Student s = new Student ( "刘亦菲" , 33 ) ;
System . out. println ( s. toString ( ) ) ;
System . out. println ( s) ;
System . out. println ( "-------------------" ) ;
Student s1 = new Student ( "高圆圆" , 35 ) ;
Student s2 = new Student ( "高圆圆" , 35 ) ;
boolean flag = s1. equals ( s2) ;
System . out. println ( flag) ;
}
}
21. 字符串比较和反转
package com. demo03_string ;
public class Demo01 {
public static void main ( String [ ] args) {
char [ ] chs = { 'a' , 'b' , 'c' } ;
String s1 = new String ( chs) ;
String s2 = new String ( chs) ;
String s3 = "abc" ;
String s4 = "abc" ;
System . out. println ( s1 == s2) ;
System . out. println ( s1 == s3) ;
System . out. println ( s3 == s4) ;
System . out. println ( "----------------" ) ;
System . out. println ( s1. equals ( s2) ) ;
System . out. println ( s1. equals ( s3) ) ;
System . out. println ( s3. equals ( s4) ) ;
System . out. println ( "----------------" ) ;
System . out. println ( "abc" . equals ( "ABC" ) ) ;
System . out. println ( "abc" . equalsIgnoreCase ( "ABC" ) ) ;
}
}
package com. demo03_string ;
import java. util. Scanner ;
public class Demo2 {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "请录入一个字符串: " ) ;
String s = sc. nextLine ( ) ;
System . out. println ( "反转前: " + s) ;
StringBuilder newStr = new StringBuilder ( s) ;
newStr. reverse ( ) ;
System . out. println ( newStr) ;
}
public static String reverse ( String s) {
String newStr = "" ;
char [ ] chs = s. toCharArray ( ) ;
for ( int i = chs. length - 1 ; i >= 0 ; i-- ) {
newStr += chs[ i] ;
}
return newStr;
}
}
22. 字符串遍历
package com. demo04_exercise ;
import java. util. Scanner ;
import java. util. Arrays ;
public class Demo02 {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "请录入一个字符串: " ) ;
String s = sc. nextLine ( ) ;
for ( int i = 0 ; i < s. length ( ) ; i++ ) {
char ch = s. charAt ( i) ;
System . out. println ( ch) ;
}
System . out. println ( "-------------------" ) ;
char [ ] chs = s. toCharArray ( ) ;
for ( int i = 0 ; i < chs. length; i++ ) {
char ch = chs[ i] ;
System . out. println ( ch) ;
}
System . out. println ( "-------------------" ) ;
System . out. println ( Arrays . toString ( s. toCharArray ( ) ) ) ;
}
}
23. 数组排序
package com. demo04_exercise ;
import java. util. Arrays ;
public class Demo03 {
public static void main ( String [ ] args) {
String s = "91 27 45 38 50" ;
String [ ] arrStr = s. split ( " " ) ;
int [ ] arr = new int [ arrStr. length] ;
for ( int i = 0 ; i < arrStr. length; i++ ) {
String str = arrStr[ i] ;
arr[ i] = Integer . parseInt ( str) ;
}
Arrays . sort ( arr) ;
String s2 = Arrays . toString ( arr) ;
String result = s2. substring ( 1 , s2. length ( ) - 1 ) ;
System . out. println ( result) ;
}
}
24. StringBuilder
24.1 创建StringBuilder
package com. demo05_stringbuilder ;
public class Demo01 {
public static void main ( String [ ] args) {
StringBuilder sb1 = new StringBuilder ( ) ;
StringBuilder sb2 = sb1. append ( "abc" ) ;
StringBuilder sb3 = sb1. append ( "123" ) ;
System . out. println ( "sb1: " + sb1) ;
System . out. println ( "sb2: " + sb2) ;
System . out. println ( "sb3: " + sb3) ;
System . out. println ( "-----------------------" ) ;
System . out. println ( sb1 == sb2) ;
System . out. println ( sb1 == sb3) ;
System . out. println ( sb2 == sb3) ;
System . out. println ( "-----------------------" ) ;
StringBuilder sb4 = new StringBuilder ( "我爱黑马!" ) ;
System . out. println ( "sb4: " + sb4) ;
}
}
24.2 StringBuilder和String的相互转换
package com. demo05_stringbuilder ;
public class Demo02 {
public static void main ( String [ ] args) {
String s1 = "abc" ;
StringBuilder sb1 = new StringBuilder ( s1) ;
System . out. println ( "sb1: " + sb1) ;
System . out. println ( "-----------------" ) ;
String str = sb1. toString ( ) ;
System . out. println ( "str: " + str) ;
}
}
24.3 反转字符串
package com. demo05_stringbuilder ;
import java. util. Scanner ;
public class Demo03 {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "请录入一个字符串: " ) ;
String s = sc. nextLine ( ) ;
System . out. println ( "反转前: " + s) ;
StringBuilder sb = new StringBuilder ( s) ;
sb. reverse ( ) ;
String newStr = sb. toString ( ) ;
System . out. println ( "反转后: " + newStr) ;
System . out. println ( "-------------------------" ) ;
String result = new StringBuilder ( s) . reverse ( ) . toString ( ) ;
System . out. println ( "反转后: " + result) ;
System . out. println ( "-------------------------" ) ;
}
}
25. Arrays
package com. demo06_arrays ;
import java. util. Arrays ;
public class Demo01 {
public static void main ( String [ ] args) {
int [ ] arr = { 25 , 69 , 80 , 57 , 13 } ;
Arrays . sort ( arr) ;
String s = Arrays . toString ( arr) ;
System . out. println ( "排序后: " + s) ;
}
}
26. Integer
26.1 字符串转Integer
package com. demo07_integer ;
public class Demo02 {
public static void main ( String [ ] args) {
String s = "" + 10 ;
System . out. println ( "s: " + s) ;
System . out. println ( "----------------" ) ;
String str = "1314521" ;
int num = Integer . parseInt ( str) ;
System . out. println ( "num: " + num) ;
}
}
26.2 装箱和拆箱
package com. demo07_integer ;
public class Demo03 {
public static void main ( String [ ] args) {
Integer i1 = new Integer ( 10 ) ;
Integer i2 = 10 ;
System . out. println ( "-------------------" ) ;
int a = i2. intValue ( ) ;
int b = i2;
System . out. println ( "-------------------" ) ;
Integer i3 = 10 ;
i3 += 20 ;
}
}
27. 日期时间
27.1 获取当前时间
package com. demo08_date ;
import java. util. Date ;
public class Demo01 {
public static void main ( String [ ] args) {
Date d = new Date ( ) ;
System . out. println ( d) ;
System . out. println ( d. getTime ( ) ) ;
System . out. println ( "--------------------" ) ;
Date d2 = new Date ( 1631180490798L ) ;
System . out. println ( d2) ;
System . out. println ( "--------------------" ) ;
long time = System . currentTimeMillis ( ) ;
System . out. println ( time) ;
}
}
27.2 Date和String互转
package com. demo08_date ;
import java. text. ParseException ;
import java. text. SimpleDateFormat ;
import java. util. Date ;
public class Demo02 {
public static void main ( String [ ] args) throws ParseException {
Date d = new Date ( ) ;
System . out. println ( d) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy/MM/dd HH:mm:ss" ) ;
String result = sdf. format ( d) ;
System . out. println ( result) ;
System . out. println ( "-----------------------" ) ;
String s = "2021年09月09日 17:50:02" ;
SimpleDateFormat sdf2 = new SimpleDateFormat ( "yyyy年MM月dd日 HH:mm:ss" ) ;
Date d2 = sdf2. parse ( s) ;
System . out. println ( d2) ;
}
}
package com. demo08_date ;
import java. text. ParseException ;
import java. text. SimpleDateFormat ;
import java. util. Date ;
public class Demo03 {
public static void main ( String [ ] args) throws ParseException {
Date d = new Date ( ) ;
String s1 = DateUtils . date2String ( d, "yyyy/MM/dd HH:mm:ss" ) ;
System . out. println ( s1) ;
System . out. println ( "-----------------------" ) ;
String s = "2021年09月09日 17:50:02" ;
Date d2 = DateUtils . string2Date ( s, "yyyy年MM月dd日 HH:mm:ss" ) ;
System . out. println ( d2) ;
}
}
27.3 Calendar对象
package com. demo08_date ;
import java. util. Calendar ;
public class Demo04 {
public static void main ( String [ ] args) {
Calendar c = Calendar . getInstance ( ) ;
System . out. println ( c. get ( Calendar . YEAR) ) ;
System . out. println ( c. get ( Calendar . MONTH) ) ;
System . out. println ( c. get ( Calendar . DATE) ) ;
System . out. println ( c. get ( Calendar . DAY_OF_MONTH) ) ;
System . out. println ( c. get ( Calendar . DAY_OF_YEAR) ) ;
System . out. println ( "-----------------------------" ) ;
}
}
28. 异常处理
package com. demo09_exception ;
import java. text. ParseException ;
import java. text. SimpleDateFormat ;
import java. util. Date ;
public class Demo01 {
public static void main ( String [ ] args) {
try {
show ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
}
}
public static void show ( ) throws ParseException {
SimpleDateFormat sdf2 = new SimpleDateFormat ( "yyyy年MM月dd日 HH:mm:ss" ) ;
String s1 = "2021年09月09日 17:50:02" ;
String s2 = "2021年09月09日" ;
Date d1 = sdf2. parse ( s2) ;
System . out. println ( d1) ;
}
}