answersLogoWhite

0


Best Answer

Not without casting. A char is a 16 bit type, whereas a byte is an 8 bit type. Therefore the compiler cannot guarantee that the 16 bit value will fit into the 8 bit value without overflowing. If you attempt to stick a char into a byte, you will get a compiler error. To override this, you can cast the char value to a byte during assignment. However, you might get some unexpected results. A few examples below:

char a = 'A';

byte b = a; //compiler error

char a = 'A';

byte b = (byte)a; //valid, no error. b=65

char a = 172;

byte b = (byte)a; //valid, no error, but b=-84 because of overflow.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can you assign a char value to a variable of datatype of byte in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How is a 20 byte string value stored in memory?

As a 21 byte array of type char (including 1 byte for the null terminator).


What is the datatype of a class?

While there are obvious data types like primitives (boolean, char, byte, short, int, long, float, double) and arrays, really any class which stores some form of information can be considered a data type. Objects like String, BigInteger, and the whole Collections framework also belong in this category.


What is a variables in java?

A variable in java is something that holds a value and has a name attached to it. This value can change and hence its named a variable.There are two types of variables in Java:• Primitives - A primitive variable can be one of eight types: char, boolean, byte, short, int, long, double, or float. Once a primitive has been declared, its primitive type can never change, although in most cases its value can change.• Reference variables - A reference variable is used to refer to (or access) an object. A reference variable is declared to be of a specific type and that type can never be changed.


What does possible loss of precision means?

When Java (or another programming language) warns you that there is a possible loss of precision, they mean that you are trying to treat one type of number as a different type.For instance, if you try to store an int value in a byte variable:int i = 10;byte b = i;The int can store more information, so forcing it into a byte may cause a loss of that extra information.In order to work around this, you need to cast the variable to tell the programming language that you really want to convert from one type to the other.int i = 10;byte b = (byte) i;


How many words are there in a 64 bit variable if a word is defined as 2 bytes?

Assuming a byte is 8 bits, then a 2 byte word is 16 bits. Therefore there are maximum of four 2 byte words in a 64-bit variable. Note that a byte is defined as being the smallest unit of addressable storage. As such there is no official standard that dictates its length; it is entirely hardware dependent. Some systems can address at the bit level, thus a byte would literally be just 1 bit in length. Although most systems today use an 8-bit byte, this is not always the case thus the term octet was defined to specifically mean an 8-bit byte.

Related questions

What is the size for long datatype in .net?

Long variables are stored as signed 64-bit (8-byte) integers ranging in value from -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807.


What will be the maximum size of a double variable?

8


Why does a java byte variable only hold a minimum value of -128 and a maximum value of 127. I dont get why only those range of 3 digits. Its just holding 3 characters really. Why not hold AB2?

A byte is 8 binary bits, each of which hold a value of 0 or 1 (true or false). When counting in binary, a value of 11111111 is the highest value a byte can hold, this is 255. It doesn't matter what programming language is assigning a value to the byte, the highest it can hold is 255. A 'signed' byte uses one bit for the sign, and 7 for the value. Hence 7 bits can show values of up to 128 either side. That's a positive value of 1-127, along with the 0, and then negative values of -1 to -128. Again, regardless of the system assigning the value, 8 bits can only produce 255 different combinations.


How do you write a function which takes 2 arguments - a byte and a field in the byte and returns the value of?

bool F1(int byte,int pos) { return(byte & 1<<pos) } //pos -> position in the field // say byte is b1011 and pos is 2 then it will return value 0


How 1000 can be arranged in 1 byte?

It can't. The maximum value of a single byte is 255.


How is a 20 byte string value stored in memory?

As a 21 byte array of type char (including 1 byte for the null terminator).


How data is represented in database i mean if you have given a string like ramu is it stored as ramu or in some other format?

It is better to not concern one's self with the details of internal representation, as that representation is implementation specific. Use the provided SQL to access and format the data. However, to answer the question, for a theoretical RDBMS... If the datatype is fixed character, such as char(8), then the data is stored in 8 bytes, such as 'ramu '. If the datatype is variable character, such as varchar2(8), then the data is stored as a count byte followed by only the data bytes, such as '4ramu'. In this example, we saved 3 bytes, but added the overhead of handling variable length data, including the length byte in every instance of the field. Each approach has its pros and cons. The decision is based on tradeoff between database size and performance. (This particular example is trite and, by itself, meaningless, but these "little" factors can add up.)


What is a set of 8 bits for a computer?

This is typically called a byte, the term was defined this way by IBM for their System 360 series of computers.However IBM first defined the byte as a variable number of bits from 1 to 16 for their 7030 Stretch computer, but by the time the design of the 7030 Stretch was finished this was reduced to a variable number of bits from 1 to 8.


What is the decimal value of byte 2 by itself?

2.0


What is the datatype of a class?

While there are obvious data types like primitives (boolean, char, byte, short, int, long, float, double) and arrays, really any class which stores some form of information can be considered a data type. Objects like String, BigInteger, and the whole Collections framework also belong in this category.


What is a variables in java?

A variable in java is something that holds a value and has a name attached to it. This value can change and hence its named a variable.There are two types of variables in Java:• Primitives - A primitive variable can be one of eight types: char, boolean, byte, short, int, long, double, or float. Once a primitive has been declared, its primitive type can never change, although in most cases its value can change.• Reference variables - A reference variable is used to refer to (or access) an object. A reference variable is declared to be of a specific type and that type can never be changed.


Can a byte hold the value 500?

No. 1 byte = 8 bits. 5000 is represented as 110101110000110000. As the no. of binary digits is more then 8, so 500 cannot be represented in a byte.