aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes iteration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "iteration" Watch "iteration" New topic
Author

iteration

Heba Mahmoud
Ranch Hand

Joined: Apr 26, 2009
Posts: 62
1-what is the difference between x++ and x+=1 in the for loop iteration?such as saying
for(int x=0; x< 3 ;x+=1)
for(int x=0; x< 3 ;++x)
for(int x=0; x< 3 ;x++) how the 3 cases output the same value for x for each iteration.

2-what make the code unreachable in the try/catch/finally/code after finally code ??

3- as i understand when we try to deserialize transient variable , it gets its default value of its datatype , ok what about if that transient variable was also final ?? and if this information are wrong ,would anyone can confrim me?

this questions has no source ,as this is from my mind ...


SCJP Pass 85%....
Thousands of candles can be lighted from a single candle, and the life of the candle will not be shortened
Rajeev Trikha
Ranch Hand

Joined: Jan 29, 2010
Posts: 85
a) The first three lines are identical. They are all incrementing x by i. It is the last statement which is executed before the iteration so it doesn't matter how it is incremented.

b) Do you have code like this in mind:


Although both print statements are unreachable, there doesn't appear to be compile error.

c) If you call output/input within the same program then the final transient variable will keep its original value.

Rajeev Trikha (SCJP 6)
Heba Mahmoud
Ranch Hand

Joined: Apr 26, 2009
Posts: 62
1-i think there is a difference between ++x and x++... how the value become the same in iteration ??

2- what about that case of unreachable code ,.. i think that it should the complair fails .. but it doesn't.


3- when say transient int value =8; after deserialize it becomes 0 as i understand , may be i am wrong
so when it is final , what do you think it will be ?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Heba Mahmoud wrote:1-i think there is a difference between ++x and x++... how the value become the same in iteration ??

The increment part of the for loop is evaluated completely before the condition is checked, so it doesn't matter if you use ++x or x++

Heba Mahmoud wrote:2- what about that case of unreachable code ,.. i think that it should the complair fails .. but it doesn't.

x is not a constant, so the compiler cannot detect its value at compile time so doesn't know that the if condition is true and the println statement is not reachable. Unreachable code with if conditions works differently then we think, see this post by Henry. But don't worry, such details (which Henry pointed out) won't be tested in the exam.

Heba Mahmoud wrote:3- when say transient int value =8; after deserialize it becomes 0 as i understand , may be i am wrong
so when it is final , what do you think it will be ?

What happened when you tried it??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
System.exit(-1) is a valid function call. The compiler is happy
because it does not attempt to understand the result of the call.

Jim...


BEE MBA PMP SCJP-6
venkat ga
Greenhorn

Joined: Feb 14, 2010
Posts: 7
package file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
class StreamA implements java.io.Serializable{
private int a =10;
final transient private int b=10; // HERE i AM ALWQAYS GETTING THE VALUE 10,
public int getB(){
return b;
}
}
public class StreamTest {
public static void main(String[] s) throws IOException,ClassNotFoundException{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("d:/out.txt")));
StreamA a = new StreamA();
System.out.println("welcome"+a.getB());
out.writeObject(a);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("d:/out.txt")));
a = (StreamA)ois.readObject();
System.out.println("welcome"+a.getB());
}
}

Please explain about final transient private int b=10; some internal mechanism.

The variables which are transient which are not serialized. but if i use fianl why serializing is happening?
Heba Mahmoud
Ranch Hand

Joined: Apr 26, 2009
Posts: 62
Ankit Garg said ...
x is not a constant, so the compiler cannot detect its value at compile time so doesn't know that the if condition is true and the println statement is not reachable.


i understand from that if we make the boolean x = true ; as a final value then we should have a complair error as x in that case is a constant .... but it complies and runs fine ......



Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

venkat ga wrote:
Please explain about final transient private int b=10; some internal mechanism.

The variables which are transient which are not serialized. but if i use fianl why serializing is happening?



Final variables, that are assigned to a compile time constant, at the same time of the declaration, are also compile time constants. You can even argue that the variable probably doesn't really exist, as the compiler replaces it with the constant, whenever it is used.

Anyway, move the assignment to a instance initializer, or a constructor, and you will see that it is not serialized.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

Heba Mahmoud wrote:
Ankit Garg said ...
x is not a constant, so the compiler cannot detect its value at compile time so doesn't know that the if condition is true and the println statement is not reachable.


i understand from that if we make the boolean x = true ; as a final value then we should have a complair error as x in that case is a constant .... but it complies and runs fine ......


Maybe it would help if you read the next sentence to the one that you quoted?

Henry
venkat ga
Greenhorn

Joined: Feb 14, 2010
Posts: 7
Thank you very much HenryWong

Venkat
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Venkat in the future please start a new topic for your problem by clicking . The original topic had nothing to do with your problem, so the question is not appropriate in this topic...
venkat ga
Greenhorn

Joined: Feb 14, 2010
Posts: 7
Hi Ankit,

If it is, i am sorry.
But as per Heba Mahmoud (3rd question), i got this doubt. so i continued here.
because i am new to this forum.
Can you please give some more suggestions, how should i effectively follow this forum.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Venkat, I actually didn't realize that your question was indeed related to the original question. So posting in this same topic isn't a problem. See How To Ask Questions On JavaRanch for other forum rules...
venkat ga
Greenhorn

Joined: Feb 14, 2010
Posts: 7
Thanks alot Ankit, The link which you have given is very good. i will go through it once.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: iteration