Results 1 to 8 of 8
  1. #1
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default Confused myself learning Java... please help

    Hi,
    While going through my tutorials I have confused myself in one area.
    This part:

    Food foodObject = new Food();
    The bolded part below is what confuses me now:
    Java Code:
    [B]Food[/B] foodObject = new Food();
    Why do we need to tell java twice that we need to create a new instance of Food?

    Why not just:
    Java Code:
    foodObject = new Food();
    ?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    What you have to realise that the
    Java Code:
    Food foodObject = new Food();
    line of code is doing 2 things. It is declaring a variable and it is initialising the variable. The very first time you use a variable in a program you need to tell the compiler what its type is.
    Java Code:
    Type variableName;  // perfectly legal
    //later
    variableName = ....
    // or all on one line
    Type variableName = ...
    What do the ... mean? Well if it was a primitive then you could just hard code a literal value. If it was a reference type (ie object) the you use the new keyword to invoke the constructor. Another possibility is a method call or compund statement such as a mathematical formula.
    Java Code:
    String stringOne = "hello ";
    String stringTwo = "world";
    int x;
    int y;
    int z = 2;
    String s;
    Foo f;
    x = 7;
    y = x * z;
    s = stringOne + stringTwo;
    f = methodThatReturnsAFooObject();
    I hope that helps.

  3. #3
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Thanks! I think I got it thanks to your explanation.
    This is the part that really helped:
    line of code is doing 2 things...
    just to confirm, this:
    Food foodObject = new Food();
    and this:
    Food foodObject;
    foodObject = new Food();
    Is identical, except that "Food foodObject = new Food();" is doing it more efficiently in one line instead of two.

    Yes?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Depends upon what you mean by efficient. When the JVM executes the code I'm sure it makes no difference if it were on one or two. In fact if it were on two lines then it is possible that the compiler will change it to one line.

  5. #5
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Depends upon what you mean by efficient. When the JVM executes the code I'm sure it makes no difference if it were on one or two. In fact if it were on two lines then it is possible that the compiler will change it to one line.
    Sorry, I meant human efficient / easy to read but to the computer/jvm it looks exactly the same as the two line example written above.

  6. #6
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    I think one of the reasons I got so confused was that I tend to think of variables as (I dont know the exact word, but I think its "primitive") either
    int
    string
    double
    etc

    so getting around the concept of:
    make a class Foo
    have nothing in that class (no methods, variables)
    then make a variable of that class like so "Foo foo;" in class Bar
    now what the heck is foo? (I know its a variable of class Foo but in my head my brain is screaming: its not a int, its not a string, its not a double its not a boolean... and class Foo does not contain anything... what type does that fall under? ARRRRGH!!!!!!)

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Go through this for more details about variables: Java Variables

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  8. #8
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Quote Originally Posted by goldest View Post
    Go through this for more details about variables: Java Variables

    Hope that helps,

    Goldest
    Thanks for the link! Will check it out now;)

Similar Threads

  1. How to start learning Java
    By hiranya in forum New To Java
    Replies: 12
    Last Post: 01-13-2014, 09:47 AM
  2. Learning Java...
    By Learning Java in forum New To Java
    Replies: 2
    Last Post: 09-24-2010, 09:03 PM
  3. Pls Help me in learning Java
    By SimranK in forum New To Java
    Replies: 6
    Last Post: 06-24-2010, 07:44 PM
  4. Learning Java
    By Derkins in forum New To Java
    Replies: 3
    Last Post: 04-09-2009, 02:46 AM
  5. Learning Java
    By jjoshua2 in forum New To Java
    Replies: 2
    Last Post: 12-20-2008, 02:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •