Results 1 to 10 of 10

Thread: Public, Private, Static?

  1. #1

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810

    Post 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

  2. #2

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810
    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

  3. #3
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703

    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.
    an ending

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  5. #5

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810
    I still don't understand how i++ works...

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  6. #6
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    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

  7. #7
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Don't you program in C++...it is the same there....

  8. #8

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810
    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

  9. #9
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  10. #10

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810
    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
  •  



Click Here to Expand Forum to Full Width