21xrx.com
2024-12-23 02:35:16 Monday
登录
文章检索 我的文章 写文章
《Java编程思想》- 代码案例分享
2023-06-14 22:34:26 深夜i     --     --
Java编程思想 代码案例 入门自学

    // Arithmetic operators:

    //! x = x * y;

    //! x = x / y;

    //! x = x % y;

    //! x = x + y;

    //! x = x - y;

    //! x++;

    //! x--;

    //! x = +y;

    //! x = -y;

    // Relational and logical:

    //! f(x > y);

    //! f(x >= y);

    //! f(x < y);

    //! f(x <= y);

    f(x == y);

    f(x != y);

    f(!y);

    x = x && y;

    x = x || y;

    // Bitwise operators:

    //! x = ~y;

    x = x & y;

    x = x | y;

    x = x ^ y;

    //! x = x << 1;

    //! x = x >> 1;

    //! x = x >>> 1;

    // Compound assignment:

    //! x += y;

    //! x -= y;

    //! x *= y;

    //! x /= y;

    //! x %= y;

    //! x <<= 1;

    //! x >>= 1;

    //! x >>>= 1;

    x &= y;

    x ^= y;

    x |= y;

    // Casting:

    //! char c = (char)x;

    //! byte b = (byte)x;

    //! short s = (short)x;

    //! int i = (int)x;

    //! long l = (long)x;

    //! float f = (float)x;

    //! double d = (double)x;

对于想要自学Java的初学者们来说,选择一本好的入门书籍非常重要。而在市面上各种Java入门书籍中,《Java编程思想》无疑是备受推崇的经典之作。本文将着重介绍该书的特点和代码案例,帮助大家更好地学习Java。

首先,《Java编程思想》是Bruce Eckel所著的Java编程指南。这本书被誉为是Java普及的一大贡献,是Java编程领域中极具权威性的经典著作。这本书全面而深入地介绍了Java编程的基础知识,从Java语言的基础开始讲起,通过诸多代码案例的讲解,深入浅出地帮助读者理解概念和思路。

《Java编程思想》的每一章内容都遵循“先理解概念,再进行代码案例”的原则,每一章都有很多实例代码以及注释解释,对于初学者非常友好。除此之外,该书还介绍了Java核心技术,包括多线程、容器、异常处理、泛型、反射等,内容十分全面。

接下来,我们就以书中的一个代码案例来说明其教学风格:


//: operators/AllOps.java

// Tests all the operators on all the primitive data types

// to show which ones are accepted by the Java compiler.

public class AllOps {

  void f(boolean b) {}

  void boolTest(boolean x, boolean y) {

    // Arithmetic operators:

    //! x = x * y;

    //! x = x / y;

    //! x = x % y;

    //! x = x + y;

    //! x = x - y;

    //! x++;

    //! x--;

    //! x = +y;

    //! x = -y;

    // Relational and logical:

    //! f(x > y);

    //! f(x >= y);

    //! f(x < y);

    //! f(x <= y);

    f(x == y);

    f(x != y);

    f(!y);

    x = x && y;

    x = x || y;

    // Bitwise operators:

    //! x = ~y;

    x = x & y;

    x = x | y;

    x = x ^ y;

    //! x = x << 1;

    //! x = x >> 1;

    //! x = x >>> 1;

    // Compound assignment:

    //! x += y;

    //! x -= y;

    //! x *= y;

    //! x /= y;

    //! x %= y;

    //! x <<= 1;

    //! x >>= 1;

    //! x >>>= 1;

    x &= y;

    x ^= y;

    x |= y;

    // Casting:

    //! char c = (char)x;

    //! byte b = (byte)x;

    //! short s = (short)x;

    //! int i = (int)x;

    //! long l = (long)x;

    //! float f = (float)x;

    //! double d = (double)x;

  }

  void charTest(char x, char y) {

    // Arithmetic operators:

    x = (char)(x * y);

    x = (char)(x / y);

    x = (char)(x % y);

    x = (char)(x + y);

    x = (char)(x - y);

    x++;

    x--;

    x = (char)+y;

    x = (char)-y;

    // Relational and logical:

    f(x > y);

    f(x >= y);

    f(x < y);

    f(x <= y);

    f(x == y);

    f(x != y);

    //! f(!x);

    //! f(x && y);

    //! f(x || y);

    // Bitwise operators:

    x = (char)~y;

    x = (char)(x & y);

    x = (char)(x | y);

    x = (char)(x ^ y);

    x = (char)(x << 1);

    x = (char)(x >> 1);

    x = (char)(x >>> 1);

    // Compound assignment:

    x += y;

    x -= y;

    x *= y;

    x /= y;

    x %= y;

    x <<= 1;

    x >>= 1;

    x >>>= 1;

    x &= y;

    x ^= y;

    x |= y;

    // Casting:

    //! boolean bl = (boolean)x;

    byte b = (byte)x;

    short s = (short)x;

    int i = (int)x;

    long l = (long)x;

    float f = (float)x;

    double d = (double)x;

  }

  void byteTest(byte x, byte y) {

    // Arithmetic operators:

    x = (byte)(x * y);

    x = (byte)(x / y);

    x = (byte)(x % y);

    x = (byte)(x + y);

    x = (byte)(x - y);

    x++;

    x--;

    x = (byte)+ y;

    x = (byte)- y;

    // Relational and logical:

    f(x > y);

    f(x >= y);

    f(x < y);

    f(x <= y);

    f(x == y);

    f(x != y);

    //! f(!x);

    //! f(x && y);

    //! f(x || y);

    // Bitwise operators:

    x = (byte)~y;

    x = (byte)(x & y);

    x = (byte)(x | y);

    x = (byte)(x ^ y);

    x = (byte)(x << 1);

    x = (byte)(x >> 1);

    x = (byte)(x >>> 1);

    // Compound assignment:

    x += y;

    x -= y;

    x *= y;

    x /= y;

    x %= y;

    x <<= 1;

    x >>= 1;

    x >>>= 1;

    x &= y;

    x ^= y;

    x |= y;

    // Casting:

    //! boolean bl = (boolean)x;

    char c = (char)x;

    short s = (short)x;

    int i = (int)x;

    long l = (long)x;

    float f = (float)x;

    double d = (double)x;

  }

  void shortTest(short x, short y) {

    // Arithmetic operators:

    x = (short)(x * y);

    x = (short)(x / y);

    x = (short)(x % y);

    x = (short)(x + y);

    x = (short)(x - y);

    x++;

    x--;

    x = (short)+y;

    x = (short)-y;

    // Relational and logical:

    f(x > y);

    f(x >= y);

    f(x < y);

    f(x <= y);

    f(x == y);

    f(x != y);

    //! f(!x);

    //! f(x && y);

    //! f(x || y);

    // Bitwise operators:

    x = (short)~y;

    x = (short)(x & y);

    x = (short)(x | y);

    x = (short)(x ^ y);

    x = (short)(x << 1);

    x = (short)(x >> 1);

    x = (short)(x >>> 1);

    // Compound assignment:

    x += y;

    x -= y;

    x *= y;

    x /= y;

    x %= y;

    x <<= 1;

    x >>= 1;

    x >>>= 1;

    x &= y;

    x ^= y;

    x |= y;

    // Casting:

    //! boolean bl = (boolean)x;

    char c = (char)x;

    byte b = (byte)x;

    int i = (int)x;

    long l = (long)x;

    float f = (float)x;

    double d = (double)x;

  }

  void intTest(int x, int y) {

    // Arithmetic operators:

    x = x * y;

    x = x / y;

    x = x % y;

    x = x + y;

    x = x - y;

    x++;

    x--;

    x = +y;

    x = -y;

    // Relational and logical:

    f(x > y);

    f(x >= y);

    f(x < y);

    f(x <= y);

    f(x == y);

    f(x != y);

    //! f(!x);

    //! f(x && y);

    //! f(x || y);

    // Bitwise operators:

    x = ~y;

    x = x & y;

    x = x | y;

    x = x ^ y;

    x = x << 1;

    x = x >> 1;

    x = x >>> 1;

    // Compound assignment:

    x += y;

    x -= y;

    x *= y;

    x /= y;

    x %= y;

    x <<= 1;

    x >>= 1;

    x >>>= 1;

    x &= y;

    x ^= y;

    x |= y;

    // Casting:

    //! boolean bl = (boolean)x;

    char c = (char)x;

    byte b = (byte)x;

    short s = (short)x;

    long l = (long)x;

    float f = (float)x;

    double d = (double)x;

  }

  void longTest(long x, long y) {

    // Arithmetic operators:

    x = x * y;

    x = x / y;

    x = x % y;

    x = x + y;

    x = x - y;

    x++;

    x--;

    x = +y;

    x = -y;

    // Relational and logical:

    f(x > y);

    f(x >= y);

    f(x < y);

    f(x <= y);

    f(x == y);

    f(x != y);

    //! f(!x);

    //! f(x && y);

    //! f(x || y);

    // Bitwise operators:

    x = ~y;

    x = x & y;

    x = x | y;

    x = x ^ y;

    x = x << 1;

    x = x >> 1;

    x = x >>> 1;

    // Compound assignment:

    x += y;

    x -= y;

    x *= y;

    x /= y;

    x %= y;

    x <<= 1;

    x >>= 1;

    x >>>= 1;

    x &= y;

    x ^= y;

    x |= y;

    // Casting:

    //! boolean bl = (boolean)x;

    char c = (char)x;

    byte b = (byte)x;

    short s = (short)x;

    int i = (int)x;

    float f = (float)x;

    double d = (double)x;

  }

  void floatTest(float x, float y) {

    // Arithmetic operators:

    x = x * y;

    x = x / y;

    x = x % y;

    x = x + y;

    x = x - y;

    x++;

    x--;

    x = +y;

    x = -y;

    // Relational and logical:

    f(x > y);

    f(x >= y);

    f(x < y);

    f(x <= y);

    f(x == y);

    f(x != y);

    //! f(!x);

    //! f(x && y);

    //! f(x || y);

    // Bitwise operators:

    //! x = ~y;

    //! x = x & y;

    //! x = x | y;

    //! x = x ^ y;

    //! x = x << 1;

    //! x = x >> 1;

    //! x = x >>> 1;

    // Compound assignment:

    x += y;

    x -= y;

    x *= y;

    x /= y;

    x %= y;

    // x <<= 1;

    // x >>= 1;

    // x >>>= 1;

    // x &= y;

    // x ^= y;

    // x |= y;

    // Casting:

    //! boolean bl = (boolean)x;

    char c = (char)x;

    byte b = (byte)x;

    short s = (short)x;

    int i = (int)x;

    long l = (long)x;

    double d = (double)x;

  }

  void doubleTest(double x, double y) {

    // Arithmetic operators:

    x = x * y;

    x = x / y;

    x = x % y;

    x = x + y;

    x = x - y;

    x++;

    x--;

    x = +y;

    x = -y;

    // Relational and logical:

    f(x > y);

    f(x >= y);

    f(x < y);

    f(x <= y);

    f(x == y);

    f(x != y);

    //! f(!x);

    //! f(x && y);

    //! f(x || y);

    // Bitwise operators:

    //! x = ~y;

    //! x = x & y;

    //! x = x | y;

    //! x = x ^ y;

    //! x = x << 1;

    //! x = x >> 1;

    //! x = x >>> 1;

    // Compound assignment:

    x += y;

    x -= y;

    x *= y;

    x /= y;

    x %= y;

    // x <<= 1;

    // x >>= 1;

    // x >>>= 1;

    // x &= y;

    // x ^= y;

    // x |= y;

    // Casting:

    //! boolean bl = (boolean)x;

    char c = (char)x;

    byte b = (byte)x;

    short s = (short)x;

    int i = (int)x;

    long l = (long)x;

    float f = (float)x;

  }

  public static void main(String[] args) {

    AllOps ops = new AllOps();

    ops.boolTest(true, false);

    ops.charTest('a', 'b');

    ops.byteTest((byte)1, (byte)2);

    ops.shortTest((short)1, (short)2);

    ops.intTest(1, 2);

    ops.longTest(1L, 2L);

    ops.floatTest(1.0f, 2.0f);

    ops.doubleTest(1.0, 2.0);

  }

} ///:~

该代码案例展示了Java中各种基本数据类型的可用操作符情况,既是一个非常好的Java语言入门案例,同时也是其他高级Java课程的理论基础。本案例充分体现了该书著者深入浅出、通俗易懂的教学风格,读者可以通过这份代码案例让自己更好地理解Java基本数据类型的运算和类型转换。

综上所述,《Java编程思想》是一本非常优秀的Java自学书籍,其代码案例的丰富性和深入性对于Java入门学习者来说是一大福利。读者可以在这本书的帮助下,快速掌握Java编程的基本知识和核心技术。再次推荐给想要自学Java的初学者们。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复