This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

iteration

 
Heba Mahmoud
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
Rajeev Trikha
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Heba Mahmoud
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
venkat ga
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much HenryWong

Venkat
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot Ankit, The link which you have given is very good. i will go through it once.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic