Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubts in this sample demo class in Java 1.6 Tutorial

 
Jenson Chew
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is in the FAQ : http://faq.javaranch.com/view?PostIncrementOperatorAndAssignment
 
Jenson Chew
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48967
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48967
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48967
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48967
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What was your doubt concerning 1.6 ?
 
Jenson Chew
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jenson Chew
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic