# SCJP Brainteaser (8)

Shaan Shar
Ranch Hand
Posts: 1249
Here is another.....
Place your declaration of x and i

And the reverse one is also.:

Enjoy.....

Explanations: ???Needed properly....

Joe Harry
Ranch Hand
Posts: 10124
3
Hi Above,

The first one,

public class Sample{
public static void main(String[] args) {
byte i, x = 1;// Put your declarations for x and i here
x += i; // Must be LEGAL
x = x + i; // Must be ILLEGAL

}
}

The second one,

public class Sample{
public static void main(String[] args) {
double i, x = 1;// Put your declarations for x and i here
x = x + i; // Must be LEGAL
x += i; // Must be ILLEGAL
}
}

Thanks.

Joe Harry
Ranch Hand
Posts: 10124
3
Hi Sharma Ji,

I have come across this before but I want to know the reason why this difference...When we say the declarations seperately like

double i = 1;
double x = 1;

it works fine, but when we club the declarations like,

double i, x = 1, it says the variable might not be initialized in the expression,x+=i;

but runs fine for x = x + 1;

What is the technical reason behind it?? Explanation would be appreciated.
[ November 10, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]

Shaan Shar
Ranch Hand
Posts: 1249
Originally posted by Jothi Shankar Kumar Sankararaj:

double i = 1;
double x = 1;

it works fine, but when we club the declarations like,

double i, x = 1, it says the variable might not be initialized in the expression,x+=i;

but runs fine for x = x + 1;

writing doesn't means that x is initialized.

that means you are writing same as

Hope it helps you out.

Joe Harry
Ranch Hand
Posts: 10124
3
Hi Sharma Ji,

code:

int x,i=1;

doesn't means that x is initialized.

that means you are writing same as

code:

int x;
int i=1;

Hope it helps you out.

Then how come,

double i, x = 1;// Put your declarations for x and i here
x = x + i; // Must be LEGAL
x += i; // Must be ILLEGAL

Works out?? As per your post, I'm doing like,

double i;
double x = 1;
x = x + i; //How this becomes legal now??? i is still not initialized...

Any thoughts on that??

Shaan Shar
Ranch Hand
Posts: 1249
Originally posted by Jothi Shankar Kumar Sankararaj:
Hi Sharma Ji,

Then how come,

double i, x = 1;// Put your declarations for x and i here
x = x + i; // Must be LEGAL
x += i; // Must be ILLEGAL

Works out?? As per your post, I'm doing like,

double i;
double x = 1;
x = x + i; //How this becomes legal now??? i is still not initialized...

Any thoughts on that??

I got somehow confused,what you are using exactly, and which one is working according to you...

Could you please show us. what you have done as declarations..

Joe Harry
Ranch Hand
Posts: 10124
3
Hi Sharma Ji,

I'm using netBeans 5.0 to run my programs, and now I'm realizing the disadvantage of using an IDE to learn...

When I tried the code below,

double i, x = 1;
x += i; // Must be LEGAL
x = x + i; // Must be ILLEGAL

My IDE showed error only in the first line...I mean I did not compile it and it showed me by underlining the first line in red that variable might not have been initialized but nothing was shown in the next line which confused me. After I commented the first line, I got it cleared.

Anyways, for the first question you posted, the explanation is compound assignment operators does not require an explicit cast but whereas normal operators would result in an int value for which an explicit cast to int is required. On the other hand if one of the variable is of type double, the resultant of the arithmetic operation is promoted to double.

What's the logic behind the second question???

Dan Polak
Ranch Hand
Posts: 32
hello

the first one is easy

short x = 0;
int i = 1;// Put your declarations for x and i here
x += i; // Must be LEGAL
x = x + i; // Must be ILLEGAL

is te second possible ???

Ganesh Pujar
Ranch Hand
Posts: 45
Any explenations for the second? please.

Shaan Shar
Ranch Hand
Posts: 1249
Originally posted by Ganesh Pujar:
Any explenations for the second? please.

First we are expecting some solution atleast, then only we can give some explanations.

Right ?

Ganesh Pujar
Ranch Hand
Posts: 45
Compiler helps!

Above code does not work for BOTH

So that means, we dont have the answer for the ABOVE yet.

Shaan Shar
Ranch Hand
Posts: 1249
Aren's this working for first question.

Just check

Now we are seeking for second question .....

Ganesh Pujar
Ranch Hand
Posts: 45

Joe Harry
Ranch Hand
Posts: 10124
3
Sharma Ji,

Is there a way for the second question??