Naveed Hassan

Greenhorn
+ Follow
since Feb 23, 2001
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Naveed Hassan

Originally posted by nitin sharma:
public class lap
{
public static void main(String[]rgs)
{
for(int j=0,k=0;j<10&&k<10;k+=++j==10 ? 1+(j=0):0)
System.out.println( " k "+k+", j "+j);
}
}

Hi everybody,
can anybody explain that loop.?


-----------------------------
Step 0 = first part of for
step 1 = second part of for
step 2 = third part of for
for # 1: step 0: j=0 k=0
step 1: Step 1-1: j<10 gives true
Step 1-2: k<10 gives true
Step 1-3: true && true gives true

---Thus prints k=0, j=0
step 2: Now we see at k+=++j==10 ? 1+(j=0):0
Now according to JVM it this expression has to do four MAJOR operators
1) +=
2) ++
3) ==
4) ? :
According to precedence table, the order of operations will be 2,3,4,1.i.e. ++, ==, ? :, +=
So here we go

step 2-1: first ++j is evaluated thus ++j giving j = 1
step 2-2: now == is evaluated thus 1==10 gives false
step 2-3: now the turnary opearator goes, So having result of false, the right side of : gives 0
step 2-4: The 0 got from the turnary operator gives k+=0 thus making k=0
FINALLY, At the end we are having k=0 and j=1
for # 2: Step 0: Will Not execute (and now onwards, step zero is ignored in this explaination)
Step 1: 1<10 && 0<10 gives true
---Prints k=0, j=1
Step 2: 2-1: makes j=2
2-2: 2==10 gives false
2-3: gives 0
2-4: k+=0 gives k=0

FINALLY, At the end we are having k=0 and j=2
for # 3: Step 1: 2<10 && 0<10 gives true
---Prints k=0, j=2
Step 2: 2-1: makes j=3
2-2: 3==10 gives false
2-3: gives 0
2-4: k+=0 gives k=0

FINALLY, At the end we are having k=0 and j=3
*****************************************************************
Now calculate urself how for # 4,5,6,7,8,9 goes and let us evaluate for # 10
*****************************************************************
for # 10: Step 1: 9<10 && 0<10 gives true
---Prints k=0, j=9
Step 2: 2-1: makes j=10
2-2: 10==10 gives TRUE
2-3: gives true side of the turnary operator.i.e. 1+(j=0) which makes j=0 and 1+0 and thus 1
2-4: k+=1 gives k=1

FINALLY, At the end we are having k=1 and j=0
for # 11: Step 1: 0<10 && 1<10 gives true
---Prints k=1, j=0
Step 2: 2-1: makes j=1
2-2: 1==10 gives false
2-3: gives 0
2-4: k+=0 gives k=1

FINALLY, At the end we are having k=1 and j=1

***************************************************************
Let us see what happens at for # 100 and for # 101
***************************************************************
for # 100: Step 1: 9<10 && 9<10 gives true
---Prints k=9, j=9
Step 2: 2-1: makes j=10
2-2: 10==10 gives TRUE
2-3: gives true side of the turnary operator.i.e. 1+(j=0) which makes j=0 and 1+0 and thus 1
2-4: k+=1 gives k=10

FINALLY, At the end we are having k=10 and j=10

for # 101: Step 1: 10<10 && 10<10 gives false
Hence the loop exits.

FINALLY, At the end we are having k=10 and j=10

---------------------------------
Thats all! Please correct me if i am wrong at any step
Thanx
Naveed
Till today i always considered that small brackets ( ) has the highest precednce but the following code of program has made me confused
public class Test
{
public static void main(String args[])
{
int i=0;
boolean t=true;
boolean b=(t | | 0 < (i+=5));
System.out.println(i); //prints 0, why not 5
}
}

This prints 0, why not 5. Please help me in this regard.
Thank You.
AoA Pakistan!
I am also planning to appear near mid of April.
Will u guide me that which strategy one should adopt just one week before appearing for exams.
Thanx
Naveed
(Lahore)
Hi
but why the compiler is not ignoring the rest of the line by finding comment symbol==> // on the start of the line
BYe
Hi
from where i can download complete JLS
Thanx
I am totally puzzled as i have also coumminctaed with the write through email and i got the following reply from Mr, Paul Deitel
(i am pasting my email and his reply)
------My email-------------
Respected Sir,
On page # 1254 , Qusteion E-19 says
"Whats is the result when the one's
complement of a number is added to itself?"
And the answer says "Zero"
But i think the correct answer is "One's"
Please guide me regarding this.
Thanx
-----And in Reply Mr. Deitel says------
Hi Naveed,
The answer is correct as is.
For confirmation, run example 23.8
Best wishes,
Paul
--------------
I am still confused due analyzing the above disccusiion and reading the example 23.8.
Please i still need guidence
In Deitel & Dietel (3rd Ed), on page # 1254 , Qusteion E-19 says
"Whats is the result when the one's complement of a number is added to itself?"
And the answer says "Zero"
But i think the correct answer is "One's"
Please guide me regarding this.
Thanx
1)First we will see how 300 will be representated in int (i.e. using 32 bits)

Thus 300 = 0000 0000 0000 0000 0000 0001 0010 1100
Now when we cpnvert it into byte (8 bits) the remaining 24 left side bits are truncated (chopped-off) , thus giving u only

0010 1100 which is equal to 44.
2) The least significant bit is, clearly, �0�
3)I don�t know how to change 300 to �300.
Well, the simplist way is a=a*-1
4) Dietel and Dietel, JAVA How to program (3rd Ed) explains a very simple Short cut Method to Convert binary into Octal and hexa system by grouping into 3 and 4. Try to read it from there. Its easy.
Hope everything is clear to u.
Well, then is there any idea for -0 (negative zero) in JAVA?
Hi
We know the range of byte is -128 to +127.
I want to know how -128 is represented in binary system
Hi
Regarding 3)
The operands are being accepted as Octal and with the help of conversion system we know
00001010 in Octal = 512 in decimal System
00001000 in Octal = 520 in decimal System

Now the 3) statment becomes
System.out.println(512^520);
giving you 8.
(I hope that u wont ask for: how 512^520=8???)

Well, I serached more and found the following justification
The precedence of [] is higher than =
So, [] will have to be solved first in the expression

Hi Puneet
Well, i try to explain
Any exprseeion is sloved in two steps. They are
Step 1) First Recognise ALL the oprands in expression (moving from left to right)
Step 2) Then do the Operations (considering their prcedence and associativity)
So going through these Steps on ur expression, we have
Step 1) Recognise all operands moving from left to right
So, we have three operands
Operand # 1 = a[0] (a refernce to the 1st elemnet of array a)
Operand # 2 = i (simply a refernce to the variable)
Operand # 3 = 9 ( a constant value)
Step 2) Now the expression becomes a[0]=i=9
And the operation of assignment is done.
Thus giving NO out of Bound error
I hope this will help u
Bye

Hi Jane
This question is from
1999 First Indain Edition
Naveed