This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question

 
Hans
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)Anonymous Inner can implement an interface, extend a nonstatic class(but not at same time).True or false?Why?How to complete?
2)
final int i=2;
byte b=i;
the compile is ok.Why do not need explicit cast?
Thank you!

 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3776
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True. What do you mean by "how to complete"???
2)
Refer to: http://enthuware.com/jqplus/FAJQuestions.html
HTH,
Paul.
------------------
Get Certified, Guaranteed!
http://pages.about.com/jqplus
 
scott irwin
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An anonymous inner class either implements an interface OR extends an object. You lost me on the non-static class part. An anonymous class is either static or non-static based on the CONTEXT of the declaration (if in a static method it is a static inner class). The access rules for member and local variables are governed like any other inner class.
To construct an extended object
new TheObject([any existing constructor args]) {};
You can't specify your own constructor, so if you need to set additional member variables you can specify an instance initializer, but not your own constructor.
To construct an interface
new InterfaceName ([no arguments allowed!]) {};
The object extends Object.
Question 2....
An implicit cast is done for you if and only if the right side is a type of INT and it's value is known at compile time (i.e., final) and the value is in range of the left type.
So you have a final int=2 being assigned to a byte. Remove the final and error, change to short and error, change to 128 and error.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3776
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Small glitch there, Scott. Implicit narrowing will occur for byte, char, short and int (not just for int).
-Paul.


------------------
Get Certified, Guaranteed!
http://enthuware.com/jqplus
 
scott irwin
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right byte, short and char on the left, and an int on the right for assignment. int to int doesn't need cast.
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It�s a tricky question.
Here Thomas said:
�JLS first edition says that during assignment a narrowing primitive conversion is allowed provided:
  • The expression is a constant expression of type int.

  • The type of the variable is byte, short, or char.

  • The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.

  • In JLS second edition, the first condition is amended to read:
    • The expression is a constant expression of type byte, short, char or int.

    • My jdk 1.2.2 apparently follows JLS 1st ed, and other people said that JDK 1.3. votes for JLS 2nd edition.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic