wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubts" Watch "Doubts" New topic
Author

Doubts

Murali Veeraiyan
Greenhorn

Joined: Oct 11, 2003
Posts: 5
Q1, class floattest {

public static void main(String[] args) {
final String a = "A";//1
final String b = "B";//2
final String c = a+b;
final String d = a+b;
System.out.print((c==c) + ",");
System.out.print(((a+b)==(a+b)) + ",");
System.out.print(c==d);

}
}
try out taking final in 1 and 2...why this behaviour?
Q2,
Why cant a local variable in java be static?...while in C++ static
variables inside a method is allowed
Q3,
public class Question20 {
public static void main(String[] args) {
Question20Sub myref = new Question20Sub();
try{
myref.test();
}catch(Exception e){}
}
void test() throws Exception{
System.out.println("In Question20");
throw new Exception();
}
}
class Question20Sub extends Question20 {
void test() {
System.out.println("In Question20Sub");
}
}
***********************************************************
this works...while the next doesn't.... why?
***********************************************************
import java.io.*;
public class Question05 {
public static void main(String[] args) {
Question05Sub myref = new Question05Sub();
try{
myref.test();
}catch(IOException ioe){}
}
void test() throws IOException{
System.out.println("In Question05");
throw new IOException();
}
}
class Question05Sub extends Question05 {
void test() {
System.out.println("In Question05Sub");
}
}
Eric Y. Wu
Greenhorn

Joined: Oct 11, 2003
Posts: 2
Q1.
I don't quite understand what your question is. Let see if I get what you mean.
"A" is a String literal in the shared String pool. By having this pool, we can achieve object-reuse to reduce the number of objects and to eliminate the overhead creating and destroying objects. The reason we can afford such a pool is that "A" is constant, immutable. Variable a is a reference to "A". It is set as final so that we cannot re-assign a to other objects during runtime. At one end, we have a String literal "A" or "B", which does not change at runtime. At the other end, we have a final variable a or b, which does not change either. So the compilor just takes advantage of this information, which means a+b will be evaluated at compile-time. No matter how many time a+b appears and where it appears, its result is the same String literal "AB" in the String pool. I guess the compilor just replaces each a+b with "AB". So we have c==d is true because each a+b is the same piece of "AB" in the String pool.
But when removing final at line 1 and line 2. Variabls a and b are not "fixed" any more. It cannot be evaluated at compile-time. Since information cannot be determined beforehand, a run-time evaluation of a+b is performed and each evaluation returns a new String. It has to because a or b can change. By the way, this new String is not in the shared pool automatically. Only calling intern() will move it into the pool. If you add

the pool will have "A", "B", "AB", and "AB". Yes, two "AB"s. So c==d still not true.

Q2.
I can only guess here. Local static variables are not easy to track. When a method exits, these variables are still there. They are used again the next time the method is invoked. I also don't see major advantage by having local static variables. I could be wrong.

Q3.
This one is about throwing exceptions when overriding. If the overridded method throws an exception, the overriding method should throw the same exception or its subclass exception. The point in this question is about runtime exception. You are NOT required to deal with runtime exception. Exception has a subclass called RuntimeException. You are not required to handle it. This is why you subclass method, which overrides the original method, may have RuntimeException, a subclass of Exception, and you are not required to deal with it. However, IOException is a checked exception. You are required to handle it.
[ October 11, 2003: Message edited by: Eric Y. Wu ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch, Murali.
Please ask only a question for each post. Otherwise the answers will be mixed up in the thread.
Q1)
Being a and b final and initialized to a constant value the compiler classifies a+b as a compile contant expression (JLS 15.28) The compiler replaces each occurrance of a+b with a symbolic reference to the same string AB.
When a and b are not final, the expression a+b produces a different string object each time is evaluated. These string objects are not going to be found equal by the == operator.
Q2)
In Java the local variables are created in the Java stack. Thus, they can only exist for the duration of the method invocation.
[ October 11, 2003: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Murali,
I recognize the following question as one of mine.

Although the code is clearly mine, the name of the class has been changed. Whenever I see one of my questions with the class name changed I start to wonder if the question has appeared on a mock exam web site other than mine. Where did you find that question?
I have a request for everyone here at the ranch. Please make sure that you always identify the source of the questions.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270

import java.io.*;
public class Question05 {
public static void main(String[] args) {
Question05Sub myref = new Question05Sub(); //line 1
try{
myref.test();
}catch(IOException ioe){}
}
void test() throws IOException{
System.out.println("In Question05");
throw new IOException();
}
}
class Question05Sub extends Question05 {
void test() {
System.out.println("In Question05Sub");
}
}

In line 1, we are creating an instance of Question05Sub (myref), the test() method is called using *myref* reference does not throw an IOException, hence the compiler error.
If you change line 1 to :
Question05 myref = new Question05();
The code will compile fine, since test() of Question05 class, does throw the IOException.
--Cathy.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubts
 
Similar Threads
Val's mock exam question
Why this code is not compiling?
Question 6 val's mock
Exception
mock01 Q5, Q20 What is the difference?