#1
  1. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    257
    Rep Power
    0

    If I do this, will I get any NPEs?


    I have four variables which are x, y, p, and z.

    y= 1, z = 2 but I do not assign any numbers to x. If I do

    p = x + y + z, will I get a NPE?
    Last edited by Tawijakarta; August 8th, 2013 at 06:14 AM.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    You don't get NPE with primitive variables. You only get NPEs when working with object references (pointers).
  4. #3
  5. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by Tawijakarta
    I have four variables which are x, y, p, and z.

    y= 1, z = 2 but I do not assign any numbers to x. If I do

    p = x + y + z, will I get a NPE?
    As Norm said, you won't get an NPE, but if the x is a local variable, the code won't even compile. You have to explicitly initialize local variables before you use them in an expression that assumes they have a value.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    15
    Rep Power
    0
    However, if by some way it does not give you an error in compiling.
    The value defaults to 0.

IMN logo majestic logo threadwatch logo seochat tools logo