The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Sun Certified Programmer for Java[tm] 2 Platform

Posted by Matt Gerrans on November 16, 2001 at 3:11 PM

Hey Chin,

I was considering signing up, but I wasn't sure if I'm ready, as I only really started learning Java in earnest in the last few months. I know all the answers (I think, see below) to your questions and I still don't think I'm ready for the test. They ask a lot of very nit-picky questions about how Java works, from what I've seen, so just being able to program in Java is nowhere near enough. Check out JavaRanch, it looks like a good place to find information for preparing for certification. When do you take the test?

Here are my answers to your questions:


  1. An identifier is a name you create in your program. Both getChars and str are identifiers in this case.
  2. I think what they want to know is, what will it return and what is the method name (since the parameter list can be what you want (unless you forgot the word "default" and the question was "what is the prototype of a default constructor?" which is easier to answer more specifically)). So the answer would be that it as no return and matches the class name.
  3. Yes, your example is unlabeled. A labeled break or continue allows you to break out of multiple nested loops, to a specific point without using a lot of state variables. Here's how it looks:

    SomeObject o = new SomeObject();

    outerLabel:
    for( int i=0; i < X; i++ )
    {
    for( int j=0; j < Y; j++ )
    {
    for( int k=0; k < Z; k++ )
    {
    o.doStuff();

    if( o.inner() )
    break; // breaks out to the j-loop.

    if( o.flap() )
    break outerLabel; // breaks all the way out.
    }
    if( o.ready() )
    continue outerLabel; // continues with next i-loop.
    }
    }


  4. Literal means the quoted String. So in your example, str is a String and "a" is a String literal, to which you have assigned str in the first case. The second case is kind of a waste, unless you really need to create a new unique String object which matches the content of the literal "a". That is, the in the first case, you have a reference that refers to the same string that the literal represent, but in the second case, you have explicitly created a new String object that is initialized with the same content as the literal. The second is wasteful and should only be done when you really need to do it (which is probably never, since Strings are immutable, but there may be some very specialized cases where there is some reason to do it (mostly when you are concerned about the unique identity of the string object)).
  5. &, | and ^ are bitwise operators that you use on integral primitive types (char, byte, int, long and boolean). & is a bitwise and, | is a bitwise or and ^ is an exclusive or. Here's an example (you can write a little program to do more experimenting):

    int x = 1; // binary 1 (with 31 preceding 0's).
    int y = 2; // binary 10 (with 30 preceding 0's).
    int z = x & y; // z is 0.
    z = x | y; // z is now binary 11 (decimal 3).
    z = z ^ x; // z is now binary 10 (decimal 2).

  6. No and no. It is the other way around.
  7. The example you have is just two top-level classes, with one derived, from the other, but not an inner class. Inner classes are classes within classes. There can be zero to many inner class instances for each outer class instance that contains it. Inner classes and anonymous inner classes can access the members (including private) of their outer class. A normal inner class is defined just like a normal class, but it is done within the scope of the outer class. An anonymous inner class is defined at the same time an instance is created and must be derived from some other class (often a listener in Swing). These are usually very simple and often have just one method. A static inner class unlike the other two, does not have access to the members of its outer class. This description is probably too brief for you to really get a good grasp, so you may want to check a book (like Eckel's Thinking In Java), or the Java Tutorials at sun. Here's a simple example with all three types of inner class:

    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.Container;
    import javax.swing.JFrame;
    import javax.swing.JLabel;

    public class Outer
    {
    private String ryan = "I want to be a corporal some day!";

    void Outer()
    {
    System.out.println( "Created an instance of Outer!" );
    }

    class Inner
    {
    Inner()
    {
    System.out.println( "Created an instance of Inner! ryan says: " + ryan );
    }
    }

    class StaticInner
    {
    StaticInner()
    {
    // System.out.println( "Created an instance of Inner! ryan says: " + ryan );
    // Can't access Outer class from inner static!
    System.out.println( "Created an instance of StaticInner!");
    }
    }

    Inner getNewInner()
    {
    return new Inner();
    }

    StaticInner getNewStaticInner()
    {
    return new StaticInner();
    }

    void showUI()
    {
    // Here is an anonymous class, derived from WindowAdapter:
    WindowAdapter appCloser = new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    };

    JFrame frame = new JFrame("Outer Demo");
    frame.addWindowListener( appCloser );
    Container pane = frame.getContentPane();
    pane.setLayout( new java.awt.FlowLayout() );
    pane.add(new JLabel("Pretty exciting, eh?"));
    frame.pack();
    frame.setSize( 200, 100 );
    frame.setVisible(true);
    }

    public static void main( String[] args )
    {
    Outer outerInstance = new Outer();
    Object innerInstance = outerInstance.getNewInner();
    Object staticInnerInstance = outerInstance.getNewStaticInner();

    outerInstance.showUI();
    }
    }


Good luck!

- mfg


> oh geez :( i typed a long list of questions but accidentally pressed escape button and it all got lost! :( guess i have to type it all *again* (boo hoo ~)

> *sigh* well :P i just went through the objectives and there's some things i don't really understand, probably due to my bad bad english. if u guys are free and would like to share your opinion/knowledge, please feel free to do so because i bought my voucher yesterday :P. i just bought it coz i know if i don't buy it, it'll get get postponed again and again and i'll end up not taking it years later. it's just a way to force myself to take it since it has been postponed for about a year plus :P.

> okay here's the questions after my length introduction.....

> 1. what is identifiers? is it the variable/class/method names that's use to identify them individually?

>


> public void String getChars(String str)
> {
> ...
> }
>

> is getChars the identifier?

> 2. what is the "prototype of a constructor"? (personal comment : there's such thing as a prototype of a constructor?? *shriek*)

> 3. what is labeled and unlabeled use of break and continue? as far as i know.. there's just break and continue, rite? but since it's stated in the objectives.. i guess there is such thing as labeled and unlabeled use.

>


> for(int i=0;i<5;i++)
> if(i==3)
> break;
> else continue;
>

> is this unlabeled use? what is labeled use then? (full of questions.....)

> 4. what is literal values of String? i don't understand the statement "declare literal values for String and all primitive rtypes using all permitted formats, bases and representations".

>


> String str="a"; // is this declaring literal value as opposed to
> String str=new String("a"); // ??
>

> 5. err i know how to use && and || but i don't know how to use & and | :P. anybody care to explain? ^_^

> 6. just a yes-or-no question...

> is overloading : redefinining different implementations of a method that has the same name and parameter type in the parent class?
> and is overriding : defining different implementations of methods of same name but of different parameter types?

> 7. what is top level classes, inner claases, static inner classes, anonymous inner classes? is top level classes something like

>


> class Human
> {
> ...
> }
>

> and inner classes

>


> class Man extends Human
> {
> ...
> }
>

> ?? (my goodness my theoretical java is really bad)

> yup.. that's about it.. for other objectives i could do my own homework, go to the library next week and LEARN how to use them :P. these questions just..well.. i'm just not sure about whether they are really what i think they are. heheh.





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us