Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How This Java Program Executes ?

 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello to All My JavaRanch Friends

I created the following code when I worked with the java coding
I found following type of code
after editting, compiling and running the code.



When I try to compile and run the following output produced

i = 256 and j = 128

Also note down one thing by observation the value of i is exactly two times greater than value of j

When you reduce the number of i's and j's in the Line 1 above you get same result that is the value of i is exactly two times greater than value of j

How java code executes in the above program?
Please Help on this
 
Patel Chintan
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ninad Kulkarni:
Hello to All My JavaRanch Friends

I created the following code when I worked with the java coding
I found following type of code
after editting, compiling and running the code.



When I try to compile and run the following output produced

i = 256 and j = 128

Also note down one thing by observation the value of i is exactly two times greater than value of j

When you reduce the number of i's and j's in the Line 1 above you get same result that is the value of i is exactly two times greater than value of j

How java code executes in the above program?
Please Help on this


You can simplyfy Line 1 to:
i *= j *= i *= j *= i *= j *= i *= j;
i = I * (j *= i *= j *= i *= j *= i *= j)
1=i*(j*( i *= j *= i *= j *= i *= j))
I= i*(j*(i*( j *= i *= j *= i *= j)))
I= i*(j*(i*( j*( i *= j *= i *= j))))
I= i*(j*(i*( j*( i *( j *= i *= j))))
I= i*(j*(i*( j*( i *( j *( i *= j)))))
I= i*(j*(i*( j*( i *( j *( i * j))))))

And so J=j*(i*( j*( i *( j *( i * j)))))
That means = I= 2*(2*(2*( 2*( 2 *( 2 *( 2 * 2))))) = 256

and j = 2*(2*( 2*( 2 *( 2 *( 2 * 2)))) =128
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Patel Chintan
now I got it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic