Page 1 of 1

Re: About Question enthuware.ocajp.i.v7.2.1000 :

Posted: Wed Dec 03, 2014 9:28 am
by sdey2000
Hi ,

Aren't strings immutable . Here value of s is assigned a new value 1 and same is displayed when run. Am i missing something here ?

Thanks

Re: About Question enthuware.ocajp.i.v7.2.1000 :

Posted: Wed Dec 03, 2014 10:12 am
by admin
Yes, strings are immutable but you can make a variable point to another String object. That is what is happening here. For example, when you do s += i; the reference variable s starts pointing to a new String object (which has the value of s+i) altogether. The original String object (which contains the value of the original string s) remains as is but without any reference pointing to it.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1000 :

Posted: Fri Feb 20, 2015 7:46 am
by gezzoo
public class Test {

static String s = "";

public static void m0(int a, int b) {
s += a;
m2();
m1(b);
}

public static void m1(int i) {
s += i;
}

public static void m2() {
throw new NullPointerException("aa");
}

public static void m() {
m0(1, 2);
m1(3);
}

public static void main(String args[]) {
try {
m();
} catch (Exception e) {
}
System.out.println(s);
}
}
The question is; what will this code print when run.
And the answer is 1, I understand this, because the exception is not caught, it is propagated back to the previous method.
But I always thought you had to use the throws clause in the method declaration to pass the exception to the previous method?
If not, when exactly should you use throws?

Re: About Question enthuware.ocajp.i.v7.2.1000 :

Posted: Fri Feb 20, 2015 9:53 am
by admin
You need to put the exception in the throws clause only if the exception is a checked exception and the method does not handle the exception. An uncaught runtime exception will be propagated to the caller without any need to put it in the throws clause.

Re: About Question enthuware.ocajp.i.v7.2.1000 :

Posted: Fri Feb 20, 2015 10:29 am
by gezzoo
Ah ofcourse.
Thanks for making that clear!

Re: About Question enthuware.ocajp.i.v7.2.1000 :

Posted: Sat Feb 25, 2017 10:50 am
by AndaRO
This question is not easy.
I think that it is tough.

Re: About Question enthuware.ocajp.i.v7.2.1000 :

Posted: Sat Feb 25, 2017 10:57 am
by AndaRO
I feel strange that NullPointerException can have parameter in the statement.
throw new NullPointerException("aa");

What is the scope of this parameter?

Thanks a lot!

Re: About Question enthuware.ocajp.i.v7.2.1000 :

Posted: Sat Feb 25, 2017 11:05 am
by admin
Not sure what you mean by, "What is the scope of this parameter?". Which parameter and which scope are you referring to? What is the scope of the parameter when you do "new Integer(10)"?

NPE just like any other class. It does have a constructor that takes a String argument. That is what is being used here. Pretty much all standard exception classes do have a constructor that takes a String argument.

Re: About Question enthuware.ocajp.i.v7.2.1000 :

Posted: Tue Apr 04, 2017 4:05 pm
by Walter
I would have thought the program would have terminated in the catch clause and therefore not execute the System.out.println(s); but it does, is this because the exception is handled and program ends gracefully. I added this code:

Code: Select all

public static void main(String args[]) {
        try {
            m();
        } catch (Exception e) {
			e.printStackTrace();
        }
		finally{
			System.out.println("s");
		}
        System.out.println(s);
    }
It outputs the stacktrace, s and 1 so I am assuming that once the exception is handled properly println() will be executed?

Regards
Walter

Re: About Question enthuware.ocajp.i.v7.2.1000 :

Posted: Tue Apr 04, 2017 10:08 pm
by admin
Which println are you talking about? If call to m() throws an exception, it will be caught by the catch block, which will print the stack trace. Next, finally will be executed (which has a println), and since there is no return statement in the try, catch, or finally, the statements after the whole try/catch/finally will be executed (which is another println).

Re: About Question enthuware.ocajp.i.v7.2.1000 :

Posted: Wed Apr 05, 2017 2:29 pm
by Walter
Sorry, just considering the last bit of code:

Code: Select all

public static void main(String args[]) {
   try {
      m();
   } catch (Exception e) {
   }
   System.out.println(s);
}

I assumed that the program would terminate at the catch block and there wouldn't be any printout, but the last println() does get executed. I guess the question I am asking is, println() gets executed because the exception is handled and therefore not thrown out of main for the JVM to kill the thread, which is what i have done by removing the try/catch and the stacktrace throws an Exception in thread "main", which answers my own question. Thanks for the earlier response, you mentioning the return statements has gotten the ball rolling for me.

Regards
Walter