File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

an int outside a for loop, which is printed after the for loop

 
Jasper Vader
Ranch Hand
Posts: 284
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will happen if you attempt to compile and run the following code
public class TimDig{
public static void main(String argv[]){
TimDig td = new TimDig();
td.samcov();
}
public void samcov(){
int i=1;
int j=2;
if((i==20) && (j==(i=i*2))){
}
System.out.print(i);
if((i==20) & (j==(i=i*2))){}
System.out.print(i);
int x = i & 2;
System.out.print(x);
}
}

What is the deal with the variable int i? is it being affected by the for loops, and what will happen when
int x = i & 2; ?
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jasper -
I feel like we're old friends!
What are you trying to do with the various instances of '&&' and "&' in your code?
-Bert
 
Jasper Vader
Ranch Hand
Posts: 284
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey there Bert!
not my code but the code of a mock-exam q.
&& is a short circuit operator to see if both are true. The first expression is not so nothing gets a look-in for the second expression.
The first & operator looks at the boolean values.
The second & operator does an AND job on the 0's and 1's (which && cannot do).
 
Jasper Vader
Ranch Hand
Posts: 284
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class TimDig{
public static void main(String argv[]){
TimDig td = new TimDig();
td.samcov();
}
public void samcov(){
\\here is where instance variable i is assigned the value of 1
int i=1;
\\here is where instance variable j is assigned the value of 2
int j=2;
\\here is where i nearly gets doubled but the short circuit AND prevents it. I guess if 'i' had been declared in that if loop, it's only scope would have been there. If 'i' had been declared in the class body then this method would have been doing stuff to what the reference gives but not changing the initial value of 'i'?
if((i==20) && (j==(i=i*2))){
}
\\if this print line was in the curly brackets after the AND line, it would not get printed. As it is, 'i' gets printed out (which is 1)
System.out.print(i);
\\aha, here is where 'i' gets multiplied by 2. But I can't help wondering what would have happened if i was defined inside this if loop - i guess it would not have been accessible outside.
if((i==20) & (j==(i=i*2))){}
\\again, this call to print to the screen is not inside the curly brackets of the for loop so it happens no matter what. Except that now 'i' has the value of 2.
System.out.print(i);
\\a new local variable is introduced, 'x',. 'i' is now 2, or 10 in binary, as is the literal 2. 0 AND 0 gives 0; 1 AND 1 gives 1, so i AND 2 gives the result 2, so x is 2 and gets printed out.
int x = i & 2;
System.out.print(x);
}
}
end situation - 122 gets printed.

[ January 15, 2003: Message edited by: Jasper Vader ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic