Page 1 of 1
About Question enthuware.ocajp.i.v7.2.965 :
Posted: Wed Nov 28, 2012 5:06 pm
by Deepa
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.
Re: About Question enthuware.ocajp.i.v7.2.965 :
Posted: Thu Nov 29, 2012 7:34 am
by admin
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.
Re: About Question enthuware.ocajp.i.v7.2.965 :
Posted: Thu Jan 10, 2013 1:38 pm
by guest
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.
Re: About Question enthuware.ocajp.i.v7.2.965 :
Posted: Thu Jan 10, 2013 6:20 pm
by admin
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.
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.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.965 :
Posted: Thu Oct 12, 2017 1:26 pm
by JuergGogo
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
}
}
After switching the cases that code doesn't compile anymore. The order of the cases and the entry point seems to be relevant. Even changing calculate(2); --> calculate(1); doesn't fix the compiler error. Even switch(1) doesn't help.
Re: About Question enthuware.ocajp.i.v7.2.965 :
Posted: Thu Oct 12, 2017 9:20 pm
by admin
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.
Re: About Question enthuware.ocajp.i.v7.2.965 :
Posted: Thu Feb 15, 2018 10:21 am
by ArpRokz
int a;
if(false)
{System.out.println(a);} // works
boolean b=false;
if(b)
{System.out.println(a);}// Error !
Why ?
Re: About Question enthuware.ocajp.i.v7.2.965 :
Posted: Thu Feb 15, 2018 11:48 am
by admin
In if(false), the condition is a compile time constant, while in f(b), it is not.
Re: About Question enthuware.ocajp.i.v7.2.965 :
Posted: Tue Mar 13, 2018 2:52 am
by Meghana
I changed the code in question from:
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);
}}
(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?
Re: About Question enthuware.ocajp.i.v7.2.965 :
Posted: Tue Mar 13, 2018 5:30 am
by admin
Re: About Question enthuware.ocajp.i.v7.2.965 :
Posted: Tue Mar 13, 2018 8:24 am
by Meghana
Thank you!!
Re: About Question enthuware.ocajp.i.v7.2.965 :
Posted: Fri Apr 24, 2020 6:22 am
by evaevaeva
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?
Re: About Question enthuware.ocajp.i.v7.2.965 :
Posted: Fri Apr 24, 2020 8:23 am
by admin
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?