• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubts

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
}
}
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic