File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question" Watch "Question" New topic
Author

Question

Hans
Greenhorn

Joined: Sep 20, 2000
Posts: 11
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

Joined: Sep 23, 2000
Posts: 3312
    
    7
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


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
scott irwin
Ranch Hand

Joined: Aug 07, 2000
Posts: 87
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

Joined: Sep 23, 2000
Posts: 3312
    
    7
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

Joined: Aug 07, 2000
Posts: 87
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

Joined: Aug 26, 2000
Posts: 10065
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.

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question