How does Java convert integer value into byte?

0 votes
Feb 28 in Java by Phalguni
• 990 points
99 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,460 points

edited Feb 28 by Omkar

Related Questions In Java

0 votes
1 answer

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

private final static char[] hexarr = "0123456789ABCDEF".toCharArray(); public ...READ MORE

answered May 24, 2018 in Java by sharth
• 3,320 points
282 views
0 votes
1 answer

How to convert a java string into byte array

You can probably use ByteBuffer:  byte[] byte = ...READ MORE

answered Jun 21, 2018 in Java by Perry
• 17,010 points
36 views
0 votes
1 answer

How to convert a byte into Hexadecimal in Java?

byte[] bytes = {-1, 0, 1, 2, ...READ MORE

answered Aug 21, 2018 in Java by parth295
• 4,600 points
30 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
36 views
0 votes
1 answer

How do I convert an input stream into a byte array

There is a simple solution without the ...READ MORE

answered Apr 25, 2018 in Java by developer_1
• 3,280 points
182 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,380 points

edited Nov 6, 2018 by Omkar 5,996 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,050 points
1,580 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,050 points
4,247 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,050 points
319 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,460 points
22 views