About Question enthuware.ocajp.i.v7.2.1000 :
Moderator: admin
-
- Posts: 1
- Joined: Wed Dec 03, 2014 9:15 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1000 :
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
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
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1000 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Fri Feb 20, 2015 7:14 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1000 :
The question is; what will this code print when run.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);
}
}
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?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1000 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Fri Feb 20, 2015 7:14 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1000 :
Ah ofcourse.
Thanks for making that clear!
Thanks for making that clear!
-
- Posts: 31
- Joined: Wed Feb 08, 2017 5:42 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1000 :
This question is not easy.
I think that it is tough.
I think that it is tough.
-
- Posts: 31
- Joined: Wed Feb 08, 2017 5:42 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1000 :
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!
throw new NullPointerException("aa");
What is the scope of this parameter?
Thanks a lot!
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1000 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Tue Apr 04, 2017 3:35 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1000 :
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:
It outputs the stacktrace, s and 1 so I am assuming that once the exception is handled properly println() will be executed?
Regards
Walter
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);
}
Regards
Walter
Last edited by admin on Tue Apr 04, 2017 10:05 pm, edited 1 time in total.
Reason: Please enter code inside [code] [/code] tags
Reason: Please enter code inside [code] [/code] tags
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1000 :
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).
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Tue Apr 04, 2017 3:35 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1000 :
Sorry, just considering the last bit of code:
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
Code: Select all
public static void main(String args[]) {
try {
m();
} catch (Exception e) {
}
System.out.println(s);
}
Regards
Walter
Last edited by admin on Wed Apr 05, 2017 2:59 pm, edited 1 time in total.
Reason: Please enter code inside [code] [/code] tags
Reason: Please enter code inside [code] [/code] tags
Who is online
Users browsing this forum: No registered users and 25 guests