Top community members
All Wiki Articles Create Wiki Article

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ❤ 💻

If you think sharing knowledge and helping other is valuable.

join our community - Click here

Java - convert String to byte

0 contributions
12 points

In Java, we can convert String to byte in couple of different ways.

Short solutions:

// solution 1
byte num1 = Byte.parseByte("31");

// solution 2
Byte num2 = Byte.valueOf("31");

// solution 3
Byte num3 = new Byte("31");

1. Using Byte.parseByte()

public class Example1 {

    public static void main(String[] args) {
        String str = "31";
        byte num = Byte.parseByte(str);
        System.out.println(num); // 31
    }
}

Output:

31

2. Using Byte.valueOf()

public class Example2 {

    public static void main(String[] args) {
        String str = "31";
        Byte num = Byte.valueOf(str);
        System.out.println(num); // 31
    }
}

Output:

31

3. Using constructor - new Byte(String s)

public class Example3 {

    public static void main(String[] args) {
        String str = "31";
        Byte num = new Byte(str);
        System.out.println(num); // 31
    }
}

Output:

31

 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more