|
-
Feb 14th, 2004, 12:34 AM
#1
Thread Starter
Fanatic Member
Public, Private, Static?
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
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Feb 14th, 2004, 12:36 AM
#2
Thread Starter
Fanatic Member
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.

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Feb 14th, 2004, 10:25 AM
#3
Fanatic Member
Re: Public, Private, Static?
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.
-
Feb 14th, 2004, 10:52 AM
#4
Also, using ++/--/+=/etc is more efficient than using i = i + 1 because of the way memory is allocated.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Feb 14th, 2004, 03:20 PM
#5
Thread Starter
Fanatic Member
I still don't understand how i++ works...

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Feb 14th, 2004, 03:22 PM
#6
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
-
Feb 14th, 2004, 04:01 PM
#7
Don't you program in C++...it is the same there....
-
Feb 14th, 2004, 10:24 PM
#8
Thread Starter
Fanatic Member
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?

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Feb 14th, 2004, 10:29 PM
#9
No.
Code:
int i = 0;
System.out.println(i++);
"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?
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Feb 15th, 2004, 01:38 AM
#10
Thread Starter
Fanatic Member
wisdom man

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|