About Question enthuware.ocajp.i.v7.2.964 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.964 :
System.out.println("c") is always unreachable , so why not it cause compiler error.
It the answer is: The compiler cannot decide during compile time that the statement is unreachable, then how do we decide if the compiler will be able to decide or not during compile time?
It the answer is: The compiler cannot decide during compile time that the statement is unreachable, then how do we decide if the compiler will be able to decide or not during compile time?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.964 :
The detailed rules are given here: Section 14.21 of JLS
It may seem complicated but the general idea is to look for compile time constants i.e. final variables and literals. For example,
will work. But this will not:
because compiler knows that x cannot change from 5.
On top of this, there are some exceptions such as the if statement. It is explained in the above mentioned section.
HTH,
Paul.
It may seem complicated but the general idea is to look for compile time constants i.e. final variables and literals. For example,
Code: Select all
int x = 5;
while (x > 100){ System.out.println(x); }
Code: Select all
final int x = 5;
while (x > 100){ System.out.println(x); }
On top of this, there are some exceptions such as the if statement. It is explained in the above mentioned section.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Mon Nov 11, 2013 3:11 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.964 :
I pasted the code in my eclipse, it fails to compile because m1 is supposed to throw an exception, which is not caught...
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.964 :
The question and the explanation are correct. Please make sure you paste the code exactly as given.jeremy_dd wrote:I pasted the code in my eclipse, it fails to compile because m1 is supposed to throw an exception, which is not caught...
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Tue May 17, 2016 2:33 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.964 :
I think you forgot to add "...throws Exception" to the main function.jeremy_dd wrote:I pasted the code in my eclipse, it fails to compile because m1 is supposed to throw an exception, which is not caught...
-
- Posts: 6
- Joined: Sat Jun 03, 2017 1:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.964 :
Hi! But if we catch the exception in catch block and print some message say "message" , then the statement System.out.println("C"); will be executed ?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.964 :
What happened when you tried it out?
Paul.
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Sat Jun 03, 2017 1:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.964 :
Thank you for your promotion to write and test code by myself and sorry for my hastiness.
And the statement System.out.println("C"); will be printed after we catch the exception
And the statement System.out.println("C"); will be printed after we catch the exception
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.964 :
Very good
This exam is all about writing short code examples to understand the basic concepts.
This exam is all about writing short code examples to understand the basic concepts.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: gadsgadsx and 51 guests