×

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

Automatic Resource Management

In computer programming, resource management refers to the wide set of techniques for the effective management of the system resources. There are two ways of management of resources: The computer program itself...

2 minutes read.

How to reverse a linked list in java

The process of reversing a linked list in Java will be covered in this section. One of the most common questions in interviews is about reversing a linked list. If...

11 minutes read.

Command Class in Java

We use the command class to run commands against the database. The Command class may find a set of parameter objects for use in sending values to a stored procedure...

3 minutes read.

Java Linters

When it comes to programming, everyone makes mistakes. Errors are bad for developers since they are difficult to handle. But handling as many as possible errors will bring out the...

6 minutes read.

PriorityQueue in Java

PriorityQueue in Java A PriorityQueue is a member of the Java Collection Framework and is used when the values are needed to be processed based on priority. Priority queue operates similar to...

8 minutes read.

Java String concat() method:

Java String concat() method is used to add the given String to the end of the current String. Syntax: public String concat(String str) Parameter: Str: String to be concatenated at the end of current...

1 minute read.

How to open the Java control panel

The many methods for launching the Java control panel will be covered in this section. We will also go through how the Java Control Panel can be used. Java Control Panel The...

2 minutes read.

Java RMI

What is RMI in Java? A framework for developing distributed Java applications is provided by the RMI (Remote Method Invocation) API. An object can call methods on an object running in...

4 minutes read.

Java Boolean Keyword

Boolean keyword In java programming language we basically have two types of primitive data types, Boolean and Numeric (integer and floating-point data types). In this article we are going to learn...

4 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Reverse a String using Collections in Java

Generally, a String is a grouping of characters. Yet, in Java, a String is an item that addresses a succession of characters. The Java.lang.String class is utilized to make a...

5 minutes read.

Singleton Design Pattern in Java

In the singleton pattern, a class that only has one instance and offers a universal point of access is taken into consideration. It can also be defined in another way, a...

4 minutes read.

Java Delete File

There are two techniques to erase a record in Java: Utilizing File.delete() technique.Utilizing File.deleteOnExit() technique. Using File.delete() technique: In Java, we can erase a document by utilizing the File.delete() technique for File class....

2 minutes read.

Prime Number Program in Java Using a Scanner

In Java, a prime number is one that can only be divided by one or by itself and is greater than one. In other words, only one or itself can...

3 minutes read.

Shift right zero Fill Operator in Java and Operator Shifting

Left shift operator ( << ) : The left shift operator is an operator which performs its action at the bits level of a binary Operator. That means when you perform...

4 minutes read.

How to enable java in chrome

The Java module is huge for the Java Runtime Environment (JRE). It permits a program to work with the Java stage to run Java applets. Essentially, each of the undertakings connect...

3 minutes read.

Java Thread Dump Analyzer

Thread: A thread is a PC program that is stacked into the PC's memory and is under execution. It tends to be executed by a processor or a bunch of processors....

15 minutes read.

Iterate JSON array in Java

This article is going to equip the knowledge about what JSON array is and how it works and also how is it distinct with the generic array. Json Array Ordered lists of...

4 minutes read.

Order of Execution of Constructors in Java Inheritance

Constructor in Java There are several distinctions between a method and a function object() in Java. The name of the function object()  is identical to the class name. There is no...

4 minutes read.

Java Primitive Data Types

Primitive data types are the simplest data types in a programming language. They’re predefined in the language. The names of the primitive types are quite descriptive of the values that...

2 minutes read.