×

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array.

Syntax:

copyValueOf(char[] data)

Parameters:

data : the character array i.e. String

Returns:

It returns a String that contains the characters of the character array.

Java String copyValueOf() Example 1:

public class JavaCopyValueOfEx1 {
   public static void main(String args[]) {
       char[] data = {'a','b','c','d','e','f','g','h','i','j','k'};
       String str1 = "Text";   
       str1 = str1.copyValueOf(data);
       System.out.println("str1 after copy: " + str1);
   }
}

Output:

str1 after copy: abcdefghijk

Java String copyValueOf() Example 2:

public class JavaCopyValueOfEx2 {
   public static void main(String args[]) {
       char[] data = {'a','b','c','d','e','f','g','h','i','j','k'};
       String str1 = "Text";
       String str2 = "String";
       //Variation 1:String copyValueOf(char[] data)
       str1 = str1.copyValueOf(data);
       System.out.println("str1 after copy: " + str1);
       //Variation 2:String copyValueOf(char[] data,int offset,int count)
       str2 = str2.copyValueOf(data, 5, 3 );
       System.out.println("str2 after copy: " + str2);
   }
}

Output:

str1 after copy: abcdefghijkstr2 after copy: fgh

Java String copyValueOf() Example 3:

public class JavaCopyValueOfEx3
{
public static void main(String[] args)
    {
        // Character array with data.
char[] arr_num = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        // Create a String containig the contents of arr_num
        // starting at index 1 for length 2.
        String str = String.copyValueOf(arr_num, 4, 3);
        // Display the results of the new String.
System.out.println("\nThis Java Book contains  " + str +" pages.\n");
    }
}

Output:

This Java Book contains  567 pages.

Java String copyValueOf() Example 4:

public class JavaCopyValueOfEx4 {
  public static void main(String args[]) {
       // Initialising Character array
     char[] ch = {'R', 'O', 'Y', ' ', 'N', 'I', 'T', 'I', 'S', 'H'};
      // Initialising String
     String ch2 = "";    
     // Copying value in ch2
     // now ch2 is equal to "ROY NITISH"
     ch2 = ch2.copyValueOf( ch );     
     // Printing String
     System.out.println("copied string is : " + ch2);
  }
}

Output:

copied string is : ROY NITISH

Related Topics

Java Snippets

The term "snippet" refers to a section of code that addresses numerous issues with just a few lines of code. Decreases the number of lines of code and improves programmer...

3 minutes read.

Advantages and Disadvantages of Strings in Java

What is a String? Java is an object-oriented, platform-independent, high-level, general-purpose, and interpreted programming language.Sun Microsystems is known as the founder of java in 1991; the Java programming language was developed...

4 minutes read.

Java Math exp() Method

The exp() method of Math class returns Euler’s number(e) raised to the power of a double value. Syntax: public static double exp(double a) Parameters: The parameter ‘a’ represents the exponent e. Return Value: The exp ()...

2 minutes read.

System Class in Java

The System class provides features including a way to load files and libraries, standard input, standard output, and errors throughput streams, accessibility to outside defined characteristics and environment variables, and...

3 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.

Beautiful Array in Java

In this section, we will be acknowledged about the beautiful array in Java, its characteristics, how it is achieved. What are the types of approaches and what are the tools...

8 minutes read.

Root exception in java

Java: The main feature of java which is not in C or object oriented programming language is platform independence. Not only the platform independence there are many other features in java...

3 minutes read.

Java String Concatenation

Java String Concatenation Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more...

4 minutes read.

Cyclic Barrier in Java

Programmers often find it challenging to run multiple threads simultaneously. Java introduces the idea of concurrency, which enables us to run many threads concurrently, making this work simpler. Concurrent programming...

6 minutes read.

How to check the Java version in cmd

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated...

5 minutes read.

Menu Driven Program in Java

Menu Driven Program in Java The menu-driven program in Java is a program that displays a menu and then takes input from the user to choose an option from the displayed...

3 minutes read.

Why to use Enum in Java?

In this article, you will be acknowledged about the Enum in java, its uses and mainly its purpose. Enum has various functionalities. Each of them will be discussed. Enum In a computer...

3 minutes read.

Java Math floor() Method

The floor() method of Math class returns the largest double value that is equal to a mathematical integer and is less than or equal to the argument. Syntax: public static double floor(double...

2 minutes read.

Java String Interview Questions

Java String Interview Questions String is the most common and important discussed topics in a Java interview. In Java interview interviewer generally asked three to four questions based on String concept....

16 minutes read.

How to add Elements in Array in Java

Consider an array of the size n, in the given size we must add the elements in the given array. In this tutorial we are preferred to learn how to add...

3 minutes read.

Java Calculate Average of List

The list is a linear data structure used in Java to store ordered data collections. Additionally, it accepts duplicate values while maintaining insertion order. It is sometimes necessary to find...

3 minutes read.

Java Anon Proxy

The Java Anon Proxy (JAP), also known as JonDonym, is a proxy system designed to enable Web browsing with revocable (the use of or publication under a pseudonym, a false...

4 minutes read.

Can Abstract Classes have Static Methods in Java

Abstract Class An abstract class in Java is one that explicitly uses the keyword "abstract" in its declaration. There are options for both non-abstract and abstract techniques (method with the body)....

4 minutes read.

Java 8 Consumer Interface in Java

The Consumer Interface is used to implement the functional programming in Java. The Consumer Interface indicates a function that accepts a single input and outputs a result. These functions don’t...

2 minutes read.