How does Java convert integer value into byte?

0 votes
Feb 28, 2019 in Java by Phalguni
• 1,010 points
1,956 views

1 answer to this question.

0 votes

When an integer value is converted into a byte, Java cuts-off the left-most 24 bits.

We will be using bitwise AND to mask all of the extraneous sign bits.

Here is an illustration of a Java Program that converts an integer to byte.

import java.util.Scanner;

public class Integer_Conversion

{

   public static void main(String[] args)

    {

        int a;

        byte b;

        Scanner s = new Scanner(System.in);

        System.out.print("Enter any integer:");

        a = s.nextInt();

        b = (byte) a; 

        System.out.println("Conversion into byte:"+b);

    }

}

Output:

Enter any integer: 100

Conversion into byte:100
answered Feb 28, 2019 by Avantika
• 1,500 points

edited Feb 28, 2019 by Omkar

Related Questions In Java

0 votes
2 answers

How can I convert byte array into hex string in Java?

public static String byteArrayToHex(byte[] a) { ...READ MORE

answered Aug 29, 2019 in Java by Sirajul
• 45,950 points
745 views
0 votes
2 answers

How to convert a java string into byte array

Try using String.getBytes(). It returns a byte[] ...READ MORE

answered Aug 22, 2019 in Java by Sirajul
• 45,950 points
100 views
0 votes
2 answers

How to convert a byte into Hexadecimal in Java?

private static final String ...READ MORE

answered Aug 29, 2019 in Java by Sirajul
• 45,950 points
82 views
0 votes
2 answers

How to convert array into list in Java?

Another workaround if you use apache commons-lang: int[] ...READ MORE

answered Aug 9, 2018 in Java by samarth295
• 2,190 points
62 views
0 votes
2 answers

How do I convert an input stream into a byte array

You could probably try this if you ...READ MORE

answered Aug 29, 2019 in Java by Sirajul
• 45,950 points
871 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,400 points

edited Nov 6, 2018 by Omkar 12,930 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,510 points
2,673 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,510 points
6,733 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,510 points
961 views
0 votes
1 answer

What is integer boxing in java?

In Java the boxing range for any ...READ MORE

answered Mar 6, 2019 in Java by Avantika
• 1,500 points
66 views