473,394 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

byte expressions in java

byte b1=10;
byte b2=20;
byte b3=b1+b2;

The above code causes Compile time error.

I understand byte,short and char is treated as int in an expression.

So, i changed the code as:

byte b1=10;
byte b2=20;
byte b3=(byte)(b1+b2);

(or)

byte b1=10;
byte b2=20;
int b3=b1+b2;

Please help me to understand why should it treated as int?

Can it perform addition on byte types itself?
Aug 6 '07 #1
3 3355
r035198x
13,262 8TB
byte b1=10;
byte b2=20;
byte b3=b1+b2;

The above code causes Compile time error.

I understand byte,short and char is treated as int in an expression.

So, i changed the code as:

byte b1=10;
byte b2=20;
byte b3=(byte)(b1+b2);

(or)

byte b1=10;
byte b2=20;
int b3=b1+b2;

Please help me to understand why should it treated as int?

Can it perform addition on byte types itself?
Arithmetic operators - *, /, %, +, - can be applied to numeric types only (at least int) except + which can also be applied to String literals.

See post #2 of the Free classes thread
Aug 6 '07 #2
JosAH
11,448 Expert 8TB
Please help me to understand why should it treated as int?

Can it perform addition on byte types itself?
Nope, just for simplicity reasons; the (virtual) cpu of the (virtual) machine has int
registers only (discaring the floating point processor). Every calculation not
involving floating point or long values implicitly casts every operand to an int
before performing any calculation at all.

The compiler can only detect so much. so: 'byte b1= 10' is fine, but the compiler
can't deduce that 'byte b2= b1+10' is fine as well because it 'thinks' that b1 can
be promoted to an int and maybe the int value doesn't fit in a byte. You should
always cast in such situations.

kind regards,

Jos
Aug 6 '07 #3
prometheuzz
197 Expert 100+
...
but the compiler can't deduce that 'byte b2= b1+10' is fine as well because
it 'thinks' that b1 can be promoted to an int and maybe the int value doesn't
fit in a byte. You should always cast in such situations.
Just a small addition: or declare b1 as final: that way the compiler is reassured that everything will fit in a single byte.

Compile error:
Expand|Select|Wrap|Line Numbers
  1. byte b1 = 10;
  2. byte b2 = b1+5;
No errors:
Expand|Select|Wrap|Line Numbers
  1. final byte b1 = 10;
  2. byte b2 = b1+5;
Aug 6 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: David Cook | last post by:
Java's InetAddress class has some methods that use a byte-array to hold what it describes as a 'raw IP address'. So, I assume that they mean an array like: byte ba = new byte; would hold an...
2
by: Dan | last post by:
Hello. I have recently tried upgrading the MySql connector for my servlet from 2.0.4 to 3.0.9. I have found a minor limitation in 2.0.4 and was hoping that 3.0.9 would fix it. However, now I...
235
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
3
by: Brian Bagnall | last post by:
I'm writing some code that passes values from a Java program to a C++ program using an output stream. The C++ program requires a UBYTE, which is an unsigned byte (values 0 to 255). Java doesn't...
0
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read...
1
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
2
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
77
by: borophyll | last post by:
As I read it, C99 states that a byte is an: "addressable unit of data storage large enough to hold any member of the basic character set of the execution environment" (3.6) and that a byte...
0
by: eitanyan | last post by:
I am trying to create a Java to .Net interop and the way I am doing it is by creating a C# com object and a native unmanaged c++ dll that uses JNIEnv of java. the java is loading the native c++ dll...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.