About Question enthuware.ocajp.i.v8.2.1023 :
Moderator: admin
-
- Posts: 1
- Joined: Tue Feb 21, 2017 2:54 am
- Contact:
About Question enthuware.ocajp.i.v8.2.1023 :
Doesn't in point 2:
public class X{
static int k = 0;
static {
k= 10/0;
}}
throw an ArithmeticException ?
This exception is not available in the set of Possible solutions
public class X{
static int k = 0;
static {
k= 10/0;
}}
throw an ArithmeticException ?
This exception is not available in the set of Possible solutions
-
- Posts: 1
- Joined: Tue Feb 21, 2017 9:50 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1023 :
I believe all errors/exceptions in the initializer will throw a ExceptionInInitializerError.
edit: see this post viewtopic.php?f=2&t=2222
edit: see this post viewtopic.php?f=2&t=2222
-
- Posts: 25
- Joined: Fri Aug 04, 2017 12:21 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1023 :
I would like to know why the following shows a compiler error ?
static
{
throw new NullPointerException();
}
but the below code doesn't .
static
{
if(true)
throw new NullPointerException();
}
if(true) evaluates to true always is known at compile time itself right ? Then why does it compile ? Whereas the first case doesn't?
static
{
throw new NullPointerException();
}
but the below code doesn't .
static
{
if(true)
throw new NullPointerException();
}
if(true) evaluates to true always is known at compile time itself right ? Then why does it compile ? Whereas the first case doesn't?
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1023 :
The if(true) construct is a special case that is exempted from compilation error by the specification to allow conditional compilation. There are many variations of the same question.
Please see this for details: https://docs.oracle.com/javase/specs/jl ... l#d5e19555
Please see this for details: https://docs.oracle.com/javase/specs/jl ... l#d5e19555
If you like our products and services, please help us by posting your review here.
-
- Posts: 25
- Joined: Fri Aug 04, 2017 12:21 am
- Contact:
-
- Posts: 8
- Joined: Mon Dec 25, 2017 4:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1023 :
Code: Select all
// #1
public static void main(String[] args) {
int x;
final boolean DEBUG=true;
if (DEBUG) {
x = 1;
return;
}
System.out.println(x); //compile error: The local variable x may not have been initialized
}
Code: Select all
// #2
public static void main(String[] args) {
int x;
if (true) {
x = 1;
return;
}
System.out.println(x); //compiles fine
}
Hi Paul, can you please explain what is the difference between #1 and #2?
DEBUG is a compile time constant, so why #1 does not compile?
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1023 :
There should not be any issue for either one. Both compile fine for me.
If you like our products and services, please help us by posting your review here.
-
- Posts: 8
- Joined: Mon Dec 25, 2017 4:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1023 :
okay i tried to compile it with command prompt and it works.
wth is eclipse doing?
(I know you shouldn't use eclipse for the exam, I used command prompt for 2 weeks while learning for the exam then it was to annoying and I switched back to IDE)
wth is eclipse doing?
(I know you shouldn't use eclipse for the exam, I used command prompt for 2 weeks while learning for the exam then it was to annoying and I switched back to IDE)
Who is online
Users browsing this forum: No registered users and 46 guests