How does Java convert integer value into byte?

0 votes
Feb 28 in Java by Phalguni
• 1,010 points
1,296 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 by Avantika
• 1,500 points

edited Feb 28 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 in Java by Sirajul
• 38,820 points
573 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 in Java by Sirajul
• 38,820 points
81 views
0 votes
2 answers

How to convert a byte into Hexadecimal in Java?

private static final String ...READ MORE

answered Aug 29 in Java by Sirajul
• 38,820 points
59 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
55 views
0 votes
2 answers
+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,380 points

edited Nov 6, 2018 by Omkar 10,355 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,110 points
2,296 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,110 points
5,990 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,110 points
714 views
0 votes
1 answer

What is integer boxing in java?

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

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