Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Declarations of variable.....  RSS feed

 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Recently I faced an Interview where I was asked one typical question which I really couldn't replied at that time::

Now I am posting that problem:




I really astonished how this is possible...............???



Early replies will be appreciated..


 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your interviwer must have asked the other way round.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srinivasa Raghavan,
I think your interviwer must have asked the other way round.

code:
--------------------------------------------------------------------------------

class testVar {public static void main(String[] args) {short x = 0;int i = 1;x = x + i; // Must be ILLEGAL x += i; // Must be LEGAL}}

--------------------------------------------------------------------------------

--------------------

Thanks & regards
Srini




I don't know what you understand by this but my problem was as simple as I stated, that is declare x and i in such a manner that

following statement must be a legal statement

x = x + i; // Must be LEGAL

and alongwith the above statement the below statement should be illegal.

x += i; // Must be ILLEGAL



Hope this Time it will be better understandable to all of your ranchers......

 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No updates till now...


Waiting for early replies..???

 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure it wasn't the reverse, with "x+=i" being legal and "x = x + i" being illegal?

UPDATE: oops, just noticed that Srinivasa already asked that.
[ May 03, 2006: Message edited by: Yuriy Zilbergleyt ]
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya I am 100% sure that this is true.......


Now I need some solution.. for this one programming puzzle...

 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No updates till now....


Still waiting for early replies.....






[ May 04, 2006: Message edited by: Ankur Sharma ]
 
seb petterson
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, dont have the solution either. I didnt even know that in:

line 3 was incorrect whereas line 4 was ok.
From java.sun.com:
op1 += op2 Equivalent to op1 = op1 + op2

Is there not a contradiction here?
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seb,

You can check now:


But in this situation condition is reverse and I want



Anyways you now came to get that these are not always similar:

op1 += op2 Equivalent to op1 = op1 + op2



Sure I will definately get my solution from Ranchers......

Waiting for early replies...


[ May 04, 2006: Message edited by: Ankur Sharma ]
 
seb petterson
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ankur Sharma:
Seb,

You can check now:


But in this situation condition is reverse and I want



Anyways you now came to get that these are not always similar:




Sure I will definately get my solution from Ranchers......

Waiting for early replies...



[ May 04, 2006: Message edited by: Ankur Sharma ]



Yes, I noticed that it gave an error. I merely remarking on that the tutorial on suns web seems to be a bit incorrect.

Can anyone explain to me why it works the way it does? I've always thought that op1 += op2 is just "syntactic sugar" for op1 = op1 + op2, which is another way of saying that they are semantically equal which I thought should mean, that if one is correct the other is too.
 
seb petterson
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I think I realized where the error comes from:

Gives an error too, apparently the operation (x + y) returns an int, even if both operands are shorts. I still don't know why there's a difference between x = x + y, and x += y, though.
 
Nam Lam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Does data type matter? If not, then try:



Line 1: dealt with concatenation operator + and the result is a reference to the newly created String object.

Line 2: dealt with assignment operator += and compile time error due to incompatible types.


-Nam.
[ May 04, 2006: Message edited by: Nam Lam ]
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nam Lam,


Hello,

Does data type matter? If not, then try:


code:
--------------------------------------------------------------------------------

Object x = null;String i = null;x = x + i; // Line 1x += i; // Line 2

--------------------------------------------------------------------------------



Line 1: dealt with concatenation operator + and the result is a reference to the newly created String object.

Line 2: dealt with assignment operator += and compile time error due to incompatible types.


-Nam


Ya it's really working but why it is so???

I mean to say that when sun itself declares that both are same then why it is showing this sort of differences...


[ May 04, 2006: Message edited by: Ankur Sharma ]
 
seb petterson
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ankur Sharma:
Nam Lam,
I mean to say that when sun itself declares that both are same then why it is showing this sort of differences...
[ May 04, 2006: Message edited by: Ankur Sharma ]


Well I guess that sun itself is wrong. Obviously the += operator cannot be reduced to the + operator when it comes to Strings.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seb,

Well I guess that sun itself is wrong. Obviously the += operator cannot be reduced to the + operator when it comes to Strings.



Well I am still waiting for the replies of Bartenders ,sheriifs and authors which may lead us to solve this confusion....


Now the ball in the court of above persons........



[ May 05, 2006: Message edited by: Ankur Sharma ]
 
Nam Lam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur Sharma,

As stated,


...

Line 1: dealt with concatenation operator + and the result is a reference to a newly created String object.

Line 2: dealt with assignment operator += and compile time error due to incompatible types.


Line 1: with the string concatenation operator +, if the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l) and, of course, new String object is created from (x + i) containing value "nullnull" assigned to x, legal and no error.

Line 2: the += is the compound assignment operator, according to the Java Language Specification, "All compound assignment operators require both operands to be of primitive type, except for +=, which allows the right-hand operand to be of any type if the left-hand operand is of type String." That is why when compile, receive error:

...incompatible types
found : java.lang.Object
required: java.lang.String
...

For more information, see Java Language Specification

-Nam.
 
Nam Lam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI,

According to Java Language Specification, "A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1."

So, x = x + 7 and x += 7 are not 100% equivalent, i.e.,



-Nam.
[ May 05, 2006: Message edited by: Nam Lam ]
 
seb petterson
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nam,

E1 op= E2 is equivalent to
E1 = (T)((E1) op (E2)), where T is the type of E1

and:

Means that, the illegal code:
is equivalent to the legal code:

[ May 05, 2006: Message edited by: seb petterson ]
 
Nam Lam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seb,

Precisely, Java Language Specification states:

"All compound assignment operators require both operands to be of primitive type, except for +=, which allows the right-hand operand to be of any type if the left-hand operand is of type String.

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once. Note that the implied cast to type T may be either an identity conversion (�5.1.1) or a narrowing primitive conversion (�5.1.3)."

My understanding from the above is:

x = (Object)(x + i); // Legal, EXPLICIT CAST

whereas,

x += i; // IMPLIED x = (Object)((x) + (i)) --> Illegal, implicit cast of += operator can only be applied to a narrowing conversion (see above JLS). In fact, by switching the data type of x and i, the code will compile w/o error, see below:



That's my interpretation from the above JLS. Please let me know if you find a better explaination down the road.

-Nam.

[ May 06, 2006: Message edited by: Nam Lam ]
[ May 06, 2006: Message edited by: Nam Lam ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!