How does Java convert integer value into byte

0 votes
Feb 28, 2019 in Java by Phalguni
• 1,020 points
18,403 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

Hope it helps!!

If you need to know more about Java, join our Java online course today.

Thank you

answered Feb 28, 2019 by Avantika
• 1,520 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
• 59,230 points
2,930 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
• 59,230 points
1,473 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
• 59,230 points
2,116 views
0 votes
1 answer

How to convert Java String into byte[]?

The object your method decompressGZIP() needs is a byte[]. So the ...READ MORE

answered Dec 30, 2020 in Java by Gitika
• 65,770 points
1,571 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 10, 2018 in Java by samarth295
• 2,220 points
892 views
+5 votes
4 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,450 points

edited Nov 7, 2018 by Omkar 81,243 views
+1 vote
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,500 points
8,365 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,500 points
17,444 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,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,882 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,520 points
2,766 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP