PDA

Click to See Complete Forum and Search --> : Public, Private, Static?


prog_tom
Feb 13th, 2004, 11:34 PM
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?

prog_tom
Feb 13th, 2004, 11:36 PM
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.

azteched
Feb 14th, 2004, 09:25 AM
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?

I'm new to Java too so this may not be the best explanation, but:

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.

crptcblade
Feb 14th, 2004, 09:52 AM
Also, using ++/--/+=/etc is more efficient than using i = i + 1 because of the way memory is allocated.

prog_tom
Feb 14th, 2004, 02:20 PM
I still don't understand how i++ works...

Kasracer
Feb 14th, 2004, 02:22 PM
Originally posted by prog_tom
I still don't understand how i++ works... i++ adds one AFTER the expression has been run, ++i runs BEFORE the expression is run.

They explained it quite well IMO

NoteMe
Feb 14th, 2004, 03:01 PM
Don't you program in C++...it is the same there....

prog_tom
Feb 14th, 2004, 09:24 PM
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?

crptcblade
Feb 14th, 2004, 09:29 PM
No.


int i = 0;

System.out.println(i++);


"0" will be printed out, then the value of i will equal 1.


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?

prog_tom
Feb 15th, 2004, 12:38 AM
:D wisdom man:wave: