Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Double x, y = 0

 
You Gin
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
You Gin
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I'm sorry, I just mistyped, I got the error for the first case... Thanks for you reply!
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good correction, thanks!
 
rewati raman
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah thats correct. i forgot to mention that
 
Vishal Kashyap
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even though it is a perfectly valid I am not a big fan of:



I prefer



or possibly



Depends of course as always
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic