• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

operator  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class MainClass{
public static void main(String[] argv){
byte b = 1;
byte b1 =-b;
System.out.println(b1);
}
}

why it gives error like this "possible loss of precision"
 
Sheriff
Posts: 23714
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The range of byte is from -128 to +127. So there exists one byte value for which its negative cannot be represented as a byte. Hence the message.

(If you were thinking that the compiler will recognize that the value of "b" at that point would be 1, that's wrong. The compiler doesn't do any such thing.)
 
Ranch Hand
Posts: 72
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-Edited-
Following post is perfect explanation
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mukesh Prajapati wrote:public class MainClass{
public static void main(String[] argv){
byte b = 1;
byte b1 =-b;
System.out.println(b1);
}
}

why it gives error like this "possible loss of precision"



Hi Mukesh...You are getting error because you are trying to store an integer in byte variable....How? See when you are writing then you are assigning a Java expression(an expression is a combination of Java terms that yields a value) to byte variable b1....When the compiler evaluates an expression it will automatically promote data type (when compiler sees that an expression has a byte or short data type they will be converted to int) i.e when you are assigning -b to b1 then b will be converted to int data type and you cannot store an int value to byte variable..Why value was changed because when you are writing -b this is an expression since you are doing some manipulation....So when you write this way then no errors Since here you are assigning a value not an expression in b1 so no change of data type....So to avoid your error you have to do an explicit cast like this way
 
Mukesh Prajapati
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks

Rameshwar Soni
 
Marshal
Posts: 60795
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Suhas Mandrawadkar wrote:-Edited-
Following post is perfect explanation

Don't change your posts after they have been replied to.
 
Suhas Mandrawadkar
Ranch Hand
Posts: 72
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Suhas Mandrawadkar wrote:-Edited-
Following post is perfect explanation

Don't change your posts after they have been replied to.



I understand. But it was out of context and harmless
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!