开发者

Is it necessary to double define data type?

In the code....

NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
开发者_JAVA技巧

is it actually necessary to define the data type twice?


I would argue that you aren't defining it twice, but defining it once, and then casting a value/object to a specific type. In some cases it is necessary to do this.

My point is there is a difference between definition and casting.


In that case, yes. getSystemService return an Object instance, that you must cast to whatever you need (in this case NotificationManager)


No, you should only need to define it once, like:

NotificationManager mNotificationManager = getSystemService(ns);

But ensure that getSystemService has a return type of NotificationManager or one of its sub-classes.


You aren't declaring the type 2x. in the line,

NotificationManager nm = (NotificationManager)getSystemService(...);

You have declared a variable of type NotificationManager nm, and cast the return type of getSystemService(), which is Object, to type NotificationManager, thereby making the assignment to nm legal. Note, you could do this without the cast,

Object obj = getSystemService(...);

But you can't use obj as anything other an Object unless you cast it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜