About Question enthuware.ocajp.i.v7.2.965 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.965 :
Hi guys,
I came across this explaination for one of the switch block questions (Question 20 under objective 3)which says:-
"int i;
if( somecondition) i = 20;
int k = i;
Here, if some condition returns false, then i remains uninitialized hence the compiler flags an error. "
So, i modified the code as
"public class AssignmentOps {
public static void main(String[] args){
int i;
boolean somecondition= true;
if( somecondition){ i = 20; System.out.println("value of i" + i);}
int k = i;
System.out.println(i +" "+k);
}
}
It shows the error as local variable i may not be initialised...
so what is wrong with my code according to the explaination.
I came across this explaination for one of the switch block questions (Question 20 under objective 3)which says:-
"int i;
if( somecondition) i = 20;
int k = i;
Here, if some condition returns false, then i remains uninitialized hence the compiler flags an error. "
So, i modified the code as
"public class AssignmentOps {
public static void main(String[] args){
int i;
boolean somecondition= true;
if( somecondition){ i = 20; System.out.println("value of i" + i);}
int k = i;
System.out.println(i +" "+k);
}
}
It shows the error as local variable i may not be initialised...
so what is wrong with my code according to the explaination.
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.965 :
The compiler doesn't know that somecondition is always true. You as a programmer know that but the compiler only considers the compile time constants as "known". So if you make somecodition final, it will work.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.965 :
Hi Paul,
In this eg. if the 2 cases in switch were switched, like below; the unintialized err wld be thrown.
switch(x){
default:
val = "def";
case 2:
}
The point being - in your expln. its not just the placement of "break" stmt in case but also the entry point in the switch stmt that defines if an err is thrown or not.
In this eg. if the 2 cases in switch were switched, like below; the unintialized err wld be thrown.
switch(x){
default:
val = "def";
case 2:
}
The point being - in your expln. its not just the placement of "break" stmt in case but also the entry point in the switch stmt that defines if an err is thrown or not.
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.965 :
No, the entry point does not determine whether the code will compile or not. Because the entry point is determined at runtime and not compile time. The compiler does not know which label will be entered at run time.guest wrote:Hi Paul,
In this eg. if the 2 cases in switch were switched, like below; the unintialized err wld be thrown.
switch(x){
default:
val = "def";
case 2:
}
The point being - in your expln. its not just the placement of "break" stmt in case but also the entry point in the switch stmt that defines if an err is thrown or not.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Mon Sep 25, 2017 8:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.965 :
Code: Select all
public class TestClass{
public static void main(String[] args) { calculate(2); }
public static void calculate(int x){
String val;
switch(x) {
default: val = "def";
case 2:
}
System.out.println(val); // --> compiler error: val may not have been initialized
}
}
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.965 :
It is not the entry point but the positioning of default that is the problem in your case. Compiler doesn't know the value of x and so it doesn't know where the case will be entered at runtime. But it knows that if it enters at 2, variable val will remain uninitialized.
Remember that entry point is determined at run time based on the value of the switch variable. Compiler knows only the positioning of the case labels.
Remember that entry point is determined at run time based on the value of the switch variable. Compiler knows only the positioning of the case labels.
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Sun Jan 28, 2018 12:38 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.965 :
int a;
if(false)
{System.out.println(a);} // works
boolean b=false;
if(b)
{System.out.println(a);}// Error !
Why ?
if(false)
{System.out.println(a);} // works
boolean b=false;
if(b)
{System.out.println(a);}// Error !
Why ?
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.965 :
In if(false), the condition is a compile time constant, while in f(b), it is not.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.965 :
I changed the code in question from:
(Just eliminated default and initialized val in case2)
I got an error now, saying "local variable val maybe uninitialized". Does this mean that we can only initialize a variable in default case?
Code: Select all
public class TestClass{
public static void main(String[] args) { calculate(2); }
public static void calculate(int x){
String val;
switch(x){
case 2:
default:
val = "def";
}
System.out.println(val);
}
}
o/p: "def"
TO:
public class TestClass{
public static void main(String[] args) { calculate(2); }
public static void calculate(int x){
String val;
switch(x) {
case 2:
val="def";
}
System.out.println(val);
}}
I got an error now, saying "local variable val maybe uninitialized". Does this mean that we can only initialize a variable in default case?
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.965 :
No. Explained here: https://stackoverflow.com/questions/108 ... a-switches
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.965 :
Thank you!!
-
- Posts: 10
- Joined: Fri Mar 06, 2020 5:18 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.965 :
If I call calculate() with a very large number (for example calculate(2892390023), I get a compile error saying that the integer is too large. I can not find on the web why that is. Does anybody know?
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.965 :
What is the largest number that can be stored in an int? If you try to assign a larger number, the compiler will know that you are doing something wrong becaus that number cannot be put in an integer, right?
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 41 guests