欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

✔《Java编程思想》(第4版)第2章 习题

程序员文章站 2022-05-07 17:22:52
...

练习1

public class Test {
    static int a;
    static char b;

    public static void main(String[] args) {

        System.out.println("a: " + a);
        System.out.println("b: " + b);
    }
    
}

练习2

public class Test {

    public static void main(String[] args) {

        System.out.println("Hello, World!");
    }

}

练习3

public class Test {

    public static void main(String[] args) {

        ATypeName name = new ATypeName();
        System.out.println(name.a);
    }

}

class ATypeName {
    public int a = 123;
}

练习4

public class Test {

    public static void main(String[] args) {

        DataOnly only = new DataOnly();
        System.out.println(only.a + only.b);
    }

}

class DataOnly {
    int a = 123;
    int b = 321;
}

练习5

public class Test {

    public static void main(String[] args) {

        DataOnly only = new DataOnly();
        System.out.println(only.a + only.b);
    }

}

class DataOnly {
    int a = 2;
    int b = 4;
}

练习6

public class Test {

    public static void main(String[] args) {
        Test test = new Test();
        System.out.println(test.storage("abc"));
    }

    public int storage(String s) {
        return s.length() * 2;
    }
}

练习7

public class Test {

    public static void main(String[] args) {
        System.out.println("i: " + StaticTest.i);
        Incrementable.increment();
        System.out.println("i: " + StaticTest.i);
    }

}

class StaticTest {
    static int i = 47;
}

class Incrementable {
    static void increment() {
        StaticTest.i++;
    }
}

练习8

public class Test {
    static int a = 100;

    public static void main(String[] args) {
        Test test1 = new Test();
        Test test2 = new Test();
        Test test3 = new Test();

        System.out.println("a: " + a);
        test1.a--;
        test2.a--;
        System.out.println("a: " + a);
    }

}

练习9

public class Test {

    public static void main(String[] args) {
        boolean b = true;
        System.out.println("boolean b: " + b);
        Boolean B = b;
        System.out.println("Boolean B: " + B);

        char c = 'a';
        System.out.println("char c: " + c);
        Character C = c;
        System.out.println("Character C: " + C);

        byte t = 9;
        System.out.println("byte t: " + t);
        Byte T = t;
        System.out.println("Byte T: " + T);

        short s = 15;
        System.out.println("short s: " + s);
        Short S = s;
        System.out.println("Short S: " + S);

        int i = 99;
        System.out.println("int i: " + i);
        Integer I = i;
        System.out.println("Integer I: " + I);

        long l = 10101;
        System.out.println("long l: " + l);
        Long L = l;
        System.out.println("Long L: " + L);

        float f = 0.3244f;
        System.out.println("float f: " + f);
        Float F = f;
        System.out.println("Float F: " + F);

        double d = 0.6432;
        System.out.println("double d: " + d);
        Double D = d;
        System.out.println("Double D: " + D);
    }

}

练习10

public class Test {

    public static void main(String[] args) {
        for (String s : args)
            System.out.println(s);
    }
}

练习11

public class Test {

    public static void main(String[] args) {
        AllTheColorsOfTheRainbow cc = new AllTheColorsOfTheRainbow();
        cc.changeTheHueOfTheColor(cc.anIntegerRepresentingColors);
    }
}

class AllTheColorsOfTheRainbow {
    int anIntegerRepresentingColors;

    void changeTheHueOfTheColor(int newHue) {
        System.out.println(newHue);
    }
}
相关标签: Java