×

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

Comparator vs Comparable in Java

Comparator Vs Comparable in Java In Java, sorting of primitive data types can be done using different inbuilt functions that are available. But for sorting the collection of objects or types...

4 minutes read.

Java String getChars() Method

Java String getChars() method copies characters from current String to the destination character array . Syntax: public void getChars(int srcBeginIndex, int srcEndIndex, char[] destination, int dstBeginIndex) Parameters: srcBegin - index of the first character...

1 minute read.

Abstract classes in Java

Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract...

4 minutes read.

Java Enhanced For Loop (For-each Loop)

JAVA ENHANCED FOR LOOP The enhanced for loop is also called the for-each loop and has some advantages over the regular for loop. A for-each loop is designed to cycle through...

4 minutes read.

Round Robin Scheduling Program in Java

A CPU scheduling technique is known as Round Robin (RR). Additionally, network schedulers employ it. It was created specifically for a time-sharing system. The temporal slicing scheduling algorithm is another...

4 minutes read.

How to compare two dates in different format in Java?

We need to compare two dates frequently when coding. Real-world examples include sorting a list of persons by age or keeping track of students' attendance. To compare two dates, we...

7 minutes read.

Java Coding Software

Desktop and web apps are created using Java, an object-oriented programming language. Java code may be executed on any platform, making it platform-independent. A text editor, tool, or piece of...

9 minutes read.

How to Convert Decimal to Hexadecimal in Java

How to Convert Decimal to Hexadecimal in Java The hexadecimal number uses 16 values to represent a number. Numbers from 0 to 9 represented by digits and the numbers from 10...

3 minutes read.

Java List Implementations

ArrayList and LinkedList are the two implementations of List that are for general-purpose. You'll probably utilise an array list the majority of the time because it's quick and provides constant-time...

3 minutes read.

Java Boolean hashCode() Method

The hashCode() method of Boolean class returns the hash code for the specified Boolean object or the given Boolean value. Syntax public int hashCode()public int hashCode(boolean value) Parameters The parameter ‘value’ represents the value...

2 minutes read.

Java Boolean compareTo() method

The compareTo() method of Java Boolean class compares the Boolean argument with the Boolean instance and returns integer value, zero, or negative 1, or positive 1 based on the result...

2 minutes read.

How to override toString() method in Java?

Java is an object-oriented language. It only works with classes and objects. Thus, whenever we need to calculate, we need an object or objects that belong to the class. The Java method...

2 minutes read.

Relatively Prime in Java

In this article, you will be very well equipped with a knowledge of a relatively prime number and also Java programs to determine whether a given integer is a relatively...

4 minutes read.

String Declaration in Java

A string is a group of characters. In Java, the string can be treated as both class and datatype. In Java programming, the String class have many advantages. Everything in...

3 minutes read.

Ramanujan Number or Taxicab Number in Java

In this section, we will discuss what a Ramanujan number (also known as a Hardy-Ramanujan number) is and how to use a Java programme to determine if a given integer...

3 minutes read.

How to Compare Two Strings in Java

How to Compare Two Strings in Java On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting...

4 minutes read.

Convert List to String in Java

We occasionally need to create a string from a list of characters. Since a string is only a collection of characters, a character array can be converted into a string....

6 minutes read.

Caesar Cipher Program in Java

It is among the most popular and straightforward encryption methods. With this method, each letter in the text is swapped out for a letter that is a certain number of...

3 minutes read.

How to sort an array in Java

Sorting is the process of arranging the elements of a list or array in a specific order, either ascending or descending. The sorting criterion numerical and alphabetical is commonly used...

6 minutes read.

Java Math asin() Method

The asin() method of Math class computes the trigonometric Arc Sine (inverse of sine ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double asin(double a) Parameters: The...

1 minute read.