Forums Register Login

Double x, y = 0

+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Oh, I'm sorry, I just mistyped, I got the error for the first case... Thanks for you reply!
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Good correction, thanks!
+Pie Number of slices to send: Send
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;
+Pie Number of slices to send: Send
You're welcome!
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
yeah thats correct. i forgot to mention that
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.

+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Even though it is a perfectly valid I am not a big fan of:



I prefer



or possibly



Depends of course as always
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3820 times.
Similar Threads
[easy]Find the number
Enum example from Java Language Specification does not compile.
Mixed bag
question about refactoring conditional logic
Comparison with NaN
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:34:39.