Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8938425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:37:19+00:00 2026-06-15T10:37:19+00:00

Background: I have an assignment where I’m going to pass information through sockets to

  • 0

Background: I have an assignment where I’m going to pass information through sockets to a very limited extent. It can be a maximum of 10 bytes per message and I was thinking I’m gonna send just one byte (since one byte is enough to signal 256 different states in the protocol). Now I start to dig around looking for information about this and I run into a lot of questions. Please correct me where my assumptions are wrong and answer my literal questions if you can.

So there is the primitive data type byte (which is basically a number between -128 and 127 inclusive, right?). If I use

byte b = 125;
System.out.println(b);

…I get the correct number printed to the console and if I try to assign values outside the limits, the compiler complains.

Then we have the class Byte which apparently creates a Byte object from a byte data type (or int as it says in the API):

Byte b = new Byte(20);
System.out.println(b);

This also produces the expected result and 20 is printed to the console and if I try to use a higher number than 127, the compiler complains.

1. What is the difference between data type byte and class Byte? Is it mainly because the class offers a lot of methods like class Integer does for type int?

The next snippet produces weird results (to me):

import java.io.*;

public class ByteTest {

    public static void main(String[] args) {

        DataInputStream in = new DataInputStream(System.in);

        try {
            byte d;
            while((d = in.readByte()) != 0) {
                System.out.println(d);
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        System.exit(0);
        }
    }

2. The input is read and out comes the interpretation of the input as an ASCII character in decimal form (for example, 1 returns 49), followed by two more rows with numbers 13 and 10. why is this?

(It doesn’t matter if I declare d as a Byte or byte, the result is the same and I’ve mixed around with getting the value from Byte b instead and so on but these three lines (or more) are always the result and all I want is the input coming right back at me)

Basically I’m a bit confused by this but in the end, all I want is a reasonable way for these single bytes to be sent and when I send 34, I want the other side to received 34, nothing else.

3. Let’s say I refrain from using the class Byte and just want to send a type byte over a stream. All regular streams and readers seem to read nothing less than an int (which I assume means that they will block until they have at least two bytes of input where as I will only send one). Am I forced to use DataInputStream and DataOutputStream where I have to wrap the type byte in an object Byte or are there other ways?

All of this has made me doubt whether I can trust that an object Byte really just amounts to a byte of data and nothing more… I’m confused 🙁

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:37:20+00:00Added an answer on June 15, 2026 at 10:37 am
    1. Yes. The Byte wrapper also allows representing a nullable byte, or storing byte values into collections and maps, for example.

    2. You send text to the DataInputStream, and this text is encoded to bytes using your platform default encoding. Suppose it’s ASCII, the first character will thus be encoded to a byte, then \r and \n are sent, which are also encoded using ASCII. So the 3 bytes you read are the ASCII-encoded values of your char + \r\n.

    3. The javadoc explains what InputStream.read() does. It reads one byte, and converts it to an int between 0 and 255, in order to distinguish between the byte -1 and the -1 which means “end of stream”. To get a byte from the returned int, check it isn’t -1 (which means end of stream), and cast it to a byte: byte b = (byte) readValue;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing an assignment with pyGame, a very simple one, we have to
I have a program that is going to do some work (in a background
Background: I have an MVC based polaroid object. The model keeps the photo's metadata,
Background I have an array of objects (Users) defined and set as follows: //
Background I have two lists, the first is items which contains around 250 tuples,
Background I have a function that takes a config object as an argument. Within
Background : I have a few different threads which each need to write to
Background: I have a WebPart that makes use of the SPCalendarView control and in
Background I have a dimension table that has a single record for each day.
Background: I have this with rollup query defined in MySQL: SELECT case TRIM(company) when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.