java编程思想学习笔记(一)

本文探讨了Java相较于C++的简化之处,并深入讨论了Java中的运算符使用限制、浮点数比较严格性等问题。此外,还介绍了Java中初始化与清理的概念,包括构造函数调用规则、垃圾回收机制及静态初始化的特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java=++--(C plus plus minus minus),suggesting that Java is C++ with the unnecessary hard parts removed ,and therefore a much simpler language . You will see that many parts are simpler ,and yet in other ways Java isn’t much easier than C++

 

Operators

You can’t use a non-boolean as if it were a logical expression as you can in C and C++.

eg:     int a =1;

       int b=2;

       System.out.println("a || b is "+(a||b));

 

Be aware, however ,that the comparison of floating point numbers is very strict. A number that is the tiniest fraction different from another number is still “not equal.“ A number that is the tiniest bit above zero is still nonzero.

 

If you are used to thinking in terms of e as the base of natural logarithms, you must do a mental translation when you see an expression such as 1.39 e-43f in java;it means 1.39*10-43

 

If you shift a char, byte ,or short ,it will be promoted to int before the shift takes place, and the result will be an int . Only the five low-order bits of the right-hand side will be used .This prevents you from shifting more than the number of bits in an int. If you’re operating on a long ,you’ll get a long result .Only the six low-order bits of the right-hand side will be used ,so you can’t shift more than the number of bits in a long.

 

Shifts can be combined with the equal sign(<<= or >>= or >>>=)

 

Although goto is a reserved word in Java ,it is not used in the language; Java has no goto. However, it does have something that looks a bit like a jump tied in with the break and continue keywords .It’s not a jump but rather a way to break out of an iteration statement. The reason it’s often thrown in with discussions of goto is because it uses the same mechanism: a label.

It’s important to remember that the only reason to use labels in Java is when you have nested loops and you want to break or continue through more than one nested level.

 

Initialization & Cleanup

While you can call one constructor using this, you cannot call two. In addition, the constructor call must be the first thing you do, or you’ll get a compiler error message.

 

The compiler won’t let you call a constructor from inside and method other than a constructor.

 

Neither garbage collection nor finalization is guaranteed. If the JVM isn’t close to running out of memory, then it might not waste time recovering memory through garbage collection.

 

public class StaticInitialization {

 

    public static void main(String[] args) {

       System.out.println("Creating new Cupboard() in main");

       new Cupboard();

       System.out.println("Ctreating new Cupboard() in main");

       new Cupboard();

       table.f2(1);

       cupboard.f3(1);

    }

   

    static Table table=new Table();

    static Cupboard cupboard=new Cupboard();

}

 

class Bowl{

   

    Bowl(int marker){

       System.out.println("Bowl("+ marker+")");

    }

    void f1(int marker){

        System.out.println("f1("+marker+")");

    }

}

 

class Table{

    static Bowl bowl1=new Bowl(1);

    Table(){

       System.out.println("Table()");

       bowl2.f1(1);

    }

    void f2(int marker){

       System.out.println("f2("+marker+")");

    }

    static Bowl bowl2=new Bowl(2);

}

 

class Cupboard{

    Bowl bowl3=new Bowl(3);

    static Bowl bowl4=new Bowl(4);

    Cupboard(){

       System.out.println("Cupboard()");

       bowl4.f1(2);

    }

    void f3(int marker){

       System.out.println("f3("+marker+")");

    }

    static Bowl bowl5=new Bowl(5);

}

 

output:

Bowl(1)

Bowl(2)

Table()

f1(1)

Bowl(4)

Bowl(5)

Bowl(3)

Cupboard()

f1(2)

Creating new Cupboard() in main

Bowl(3)

Cupboard()

f1(2)

Ctreating new Cupboard() in main

Bowl(3)

Cupboard()

f1(2)

f2(1)

f3(1)

 

From the output ,you can see that the static initialization occurs only if it’s necessary. If you don’t create a Table object and you never refer to Table.bowl1 or Table.bowl2,the static Bowl bowl1 and bowl2 will never be created. They are initialized only when the first Table object is created(or the first Static access occurs).After that, the static objects are not reinitialized.

                                               

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值