Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does native methods in Java, operating system specific?

Problem Description : - During one discussion I found that programmer stuck at point and I thought I should put on forum.

Today I was searching array conversion into char array. And I checked definition of toCharArray() method of string class.

char str[] = "native".toCharArray();

toCharArray() definition : -

public char[] toCharArray() {
    char result[] = new char[count];
    getChars(0, count, result, 0);
    return result;
}

getChars definition: -

public void getChars(int srcBegin,int srcEnd,char dst[],int dstBegin){
   if(srcBegin<0){
       throw new StringIndexOutOfBoundsException(srcBegin);
   }
   if(srcEnd>count){
       throw new StringIndexOutOfBoundsException(srcEnd);
   }
   if(srcBegin>srcEnd){
       throw new StringIndexOutOfBoundsException(srcEnd-srcBegin);
   }
   System.arraycopy(value,offset+srcBegin,dst,dstBegin,srcEnd-srcBegin);
}

Then a native method: -

public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                         int length);

Here I don't have to understand native but I have to know In such a native method define here in a same way different native method will define for other operating system.

like image 301
shiv.mymail Avatar asked Feb 13 '26 04:02

shiv.mymail


1 Answers

The reason it's native is because this otherwise expensive function can be heavily optimized by using native code.

But the behavior is the same whatever the OS.

like image 179
Denys Séguret Avatar answered Feb 15 '26 17:02

Denys Séguret



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!