$show=/label

Java String copyValueOf​ method example - Internal Implementation

SHARE:

Java String copyValueOf​ method. Learn copyValueOf​ method in Java with example in java.lang.String package and How String copyValueOf​ Method Works internal implementation. Learn about String class methods usage.

Java String copyValueOf​ method:

copyValueOf​ method is present in the String class which is in package java.lang.String. copyValueOf​ is used to convert character array into String that means it creates a new String with contents of array elements. In this tutorial, We will learn String copyValueOf​ method syntax, String copyValueOf​ method example and String copyValueOf​ method internal implementation.

String has overloaded copyValueOf​ methods and both are static methods which can be accessed by class name instead of creating object for String class.



copyValueOf​ Syntax 1: 

public static String copyValueOf​(char[] data);


Returns the string representation of the char array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the returned string.


String copyValueOf​ method


Parameters:

data - the character array.

Returns:

A String that contains the characters of the character array. 

String copyValueOf​ method example 1:


package examples.java.w3schools.string;

public class StringCopyValueOfExample {
 public static void main(String[] args) {

  char[] c = { 'j', 'a', 'v', 'a' };

  String str = String.copyValueOf(c);

  System.out.println("Copy of array as String : " + str);
 }
}


String copyValueOf​ method example 1 Output:

The above program produces the following output.


Copy of array as String : java


Example: What does happen if char array is null?


String str = String.copyValueOf(null);
If char array is null then internally it tried to get the length of the char array length. But in this case, it is null. Hence, will through NullPointerException.


Null case Output:


Exception in thread "main" java.lang.NullPointerException

 at java.base/java.lang.String.(String.java:251)
 at java.base/java.lang.String.copyValueOf(String.java:3017)
 at w3schools/examples.java.w3schools.string.StringCopyValueOfExample.main(StringCopyValueOfExample.java:7)


copyValueOf​ Syntax 2 with offset: 


public static String copyValueOf​(char[] data, int offset, int count);

Returns the string representation of a specific subarray of the char array argument.

The offset argument is the index of the first character of the subarray. The count argument specifies the length of the subarray. The contents of the subarray are copied; subsequent modification of the character array does not affect the returned string.



Parameters:

    data - the character array.
    offset - initial offset of the subarray.
    count - length of the subarray.


Returns:

A String that contains the characters of the specified subarray of the character array.


Throws:

IndexOutOfBoundsException - if offset is negative, or count is negative, or offset+count is larger than data.length. 


copyValueOf​ Syntax 2 with offset Example:

Now, consider the following is the char array.


char[] c = { 'j', 'a', 'v', 'a', '-', 'w', '3', 's', 'c', 'h', 'o', 'o', 'l', 's'};

We want to create the new string for "w3schools" which starts index at 5, need to get 9 characters from 5th position. So, here offset is 5 and count is 9. Refer the below code for offset example.



public class StringCopyValueOfOffsetExample {
 public static void main(String[] args) {

  char[] c = { 'j', 'a', 'v', 'a', '-', 'w', '3', 's', 'c', 'h', 'o', 'o', 'l', 's' };
  String strOffset = String.copyValueOf(c, 5, 9);

  System.out.println("Copy of array as String using offset and count arguments: " + strOffset);
 }
}


copyValueOf​ Syntax 2 with offset Example Output:

The above program produces the following output.

Copy of array as String using offset and count arguments: w3schools

String copyValueOf​ IndexOutOfBoundsException Example:


There are some scenarios it can trigger IndexOutOfBoundsException in the following cases.



1) If offset is negative
2) If count is negative
3) If offset+count is larger than char array length.


Case 1 If offset is negative:


Here, offset value is -2 which is negative that would through IndexOutOfBoundsException.

String strOffset = String.copyValueOf(c, -2, 9);

Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: offset -2, count 9, length 14
 at java.base/java.lang.String.checkBoundsOffCount(String.java:3304)
 at java.base/java.lang.String.rangeCheck(String.java:280)
 at java.base/java.lang.String.(String.java:276)
 at java.base/java.lang.String.copyValueOf(String.java:3006)
 at w3schools/examples.java.w3schools.string.StringCopyValueOfOffsetExample.main(StringCopyValueOfOffsetExample.java:7)


Case 2 If count is negative:

String strOffset = String.copyValueOf(c, 2, -9);

Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: offset 2, count -9, length 14
 at java.base/java.lang.String.checkBoundsOffCount(String.java:3304)
 at java.base/java.lang.String.rangeCheck(String.java:280)
 at java.base/java.lang.String.(String.java:276)
 at java.base/java.lang.String.copyValueOf(String.java:3006)
 at w3schools/examples.java.w3schools.string.StringCopyValueOfOffsetExample.main(StringCopyValueOfOffsetExample.java:7)

Case 3 If offset+count is larger than char array length:

String strOffset = String.copyValueOf(c, 5, 90);


In this case, char array length is 14 but offset+count = 95 which is more than array length.

Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: offset 5, count 90, length 14
 at java.base/java.lang.String.checkBoundsOffCount(String.java:3304)
 at java.base/java.lang.String.rangeCheck(String.java:280)
 at java.base/java.lang.String.(String.java:276)
 at java.base/java.lang.String.copyValueOf(String.java:3006)
 at w3schools/examples.java.w3schools.string.StringCopyValueOfOffsetExample.main(StringCopyValueOfOffsetExample.java:7)


String copyValueOf​ method internal implementation or How String copyValueOf method works inernally:


First, We will see the internal code for these two methods.


1) copyValueOf(char data[]):



    public static String copyValueOf(char data[]) {
        return new String(data);
    }


2) copyValueOf(char data[], int offset, int count):


    public static String copyValueOf(char data[], int offset, int count) {
        return new String(data, offset, count);
    }


These two methods have internally creating string object directly calling constructor. This is very straight forward to understand about internal implementation and how String copyValueOf method works internally. Also, same implementation in java 9 and java 11 versions.


Please leave your questions in comments section. We will reply to your queries.

COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,16,Arrays,7,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,5,Collections,22,Collector,1,Command Line,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,81,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,4,Dictionary,1,Difference,1,Download,1,Eclipse,2,Efficiently,1,Error,1,Errors,1,Exception,1,Exceptions,3,Fast,1,Files,9,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,4,Grant,1,Grep,1,HashMap,1,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,5,Iterate,2,Jackson API,3,Java,29,Java 10,1,Java 11,5,Java 12,5,Java 13,2,Java 14,2,Java 8,66,Java 8 Difference,2,Java 8 Stream Conversions,2,java 8 Stream Examples,3,Java 9,1,Java Conversions,11,Java Design Patterns,1,Java Files,1,Java Program,2,Java Programs,65,java.lang,5,java.util. function,1,jQuery,1,Kotlin,10,Kotlin Conversions,3,Kotlin Programs,6,Lambda,1,lang,29,Leap Year,1,live updates,1,Logging,1,Mac OS,2,Math,1,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,1,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,21,String,58,String Programs,9,String Revese,1,Swing,1,System,1,Tags,1,Threads,10,Tomcat,1,Tomcat 8,1,Troubleshoot,16,Unix,2,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java String copyValueOf​ method example - Internal Implementation
Java String copyValueOf​ method example - Internal Implementation
Java String copyValueOf​ method. Learn copyValueOf​ method in Java with example in java.lang.String package and How String copyValueOf​ Method Works internal implementation. Learn about String class methods usage.
https://2.bp.blogspot.com/-CxP2XPODcl4/XIpKMwvBPvI/AAAAAAAABI4/NfeBnA7Xg7M1u28W3ESnNicLuCQ-MveZQCLcBGAs/s640/String%2BcopyValueOf%25E2%2580%258B%2Bmethod.PNG
https://2.bp.blogspot.com/-CxP2XPODcl4/XIpKMwvBPvI/AAAAAAAABI4/NfeBnA7Xg7M1u28W3ESnNicLuCQ-MveZQCLcBGAs/s72-c/String%2BcopyValueOf%25E2%2580%258B%2Bmethod.PNG
JavaProgramTo.com
https://www.javaprogramto.com/2019/03/java-string-copyvalueof.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2019/03/java-string-copyvalueof.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content