• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

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
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic