aspose file tools*
The moose likes Beginning Java and the fly likes Double x, y = 0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Double x, y = 0" Watch "Double x, y = 0" New topic
Author

Double x, y = 0

You Gin
Ranch Hand

Joined: Jul 23, 2010
Posts: 52
Hi guys,
what is the difference between



and



I used to think it is the same, but just got a compilator error the x and y references can be never initialized (second first case).
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

In the first one, x is not initialized at all. Only y is assigned 0.
If that statement is within a method, you will get compilation errors - variable x might not have been initialized.

I dont think there is anything wrong with the second one.Thats perfect.
I'm not clear on why you got compilation errors. If you provide some code you've used, we can help.


OCPJP 6
You Gin
Ranch Hand

Joined: Jul 23, 2010
Posts: 52
Oh, I'm sorry, I just mistyped, I got the error for the first case... Thanks for you reply!
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

You Gin wrote:...but just got a compilator error the x and y references...

Here x and y are primitives - just variables. The term 'reference' is not used for primitives. Just correcting you - never mind.
You Gin
Ranch Hand

Joined: Jul 23, 2010
Posts: 52
Good correction, thanks!
rewati raman
Ranch Hand

Joined: May 22, 2010
Posts: 62
double x,y=0;

and

double x=0;
double y=0;

is not same.

in first case you have declared x but not initialized. it can be done dome thing like this.

double x=y=0;
or
double x=0,y=0;
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

You're welcome!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

rewati raman wrote:double x,y=0;
in first case you have declared x but not initialized. it can be done dome thing like this.

double x=y=0;

That's not the same at all! It would be interpreted as:
double x = (y = 0);
Which would assign 0 to both x and y, but would not declare y. It will only compile if y is already declared.
rewati raman
Ranch Hand

Joined: May 22, 2010
Posts: 62
yeah thats correct. i forgot to mention that
Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

Hi You Gin,

In this case
CASE 1.

a]
and
CASE 2.

a]
or
b]

both CASES i.e. 1 and 2 are different in this manner. each codes written in CASE 2 i.e. a and b all are same. and differ from CASE 1.

in CASE 1, you are trying to declare x but not initializing it and after that declaring and initializing y.
this code will compile in main method; but will cause ERROR! when ever they will be LOCAL VARIABLES. Because, LOCAL variable must me initialized before their use.
But, in CASE 2; you are trying to declare and initialize both in the same line or different lines. So, this will easily compile in all scope either as an instance variable or as a LOCAL Variable.


MCSA 2003 | Preparing For OCPJP/SCJP6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Vishal Kashyap wrote: . . . But, in CASE 2; you are trying to declare and initialize both in the same line or different lines. So, this will easily compile in all scope either as an instance variable or as a LOCAL Variable.
Not quite. In case 2 you are actually initialising the value of y, and later initialising x to the same value as y. That is different from case 3.

I would say that both case 1 and case 2 are writing code difficult to understand.
Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

Ritchie Wrote :
Not quite. In case 2 you are actually initialising the value of y, and later initialising x to the same value as y. That is different from case 3.

I would say that both case 1 and case 2 are writing code difficult to understand.


Ritchie,

Sorry, their is no any difficulty in understanding codes. initializing at first or at last doesn't matter here. ultimately, code will compile or not, is the question.
So, in my opinion CASE 2 will easily compile and as I've written there will be the compilation error in CASE 1.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Vishal Kashyap wrote:. . . their is no any difficulty in understanding codes. . . .
If you find that easy to understand, shall I call you Mr Spock in future?
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Vishal Kashyap wrote:
Sorry, their is no any difficulty in understanding codes. initializing at first or at last doesn't matter here. ultimately, code will compile or not, is the question.
So, in my opinion CASE 2 will easily compile and as I've written there will be the compilation error in CASE 1.

No. Case 2(a) wont compile either, unless you have declared 'y' previously. Thats exactly why its difficult to understand such code. You yourself fell for it - thinking it might compile.
Please have a look at Mathew Brown's post above.
Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

OH!
Sorry....I am really SORRY.....To you all..........JUST MISUNDERSTOOD...........

Yeh,
you are 100% right Ritchie and Vinoth........

point related to declaration of y before assignment was just slipped out of my mind........

thanks for pin-pointing my this tiny mistake.
Fredrik Sassenberg
Greenhorn

Joined: Jul 08, 2009
Posts: 4
Even though it is a perfectly valid I am not a big fan of:



I prefer



or possibly



Depends of course as always
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Double x, y = 0