Java-之一

Early Binding vs. Late Binding

The function call generated by a non-OOP compiler causes early binding. It means the compiler generates a call to a specific function name, and the linker resolves this call to the absolute address of the code to be executed.

OOP use the concept of late binding. When you send a message to an object, the code being called is not determined until run time. The compiler only ensures that the method exists and performs type checking on the arguments and return value, but it does not know the exact code to execute.

Java allow late binding in constructor, while C++ does NOT allow this. However, you can NOT call late binding method in the constructor, because this is very dangerous since you can access uninitialized fields in the derived class.

boolean

Note that a boolean value is automatically converted to an appropriate text form if it is used where a String is expected.

Java allows you to cast any primitive type to any other primitive type, except forboolean, which does not allow any casting at all.

Scoping

Note that you cannot do the following, even though it is legal in C and C++:

{

    int x = 10;

    {

        int x = 20;

    }

}

The compiler will announce that the variable x has already been defined. Thus the C and C++ ability to hide a variable in a larger scope is not allowed in Java.

Shift Operators

>>, signed right shift, if the lvalue is positive, zeroes are inserted at the higher-order bits; if the lvalue is negative, ones are inserted at the higher-order bits.

>>>, unsigned right shift , which uses zero extension.

If you shift a char, byte, or short, it will be promoted toint before the shift takes place, and the result will be an int.

If you are operating on int, only the five low-order bits of the rvalue will be used, which means you can only shift 2^5-1=31 bits.

If you are operating on a long, only the six low-order bits of the rvalue will be used,, which means you can only shift 2^6-1=63 bits.

Array Initialization

Note that the default initialization of array not only applies to member array of a class, but also apply to local array.

Java allows to assign one array to another. However, they both refer to the same array. This is because array is also object in Java.

You can create an array of primitives. Again, the compiler guarantees initialization because it zeroes the memory for that array.

int[] a1 = {1,2,3,4,5,6};

int[] a2 = new int[6];

int a3[ ];

int[]  a4;

When you create an array of objects, you are really creating an array of references, and each of those references is automatically initialized to null. If you want to explicitly initialize an array of objects, you must always use new.It is also possible to initialize arrays of objects using the curly-brace enclosed list.

Integer[] a = new Integer[] { new Integer(1), new Integer(2), new Integer(3) };

Integer[] b = { new Integer(1), new Integer(2), new Integer(3) };

Integer[] c = new Integer[3];

Integer d[];

Primitives implicit convert - only upgrade, NO downgrade

A primitive can be automatically promoted to a wider one.

1. const numeric value is treated as int or double.

2. char is promoted to int.

If argument is wider, then you must cast to the necessary type. If you don’t do this, the compiler will issue an error message.

Static

Static can ONLY be used with data member and member method, you can NOT declare a static variable in a method or even in the static block.

Package

If two packages are imported and they include classes with the same name, then collision occurs. You can specify the package to solve this problem:

mypackage.TestClass obj = new mypackage.TestClass( );

yourpackage.TestClass obj = new yourpackage.TestClass( );

There can be ONLY one public class inside the compilation unit and the public class must have the same name as the file including capitalization. 

If there are additional classes in that compilation unit, they are hidden from the world outside that package because they are not public.

It is possible, though not typical, to have a compilation unit without public class at all. In this case, you can name the file whatever you like.

Class

Java disallow reducing the accessibility of a method during inheritance.

private < [package access] < protected < public

Constructors

(1) You can NOT call a constructor inside any method other than a constructor.

(2) Although you can call one constructor using this, you can NOT call two constructors.

(3) The constructor call must be the first thing you do, or you will get a compiler error message.

super & this

1)super( )必须写在子类构造方法的第一行。如果不写,则是隐含地调用super( ),如果父类没有这种形式的构造函数,那么在编译的时候就会报错。

2)this( )也必须要放在构造方法的第一行,所以不能同时出现在一个构造函数里面。

3)this和super都是对象,所以均不可以在static环境中使用。包括:static变量,static方法,static语句块。

4)从本质上讲,this是本对象的一个引用, 然而super是一个关键字。

final

With using with a primitive, final makes the value a constant, but with an object reference, final makes the reference a constant. Once the reference is initialized to an object, it can never be changed to point to another object. However, the object itself can be modified; Java does not provide a way to make any arbitrary object a constant.

也就是说Java里面只有C++中的指针常量(指针是常量,int*const ptr;),而没有常量指针(常量的指针,constint *ptr; intconst *ptr;)。

引用常量:引用时常量, int &const r;// error,因为引用本身就是一个常量

常量引用:常量的引用,const int &r;int const &r;













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值