Need help with declaring variables

Discussion in 'Programming General' started by I am Indian, Apr 9, 2011.

Need help with declaring variables
  1. Unread #1 - Apr 9, 2011 at 1:33 PM
  2. I am Indian
    Joined:
    Mar 9, 2011
    Posts:
    448
    Referrals:
    1
    Sythe Gold:
    0

    I am Indian Forum Addict
    Banned

    Need help with declaring variables

    I'm totally new to Java basics. I've had my own RSPS, and I'm good on coding that, but now I want to make something totally by me. I've a shitty problem in declaring variable.

    I want to declare a variable, but I don't know how to. I just started with Java 2 days ago in my school and we've just learnt making a calculator.

    I don't have to declare n2 as a text like I declared n1 & r1. Above, I've made n1 declare the value of n2. E.G.
    In order to remove the error off my 'q=n2*r1;' part, I need to declare n2.

    I'm not able to express what I meant I guess, but if you get me, please reply helping me.

    Thanks.
     
  3. Unread #2 - Apr 9, 2011 at 6:23 PM
  4. rsgold4u
    Joined:
    Nov 17, 2007
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    rsgold4u Forum Addict
    Banned

    Need help with declaring variables

    do this:

    Code:
    int n1 = 0, n2 = 0, r1 = 0, q = 0;
    That way all the ints are initialized and you shouldn't have problems.
     
  5. Unread #3 - Apr 9, 2011 at 6:56 PM
  6. Pottworth
    Joined:
    Nov 27, 2005
    Posts:
    83
    Referrals:
    0
    Sythe Gold:
    0

    Pottworth Member

    Need help with declaring variables

    EDIT: Don't listen to me, I misread what you wrote.
     
  7. Unread #4 - Apr 10, 2011 at 8:51 AM
  8. I am Indian
    Joined:
    Mar 9, 2011
    Posts:
    448
    Referrals:
    1
    Sythe Gold:
    0

    I am Indian Forum Addict
    Banned

    Need help with declaring variables

    I'll try when I'll be back home, thanks in advance.
     
  9. Unread #5 - Apr 10, 2011 at 2:54 PM
  10. rsgold4u
    Joined:
    Nov 17, 2007
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    rsgold4u Forum Addict
    Banned

    Need help with declaring variables

    You obviously don't know java then.
     
  11. Unread #6 - Apr 11, 2011 at 7:37 AM
  12. I am Indian
    Joined:
    Mar 9, 2011
    Posts:
    448
    Referrals:
    1
    Sythe Gold:
    0

    I am Indian Forum Addict
    Banned

    Need help with declaring variables

    It worked thanks.
     
  13. Unread #7 - Apr 13, 2011 at 12:29 AM
  14. RyanSand
    Joined:
    Apr 8, 2010
    Posts:
    178
    Referrals:
    0
    Sythe Gold:
    0

    RyanSand Active Member

    Need help with declaring variables

    In Java, primitive data types (int, char, boolean, etc...) need to be declared and initialized.

    A declaration statement is any line of code that includes the data type and the variable name. Data type is the type of variable that it is (int, char, short...).
    To initialize a variable is to set its value to something. The reason you are getting an error is that it tries to compare one number (say 10) to a null variable. Null does not mean 0, it means the variable has not yet been initialized.

    There are two main types of declaration statements.


    Those that include the initialization:

    int numStudents = 3;


    And those that don't:

    int numStudents;


    The difference between the two, is that the second does not initialize the variable; it only declares it.
    This is acceptable as long as the variable receives a value before being used.

    Correct declaration and initialization in 2 lines:

    int numStudents;
    numStudents = 3;

    Hope this helps :)
     
< Small HTMl Problem | My Project :) Support & ideas please >

Users viewing this thread
1 guest


 
 
Adblock breaks this site