Adblock breaks this site

Need help with declaring variables

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

  1. I am Indian

    I am Indian Forum Addict
    Banned

    Joined:
    Mar 9, 2011
    Posts:
    448
    Referrals:
    1
    Sythe Gold:
    0
    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.
     
  2. rsgold4u

    rsgold4u Forum Addict
    Banned

    Joined:
    Nov 17, 2007
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0
    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.
     
  3. Pottworth

    Pottworth Member

    Joined:
    Nov 27, 2005
    Posts:
    83
    Referrals:
    0
    Sythe Gold:
    0
    Need help with declaring variables

    EDIT: Don't listen to me, I misread what you wrote.
     
  4. I am Indian

    I am Indian Forum Addict
    Banned

    Joined:
    Mar 9, 2011
    Posts:
    448
    Referrals:
    1
    Sythe Gold:
    0
    Need help with declaring variables

    I'll try when I'll be back home, thanks in advance.
     
  5. rsgold4u

    rsgold4u Forum Addict
    Banned

    Joined:
    Nov 17, 2007
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0
    Need help with declaring variables

    You obviously don't know java then.
     
  6. I am Indian

    I am Indian Forum Addict
    Banned

    Joined:
    Mar 9, 2011
    Posts:
    448
    Referrals:
    1
    Sythe Gold:
    0
    Need help with declaring variables

    It worked thanks.
     
  7. RyanSand

    RyanSand Active Member

    Joined:
    Apr 8, 2010
    Posts:
    178
    Referrals:
    0
    Sythe Gold:
    0
    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 >


 
 
Adblock breaks this site