Jump to content

[Java] Diference Between i-- and --i

PedroBarbosa

Hey

Title says it all...

 

exemple:

 

int i = 0;

--i; or i--;

 

Thanks

Link to comment
Share on other sites

Link to post
Share on other sites

What exactly do you mean by i-- and --i?

Do you mean like this:

int i = 0;

int 0 = i;

Specs: CPU - Intel i7 8700K @ 5GHz | GPU - Gigabyte GTX 970 G1 Gaming | Motherboard - ASUS Strix Z370-G WIFI AC | RAM - XPG Gammix DDR4-3000MHz 32GB (2x16GB) | Main Drive - Samsung 850 Evo 500GB M.2 | Other Drives - 7TB/3 Drives | CPU Cooler - Corsair H100i Pro | Case - Fractal Design Define C Mini TG | Power Supply - EVGA G3 850W

Link to comment
Share on other sites

Link to post
Share on other sites

What exactly do you mean by i-- and --i?

Do you mean like this:

int i = 0;

int 0 = i;

 

My question is if this is the same thing:

 

 
for( int i = 10 ; i >= 0 ; --i )
System.out.println( i );
 
for( int i = 10 ; i >= 0 ; i-- )
System.out.println( i );
Link to comment
Share on other sites

Link to post
Share on other sites

--i subtracts 1 right now. i-- uses the current value of i, then subtracts 1.

 

So in pseudocode:

int i = 5;

print(i--);

print(i);

 

this prints 5 and then 4.

 

print(--i);

print(i);

 

this prints 4 twice.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

The following will modify the value before it assigns to the variable

int i = 5;int value = --i;// i = 4 and value = 4 int i = 5;int value = ++i;// i = 6 and value = 6

The following will assign the variable and then modify the value

int i = 5;int value = i--;// i = 4 and value = 5int i = 5;int value = i++;// i = 6 and value = 5

If you aren't assigning to a variable or passing to a method, then there's not really much of a difference. They will both still increment or decrement the value.

int i = 5;--i; // i = 4i--; // i = 3

Technically --i and ++i are slightly more efficient than i-- and i++ when considering the compiled code (without compiler optimization). However I'd be very surprised if compilers aren't optimizing it for you thus making them equivalent. Regardless, this micro-optimization wouldn't really be noticeable but I still prefer to use --i and ++i when I can anyway.

Link to comment
Share on other sites

Link to post
Share on other sites

 

My question is if this is the same thing:

 

 
for( int i = 10 ; i >= 0 ; --i )
System.out.println( i );
 
for( int i = 10 ; i >= 0 ; i-- )
System.out.println( i );

 

I am not sure what --i is, but i-- basically means:

i = i - 1;

So everytime you code comes across i--, it lower the var i with 1.

 

EDIT: better explanations above :P

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

These are types of incrementers. i-- is called a post-decrementer, and --i is a pre-decrementer. 

 

Basically, if you were to use one of these within an operation, the time at which i is subtracted from would be different.

 

for example:

int a = 1;int c = --a;

so here both c and a would equal 0 because of the use of the PRE-decrementation. It would look at the value of a, which is 1, and then subtract from it, then assign that value to c.

 

If it was like this:

int a = 1;int c = a--;

c would equal 1 and a would equal 0 because of the POST-decrementation. c would look at the value of a, which is 1, assign it, and then a would be subtracted by 1.

 

These same principles apply to pre and post incrementers like i++ and ++i.

CPU: i7-4790K --- HEATSINK: NZXT Kraken X61 --- MOBO: Asus Z97-A --- GPU: GTX 970 Strix --- RAM: 16GB ADATA XPG --- SSD: 512GB MX100 | 256GB BX200 HDD: 1TB WD Black --- PSU: EVGA SuperNova G2 --- CASE: NZXT H440 --- DISPLAY3 x Dell U2414H --- KEYBOARD: Pok3r (Clears) --- MOUSE: Logitech G Pro --- OS: Windows 10

Link to comment
Share on other sites

Link to post
Share on other sites

i-- means the expression has the value i, then i gets decremented.

--i decrements i before setting the value of the expression.

 

Example

x = 0

y = x++ => y is 0, x is 1

// y is set to x, then x gets incremented.

z = ++x => z is 1, z is 1

// x gets incremented , then z is equal to the incremented x.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not sure if it ever matters in Java but in C++ with operator overloading, unless you specifically want a post increment it's generally better to use pre increment. Since a pre increment just returns the same object it can be much faster than a post increment which much create a copy of the object, increment the old object then return the copy.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×