aspose file tools*
The moose likes Beginning Java and the fly likes Doubts in this sample demo class in Java 1.6 Tutorial Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubts in this sample demo class in Java 1.6 Tutorial" Watch "Doubts in this sample demo class in Java 1.6 Tutorial" New topic
Author

Doubts in this sample demo class in Java 1.6 Tutorial

Jenson Chew
Ranch Hand

Joined: May 27, 2007
Posts: 55
Hi Guys,

I've came across this demo class which is used to illustrate the Unary Operators. However, the prefix and postfix usage of ++ operators confused me.

class PrePostDemo {
public static void main(String[] args){
int i = 3;
i++;
System.out.println(i);// "4"
++i;
System.out.println(i);// "5"
System.out.println(++i);// "6"
System.out.println(i++);// "6"
System.out.println(i);// "7"
}
}


May I know why i++ will remain as 6, where as the last println(i) will give a 7?


:: Vision is Power ::
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

The answer is in the FAQ : http://faq.javaranch.com/view?PostIncrementOperatorAndAssignment


[My Blog]
All roads lead to JavaRanch
Jenson Chew
Ranch Hand

Joined: May 27, 2007
Posts: 55
Hi Satou,

Thanks for the reply. However, I wish to know what's the use for using i++? When and where would we be using it?

Thanks.

Regards,
Jenson
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Originally posted by Jenson Chew:
I wish to know what's the use for using i++? When and where would we be using it?


This may sound trite, but you use it where you need it. You will find situations where you need to obtain the value of before incrementing it. I often use it in for loops (but I think that it probably doesn't matter here whether i++ or ++i is used):


but I've used it in other situations, for instance in this mortgage calculator:
Jenson Chew
Ranch Hand

Joined: May 27, 2007
Posts: 55
Hi Pete,

Thanks for the clarification. I think I have a better idea now and how it works.

Great helps from you guys. Highly appreciated.

Thanks.

Regards,
Jenson
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40041
    
  28
Pete Stein, you are right about loops. Nobody uses ++i in a for loop, but it would show no perceivable difference from i++.
Jenson Chew
Ranch Hand

Joined: May 27, 2007
Posts: 55
Hi Campbell,

Can I assume that ++i is less common than i++? I see a lot of examples using i++, and not many of the examples use ++i, I might be wrong, I'm still a beginner, just make assumption based on my common sense =p
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40041
    
  28
You need to know the difference between ++i and i++.

Try this little bit of toy code:That ought to print 4 and 26. i++ is called postincrement; it adds 1 to the value of i and returns the old value. In that little example, 4. The i = bit takes the old value and reassigns it to i. So i becomes 4 again.
++j is called preincrement; it adds 1 to the value of j and returns the new value. In this case 26.

If you can't understand that . . . only ever use ++ operators on their own line. Then ++i; and i++; both act to add 1 to i; since there is no difference in the result, people usually use i++; and as you correctly suggest people never use ++i; on its own.

Some people, the Javaranch code style: look for no 3.2 included, suggest one ought only to use i++; as a statement on its own and not use ++i at all.

The same applies to the -- operators.
Jenson Chew
Ranch Hand

Joined: May 27, 2007
Posts: 55
Hi Campbell,

Thanks. Now I finally understand what does it means and when I should use i++ and ++i, which in most cases, I think most of the time I would probably using i++

You cleared the path for me, and sorry for asking this stupid questions

Cheers!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40041
    
  28
Only too pleased to help . . . and it's not a stupid question, but a point a lot of people find difficult.
Jenson Chew
Ranch Hand

Joined: May 27, 2007
Posts: 55
That's not the only point I dont understand, maybe some other people also think so? Or only me?

I will constantly seek for guidance from you guys when I'm in doubt. You guys are my lighthouse in the dark ^^
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40041
    
  28
There are some very helpful and knowledgeable people on these fora; those of us who are a bit more experienced still learn a great deal here.
Jenson Chew
Ranch Hand

Joined: May 27, 2007
Posts: 55
Originally posted by Campbell Ritchie:
There are some very helpful and knowledgeable people on these fora; those of us who are a bit more experienced still learn a great deal here.


ookie, Campbell, I'm still struggling on digesting what's being fed to me on "More on Classes" section in Java 1.6 Tutorial from Sun Java. So wordy! Else I'm dumb! Sad...
Jenson Chew
Ranch Hand

Joined: May 27, 2007
Posts: 55


BTW, does that make sense when nothing has been taught on importing Java.Util.* ?

This is the class that the tutorial requested a learner to come out with after reading through the "More On Classes", imagine a new beginner of Java having zero knowledge in Java, does he knows he needs to do this?

Well, maybe I'm wrong? But I just couldn't understand why the solution required something like that?
swapnil deo
Greenhorn

Joined: May 30, 2007
Posts: 6
hey jenson!!
i would like to make your concept of i++/++i a it more clear.
i++ is called post-increment operator..i.e it first increments and then prints the value.

post increment operator:
int i=0;
system.out.println(i++);


o/p:
0

ie 0 first gets printed

pre-increment operator:

int i=0;
system.out.println(++i);

o/p: 1

now this is because...in ++i the first part ie ++ increments n then i gets printed..hence the o/p in this case becomes 1.
Jenson Chew
Ranch Hand

Joined: May 27, 2007
Posts: 55
Originally posted by swapnil deo:
hey jenson!!
i would like to make your concept of i++/++i a it more clear.
i++ is called post-increment operator..i.e it first increments and then prints the value.

post increment operator:
int i=0;
system.out.println(i++);


o/p:
0

ie 0 first gets printed

pre-increment operator:

int i=0;
system.out.println(++i);

o/p: 1

now this is because...in ++i the first part ie ++ increments n then i gets printed..hence the o/p in this case becomes 1.


Hi Swapnil,

Thanks for the clarification I've understood that part and thanks for all the input here. Maybe someone could also look at the reply on top regarding my doubts on Java 1.6 Tutorial content too? Hehe ...
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

What was your doubt concerning 1.6 ?
Jenson Chew
Ranch Hand

Joined: May 27, 2007
Posts: 55
Originally posted by Satou kurinosuke:
What was your doubt concerning 1.6 ?


Hi Satou,

It's in the post before Swapnil =p

Regards,
Jenson
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I don't understand why the import is here anyway
You're right about the learning path. You should know about imports if you are using classes of different packages in your source. But the "java.util.*" does not seem to be used here !
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
Is that the entire code for the class? If it is, the import statement is not needed since there is nothing there from the java.utils package.


"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
Jenson Chew
Ranch Hand

Joined: May 27, 2007
Posts: 55
Originally posted by Satou kurinosuke:
I don't understand why the import is here anyway
You're right about the learning path. You should know about imports if you are using classes of different packages in your source. But the "java.util.*" does not seem to be used here !


Hi Satou,

Yes, that's the part that confuse me. I do not understand why I'm never taught of importing the Java.Util.* before learning about Class. By the way, I also have doubts in learning Java, as I have two books on hand, if I never make use of them, they will be deemed useless after Sun release maybe their Java 1.7 >.<" The post is in another thread of mine. If you are free please help me to take a look there =)

Thanks a million.

Regards,
Jenson
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubts in this sample demo class in Java 1.6 Tutorial