×

Java String getBytes() method

getBytes() method returns sequence of bytes.

Syntax:

There are three variants of getBytes() method:
public byte[] getBytes()       
public byte[] getBytes(String charsetName)
public byte[] getBytes(Charset charset)

Returns:

It returns sequence of bytes.

Java String getBytes() Example 1

public class JavaStringGetBytesEx1
{ 
                     public static void main(String args[])
{ 
            String s1="ghijklm"; 
            byte[] barr=s1.getBytes(); 
            for(int i=0;i<barr.length;i++){ 
             System.out.println(barr[i]); 
     } 
   }
}

Output:

103
104
105
106
107
108
109

Java String getBytes() Example 2

public class JavaStringGetBytesEx2
{
    public static void main(String[] args)
    {
        String str = "tutorialandexample";
        // Copy the contents of the String to a byte array.
byte[] byte_arr = str.getBytes();
        // Create a new String using the contents of the byte array.
        String new_str = new String(byte_arr);
        String str1 = new_str.concat(".com");
        // Display the contents of the byte array.
System.out.println("\nBest online Tutorial site : " +
str1 + "\n");
    }
}

Output:

Best online Tutorial site : tutorialandexample.com

Java String getBytes() Example 3

import java.nio.charset.Charset;
public class JavaStringGetBytesEx3 {
  public static void main(String[] args) {
    try {
      String str = "tutorialandexample.com";
      System.out.println();
      System.out.println("\n Given String = " + str);
      // copy the contents of the String to a byte array
      byte[] arr = str.getBytes(Charset.forName("ASCII"));
      String s1 = new String(arr);
      System.out.println(" New string = " + s1);
            System.out.println();
    } catch (Exception e) {
      System.out.print(e.toString());
      System.out.println();
    }
  }
}

Output:

Given String = tutorialandexample.com
New string = tutorialandexample.com

Java String getBytes() Example 4

import java.nio.charset.Charset;
public class JavaStringGetBytesEx4 {
  public static void main(String[] args) {
    try {
      String str = "tutorialandexample.com";
      System.out.println();
      System.out.println("\n Given String = " + str);
      // copy the contents of the String to a byte array
      byte[] arr = str.getBytes(Charset.forName("ASCII"));
       for(int i=0;i<arr.length;i++){ 
            System.out.println(arr[i]); 
        }
      String s1 = new String(arr);
      System.out.println(" New string = " + s1);
            System.out.println();
    } catch (Exception e) {
      System.out.print(e.toString());
      System.out.println();
    }
  }
}

Output:

Given String = tutorialandexample.com
116
117
116
111
114
105
97
108
97
110
100
101
120
97
109
112
108
101
46
99
111
109
New string = tutorialandexample.com

Related Topics

How to Convert char to int in Java

How to Convert char to int in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

3 minutes read.

Java concurrency interview questions

During technical interviews, one of the most challenging and sophisticated subjects is concurrency in Java. This page offers responses to some of the related interview questions you might come across. 1....

11 minutes read.

this keyword in Java

The 'this' keyword is an essential notion in Java. We'll go through the 'this' keyword in depth and provide some examples of how it's used in Java. In Java, the term...

5 minutes read.

Java Public Keyword

A Java access modifier is a public keyword. It can be applied to classes, constructors, methods, and variables. It is the type of access modifier that is least constrained. A...

4 minutes read.

Difference Between Java and PHP

The two most used programming languages are PHP and Java. Both of them have a lot of similarities and distinctions. Let's first grasp each of them individually before examining their...

3 minutes read.

Date time API in java

Introduction: In this text, we can talk approximately Data time API in java. The java.time, java.util, java.sql, and java.text packages contain classes that represent dates and times. The following classes are...

4 minutes read.

Hamming Code in Java

In a computer network, hamming code is a unique set of error-correction codes. It is mostly utilised in computer graphics for mistake detection and correction during data transmission from sender...

8 minutes read.

Java time local date

Java: Java is one of the programming language which is object oriented. It consists of many features such as robust, simple, architecture neutral, dynamic, distributed, multi threaded, portable etc. The main feature...

3 minutes read.

Creation of Multi Thread in java

What are threads in Java? We can use threads to facilitate parallel processing. Threads are helpful when you wish to execute several pieces of code concurrently. A thread is a small process...

3 minutes read.

Enterprise Java Beans

One of the many Java APIs for the common development of corporate software is Enterprise Java Beans (EJB). An EJB, a server-side software component, contains the business logic of an...

4 minutes read.

Java Math scalb() Method

The scalb() method of Java Math class returns a single perfectly rounded product of floating-point and member of the double value set as if performed by d*2scaleFacor rounded value. Syntax: public static...

2 minutes read.

Find next greater number with same set of digits in Java

It contains a number (num). Finding the smallest number that has the same number of elements as num that is also larger than num is the task at hand. If...

8 minutes read.

How to Convert long to int in Java

How to Convert long to int in Java When we assign a larger type value to a variable of smaller type, then we need to perform explicit casting for the conversion....

2 minutes read.

Java file Reader

File Reader: It is used to read the data from files. This class inherits the properties from Input Stream Reader Class. File Reader is for reading characters from the file. Input Stream: Java.io...

4 minutes read.

Encapsulation Program in Java

Encapsulation Program in Java Encapsulation program in Java demonstrates the technique to bind methods and fields in a single unit. The term encapsulation is inspired by the word ‘capsule’, which is...

3 minutes read.

Java Thread Priority in Multithreading

As we realise, java, being object-situated, works inside a multithreading climate in which the string scheduler relegates the processor to a string in light of the need for a string....

6 minutes read.

How to Convert long to String in Java

How to Convert long to String in java It is used if we have to display a long number in the text field because everything is displayed as a String. A...

3 minutes read.

Heart Pattern in Java

Heart Pattern is yet another intricate pattern program, however, due to its complexity, interviewers hardly ever inquire about it. Method for Printing the Heart Number Pattern Put the value of the total row...

2 minutes read.

Java Control Statements

Control Statements: Control statements in Java can also be referred to as decision-making while dealing with different problems. Control statements are helpful to sort out the flow of the program or...

7 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 minutes read.