数据类型

基本数据类型
Java基本数据类型分为四大类:字符型(char)、整型(byte、short、int、long)、浮点型(float、double)、布尔型(boolean)。
取值范围

public static void getBaseDataTypeRange(){
        System.out.println("byte取值范围:[" + Byte.MIN_VALUE + "," + Byte.MAX_VALUE + "]");
        System.out.println("short取值范围:[" + Short.MAX_VALUE + "," + Short.MAX_VALUE + "]");
        System.out.println("int取值范围:[" + Integer.MIN_VALUE + ","+ Integer.MAX_VALUE + "]");
        System.out.println("long取值范围:[" + Long.MIN_VALUE + "," + Long.MAX_VALUE + "]");
        System.out.println("float取值范围:[" + Float.MIN_VALUE + "," + Float.MAX_VALUE + "]");
        System.out.println("double取值范围:[" + Double.MIN_VALUE + "," + Double.MAX_VALUE + "]");
        System.out.println("char取值范围:[" + Character.MIN_VALUE + "," + Character.MAX_VALUE + "]");
        System.out.println("boolean取值范围:只能是" + Boolean.TRUE + "或" + Boolean.FALSE);
    }

运行结果:

byte取值范围:[-128,127]
short取值范围:[32767,32767]
int取值范围:[-2147483648,2147483647]
long取值范围:[-9223372036854775808,9223372036854775807]
float取值范围:[1.4E-45,3.4028235E38]
double取值范围:[4.9E-324,1.7976931348623157E308]
char取值范围:[ ,]
boolean取值范围:只能是true或false

封装类
封装类可以用来进行数据类型转换。

public static void getDataTypeConvert(){
       String str = "123";
       int intValue = Integer.valueOf(str);
       System.out.println("String转int的值:" + intValue);
       float floatValue = Float.valueOf(str);
       System.out.println("String转float的值:" + floatValue);
       long longValue = Long.valueOf(str);
       System.out.println("String转long的值:" + longValue);
    }

运行结果:

String转int的值:123
String转float的值:123.0
String转long的值:123

自增自减
i++:先运算,后加1
++i:先加1,后运算
i–:先运算,后减1
–i:先减1,后运算

public static void selfIncrease(){
        int i = 5;
        int j = 5;
        int k = i++ * ++j;
        System.out.println("自增:i=" + i + ",j=" + j + ",k=" + k);
        i = 7;
        j = 7;
        k = i-- * --j;
        System.out.println("自减:i=" + i + ",j=" + j + ",k=" + k);
    }

运算结果:

自增:i=6,j=6,k=30
自减:i=6,j=6,k=42

三目运算符

public static void getScoreGarden(){
        int score = 90;
        System.out.println("分数为:" + score + ",等级:" + (score < 60 ? "不合格" : "合格"));
    }

运行结果:

分数为:90,等级:合格

switch语句
switch里可以用char,byte,short,int这些基本类型,以及它们的封装类和字符串类型、枚举类型。

public class TestBaseDataType {

    public static void main(String[] args){
        //基本数据类型
        char c = 'A';
        testCharSwitch('A');
        byte b = 2;
        testByteSwitch(b);
        short s = 1;
        testShortSwitch(s);
        int i = 1;
        testIntSwitch(i);

        //封装类型
        Character character = new Character('a');
        testCharacterSwitch(character);
        Byte by = new Byte(b);
        testByteSwitch(by);
        Short sh = new Short(s);
        testShortSwitch(sh);
        Integer integer = new Integer(i);
        testIntegerSwitch(integer);

        //字符串类型
        String str = "*";
        testStringSwitch(str);

        //枚举类型
        DataType dataType = DataType.CHAR;
        testEnumSwitch(dataType);
    }

    private static void testCharSwitch(char c){
        switch(c){
            case 'A':
            case 'B':
                System.out.println("char类型,值为:" + c);
                break;
             default:
                 System.out.println("char类型,值为默认值。");
        }
    }

    private static void testByteSwitch(byte b){
        switch(b){
            case 1:
            case 2:
                System.out.println("byte类型,值为:" + b);
                break;
            default:
                System.out.println("byte类型,值为默认值。");
        }
    }

    private static void testShortSwitch(short s){
        switch(s){
            case 1:
            case 2:
                System.out.println("short类型,值为:" + s);
                break;
            default:
                System.out.println("short类型,值为默认值。");
        }
    }

    private static void testIntSwitch(int i){
        switch(i){
            case 1:
            case 2:
                System.out.println("int类型,值为:" + i);
                break;
            default:
                System.out.println("int类型,值为默认值。");
        }
    }

    private static void testCharacterSwitch(Character c){
        switch (c){
            case 'A':
            case 'B':
                System.out.println("Character类型,值为:" + c);
                break;
            default:
                System.out.println("Character类型,值为默认值。");
        }
    }

    private static void testByteSwitch(Byte b){
        switch(b){
            case 1:
            case 2:
                System.out.println("Byte类型,值为:" + b);
                break;
            default:
                System.out.println("Byte类型,值为默认值。");
        }
    }

    private static void testShortSwitch(Short s){
        switch(s){
            case 1:
            case 2:
                System.out.println("Short类型,值为:" + s);
                break;
            default:
                System.out.println("Short类型,值为默认值。");
        }
    }

    private static void testIntegerSwitch(Integer i){
        switch(i){
            case 1:
            case 2:
                System.out.println("Integer类型,值为:" + i);
                break;
            default:
                System.out.println("Integer类型,值为默认值。");
        }
    }

    private static void testStringSwitch(String str){
        String type;
        switch (str){
            case "a":
                type = "字符串";
                break;
            case "1":
                type = "数字";
                break;
            case "*":
                type = "特殊字符";
                break;
             default:
                type = "未知";
        }

        System.out.println(str + "的数据类型为" + type);
    }

    private static void testEnumSwitch(DataType dataType){
        String type;
        switch(dataType){
            case CHAR:
                type = "char";
                break;
            case INT:
                type = "int";
                break;
            case DOUBLE:
                type = "double";
                break;
            case BOOLEAN:
                type = "boolean";
                break;
            default:
                type = "未知";
        }

        System.out.println(dataType + "的数据类型为" + type);
    }
}

enum DataType {
    CHAR,
    INT,
    DOUBLE,
    BOOLEAN
}

运行结果:

char类型,值为:A
byte类型,值为:2
short类型,值为:1
int类型,值为:1
Character类型,值为默认值。
Byte类型,值为:2
Short类型,值为:1
Integer类型,值为:1
*的数据类型为特殊字符
CHAR的数据类型为char

面向对象
封装、继承和多态是面向对象的三大特征。
在方法内部定义的变量会在方法删除后被删除,如果在方法内部有需要的值,可以通过return来返回。
访问控制符访问范围
private:同类
default:同类、同包
protected:同类、同包、子类
public:同类、同包、子类、不同包
类是一个抽象的概念,而对象则是类的实例。

练习题

  1. 请说说&和&&以及|和||的区别。
    &和|是位运算符,所有的判断条件都要执行,而&&和||是逻辑运算符,一般用在条件判断中,&&若前面的判断条件为false,则后面的条件不会执行,||若前面的条件为true,则后面的条件不会执行。
public static void main(String[] args){
        int i = 5;
        int j = 5;
        int k = 0;

        //&和&&的区别
        if((i < j) & (k++ > 0)){

        }
        System.out.println("&普通与运算:" + k);

        k = 0;
        if((i < j) && ( k++ > 0)){

        }
        System.out.println("&&短路与运算:" + k);

        //&和&&的区别
        k = 0;
        if((i == j) | (k++ > 0)){

        }
        System.out.println("|普通或运算:" + k);

        k = 0;
        if((i == j) || ( k++ > 0)){

        }
        System.out.println("||短路或运算:"+k);
    }

运行结果:

&普通与运算:1
&&短路与运算:0
|普通或运算:1
||短路或运算:0
  1. ==和equals的区别。
    ==用来比较基本数据类型的值是否相等,对于封装类型,==是比较它们在内存中存放的地址是否一致,而equals方法才是比较它们的值是否相等。
public static void main(String[] args){
        int x = 7;
        int y = 7;
        System.out.println("基本数据类型比较(==):" + (x == y ? "x和y相等" : "x和y不相等"));

        Integer m = new Integer(7);
        Integer n = new Integer(7);
        System.out.println("封装类型比较(==):" + (m == n ? "m和n相等" : "m和n不相等"));
        System.out.println("封装类型比较(equals):" + (m.equals(n) ? "m和n相等" : "m和n不相等"));
    }

运行结果:

基本数据类型比较(==):x和y相等
封装类型比较(==):m和n不相等
封装类型比较(equals):m和n相等
  1. 判断输入的年份是否是闰年。
    判断闰年的条件:一、能被4整除,并且不能被100整除;二、能被400整除。
public static void main(String[] args){
        int year = 2019;

        boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
        if(isLeapYear){
            System.out.println(year+"年是闰年。");
        }else{
            System.out.println(year+"年不是闰年。");
        }
    }

运行结果:

2019年不是闰年。