• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Byte Data Type

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class BytePractice
{
public static void main(String[] args)
{
byte b1=33;
b1++;
byte b2=55;
b2=b1+1;
System.out.println("value of b1 and b2"+b1+ " "+b2);
}
}
Its givind=g compile time error
I do not understand why
see byte limit is -128 to 127 and here
b2=b1+1//means b2=33+1=34(which also comes under byte limit)
So what is the problem

why error ossible loss of precision
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

see byte limit is -128 to 127 and here
b2=b1+1//means b2=33+1=34(which also comes under byte limit)
So what is the problem


it is correct that byte limit is -128 to 127 , but the + operator in b2=b1+1 is your problem, it works with int values
to get around it, just typecast the result into byte before assignment.
Remember here since you are might lose precision, you need an explicit cast
so you don't blame the compiler after implicit cast.


Hope this helps
 
Sabber bhatia
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks amit
 
reply
    Bookmark Topic Watch Topic
  • New Topic