• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

SCJP Brainteaser (8)

 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is another.....
Place your declaration of x and i





And the reverse one is also.:




Enjoy.....

Explanations: ???Needed properly....
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sharma Ji,

Your Quote,

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: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:
Hi Sharma Ji,

Your Quote,



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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ???
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any explenations for the second? please.
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aren's this working for first question.

Just check




Now we are seeking for second question .....
 
Ganesh Pujar
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please answer it Sharma Ji
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sharma Ji,

Is there a way for the second question??
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic