Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

byte

 
Robbie kyodo
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why
byte = long + byte
is legal ?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not legal. The example?
 
Robbie kyodo
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
byte b = 1;
long l = 1000;
b = l + b;
System.out.println (b);
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I THINK b is not byte any more. It is long.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiler complains: possible loss of precision.
 
Robbie kyodo
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. class Teal {
2. public static void main (String[] args) {
3. byte b=1;
4. long l=1000;
5. b += l;
6. }
7. }

frm Dan's
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robbie -
b = b + l
is different than
b += l
In the second case the compiler puts a (an implicit) cast in for you.
 
Robbie kyodo
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bert
tot
a += b;
is a=a+b
can u break up the above case for me ?
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
byte b=1;
long l=1000;
b += l; //line 3
here on line 3, it is actually the same as:
b=(byte)(b+l);
(JLS 15.26.2 )
which is obviously different from
b=b+l;
 
Robbie kyodo
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is something new.....what the !!@@#@#
 
Robbie kyodo
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kathy
Is compound operator part of the objectives???
 
Robbie kyodo
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is something new.....what the !!@@#@#
 
Robbie kyodo
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is something new.....what the !!@@#@#
 
Robbie kyodo
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is something new.....what the !!@@#@#
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the compound operators like += are part of the objectives and are likely to show up in code examples.
Bill
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robbie -
We talk about it around pages 150-151, also you will find it in the JLS 15.26.2.
The JLS sez:
E1 op= E2 is equivalent to
E1 = (T)((E1) op (E2))
where T is the type of E1.
-Bert
 
Robbie kyodo
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THks guys phew ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic