0

I have a simple program as below:

class SerializationBox implements Serializable
{

    private byte    serializableProp    = 10;

    public byte getSerializableProp()
    {
        return serializableProp;
    }

    public void setSerializableProp(byte serializableProp)
    {
        serializableProp = serializableProp;
    }
}

public class SerializationSample
{

    /**
     * @param args
     */

    public static void main(String args[]) 
    {

        SerializationBox serialB = new SerializationBox();
        serialB.setSerializableProp(1); // Here i get an error
}
}

At the indicated place in code I get error that "The method setSerializableProp(byte) in the type SerializationBox is not applicable for the arguments (int)".

I believed that as per the link http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html , I am allowed to pass -128 to 127 as the arguement.

Please let me know what I am missing?

nanosoft
  • 2,913
  • 4
  • 41
  • 61
  • possible duplicate of [How do you specify a byte literal in Java?](http://stackoverflow.com/questions/5193883/how-do-you-specify-a-byte-literal-in-java) – Tarmo Apr 07 '14 at 06:59
  • possible duplicate of [Integer to byte casting in Java](http://stackoverflow.com/questions/7369493/integer-to-byte-casting-in-java) – Rahul Apr 07 '14 at 07:00

3 Answers3

1

You have to cast the integer to byte:

serialB.setSerializableProp((byte) 1);

Notes:

  • When you do

    private byte serializableProp = 10;
    
  • 10 is a integer, not a binary number. To specify that the number is a binary you have to use the following syntax:

    private byte serializableProp = 0b10;
                                    ^^
    
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • One hint... private byte serializableProp = 0b10; will work only in java 1.7 or later as suggested by compiler error Binary literals can only be used with source level 1.7 or greater" – nanosoft Apr 07 '14 at 08:15
1

you are trying to call setSerializableProp() method with a integer literal.That is giving you compilation error.
So down cast the integer literal to byte like below.
setSerializableProp((byte)1)

Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
0

serialB.setSerializableProp((byte)1);

This will explicitly cast your integer literal (1) into a byte

ifloop
  • 8,079
  • 2
  • 26
  • 35