About Question enthuware.ocajp.i.v7.2.965 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Deepa

About Question enthuware.ocajp.i.v7.2.965 :

Post 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.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.965 :

Post 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.
If you like our products and services, please help us by posting your review here.

guest

Re: About Question enthuware.ocajp.i.v7.2.965 :

Post 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.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.965 :

Post 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.
If you like our products and services, please help us by posting your review here.

JuergGogo
Posts: 28
Joined: Mon Sep 25, 2017 8:16 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.965 :

Post 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.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.965 :

Post 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.
If you like our products and services, please help us by posting your review here.

ArpRokz
Posts: 15
Joined: Sun Jan 28, 2018 12:38 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.965 :

Post by ArpRokz »

int a;
if(false)
{System.out.println(a);} // works

boolean b=false;
if(b)
{System.out.println(a);}// Error !

Why ?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.965 :

Post by admin »

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.

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.965 :

Post 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?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.965 :

Post by admin »

If you like our products and services, please help us by posting your review here.

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.965 :

Post by Meghana »

Thank you!!

evaevaeva
Posts: 10
Joined: Fri Mar 06, 2020 5:18 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.965 :

Post 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?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.965 :

Post 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?
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 41 guests