Hello, I am new to Java, could someone tell me what is the difference of Public Private Static and when to use them?
And what is the difference of i++, and ++i and i+=i? When do I use them?
Printable View
Hello, I am new to Java, could someone tell me what is the difference of Public Private Static and when to use them?
And what is the difference of i++, and ++i and i+=i? When do I use them?
And how do I tell the compiler I just wanna read only first 2 digits of a variable. IE 0.55555 i just want it to read 0.55.
I'm new to Java too so this may not be the best explanation, but:Quote:
Originally posted by prog_tom
Hello, I am new to Java, could someone tell me what is the difference of Public Private Static and when to use them?
And what is the difference of i++, and ++i and i+=i? When do I use them?
public means the function or variable is visible outside of the class in which it is declared.
private is the opposite; the function or variable isn't visible outside the class.
A static variable is one for which there is only one copy for all instances of the class. A static function is callable without an instance of the class it's defined in.
i++ means "increment i by 1", as does ++i. The difference between the two is when the increment is part of a larger expression, e.g.
int i=0;
myfunction ( i++ ); //passes zero to the function
compared to:
int i=0;
myfunction ( ++i ); //passes one to the function
i+=i is different; += is shorthand for "add to self", e.g.
i = i + 3 is equivalent to i += 3
So i += i adds i to i.
Also, using ++/--/+=/etc is more efficient than using i = i + 1 because of the way memory is allocated.
I still don't understand how i++ works...
i++ adds one AFTER the expression has been run, ++i runs BEFORE the expression is run.Quote:
Originally posted by prog_tom
I still don't understand how i++ works...
They explained it quite well IMO
Don't you program in C++...it is the same there....
Quote:
Originally posted by kasracer
i++ adds one AFTER the expression has been run, ++i runs BEFORE the expression is run.
They explained it quite well IMO
adds one before expression has been run?
int i = 0;
i++;
so result would be 2???
and ++i would be 1 only?
No.
"0" will be printed out, then the value of i will equal 1.Code:int i = 0;
System.out.println(i++);
The value of i will be set to 1, then "1" will be printed out. See it now?Code:int i = 0;
System.out.println(++i);
:D wisdom man:wave: