Results 1 to 8 of 8
- 02-14-2011, 03:47 AM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Confused myself learning Java... please help
Hi,
While going through my tutorials I have confused myself in one area.
This part:
The bolded part below is what confuses me now:Food foodObject = new Food();
Why do we need to tell java twice that we need to create a new instance of Food?Java Code:[B]Food[/B] foodObject = new Food();
Why not just:
?Java Code:foodObject = new Food();
- 02-14-2011, 04:03 AM #2
What you have to realise that the
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:Food foodObject = new Food();
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:Type variableName; // perfectly legal //later variableName = .... // or all on one line Type variableName = ...
I hope that helps.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();
- 02-14-2011, 04:15 AM #3
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Thanks! I think I got it thanks to your explanation.
This is the part that really helped:
just to confirm, this:line of code is doing 2 things...
and this:Food foodObject = new Food();
Is identical, except that "Food foodObject = new Food();" is doing it more efficiently in one line instead of two.Food foodObject;
foodObject = new Food();
Yes?
- 02-14-2011, 04:21 AM #4
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.
- 02-14-2011, 04:25 AM #5
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
- 02-14-2011, 04:31 AM #6
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
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!!!!!!)
- 02-14-2011, 04:36 AM #7
Go through this for more details about variables: Java Variables
Hope that helps,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 02-18-2011, 12:33 AM #8
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Similar Threads
-
Learning Java...
By Learning Java in forum New To JavaReplies: 2Last Post: 09-24-2010, 09:03 PM -
Pls Help me in learning Java
By SimranK in forum New To JavaReplies: 6Last Post: 06-24-2010, 07:44 PM -
Learning Java
By Derkins in forum New To JavaReplies: 3Last Post: 04-09-2009, 02:46 AM -
Learning Java
By jjoshua2 in forum New To JavaReplies: 2Last Post: 12-20-2008, 02:46 AM -
How to start learning Java
By hiranya in forum New To JavaReplies: 2Last Post: 06-28-2007, 02:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks