• Post Reply Bookmark Topic Watch Topic
  • New Topic

definite assignment  RSS feed

 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
void flow(boolean flag) {
int k;
if (flag)
k = 3;
else
k = 4;
System.out.println(k);
}

void flow(boolean flag) {
int k;
if (flag)
k = 3;
if (!flag)
k = 4;
System.out.println(k); // k is not "definitely assigned" before here
}

What is the difference between the two codes...? I think they are the same..isnt it?
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use code tag when your question contains code.

read this Use Code Tags
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


// k is not "definitely assigned" before here


K is int data type that is called java primitive data type. it is declare automatically according to type.

ex: here k is int so k is declare as 0.

If you declare using wrapper class type then you have to declare initial value.

ex: in k is Integer it prints null value.

What is the difference between the two codes...?


It is same and both will give you same result.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the second code gives a compile error that k should be initialised before use
 
Anuj Prashar
Ranch Hand
Posts: 99
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K is int data type that is called java primitive data type. it is declare automatically according to type.

ex: here k is int so k is declare as 0.

If you declare using wrapper class type then you have to declare initial value.

ex: in k is Integer it prints null value.



Here k is a local varible, it wont get initialised automatically.
Only instance variable get initialised.
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, it's local variable so it gives error. and you have to initialize it..

 
Anuj Prashar
Ranch Hand
Posts: 99
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is same and both will give you same result.


Both are not same

In first code block with 'if-else' it is sure that k will get initialized.Because if 'if-test' failed than k will get initialized in else block.
But in second block with multiple 'if' statement the k is being initialized inside 'if' block
& compiler thinks that there is no else block & if 'if test' fail than k will not het initialized
before System.out.println(k); statement so compiler gives an error in 2nd code.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks anuj......ur answer makes a lot sense....thanks a ton
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For reasons explained here, please don't write things like "ur".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!