-1

my question is: Can variables in java call method like this example:

private void test(Rabbit rabbit, byte[] key, byte[] iv, byte[] data) {

    byte[] crypt = rabbit.crypt(data.clone());}

as i know, data is a variable and it calls clone() method. does data variable or no.

Yusra
  • 1
  • 2

4 Answers4

1

In this example data can call method clone, because data is object of byte[] class which is created by JVM, as it is a object can call clone method.

swapnil7
  • 808
  • 1
  • 9
  • 22
0

Your real question here seems to be whether Java array objects support a .clone() method.

The answer is yes. All Java arrays implement Cloneable, and thus the .clone() method will work properly. See Why clone method is allowed on Array?.

Note that this will be a "shallow" clone. That doesn't matter for primitives, but for Objects it means that the clone will contain references to the same objects as the original, not clones of those objects.

Community
  • 1
  • 1
keshlam
  • 7,931
  • 2
  • 19
  • 33
0

Here data type of data variable is a Array. In Java Array is treated as type of Object. Thus you can use data.length and data.clone() as valid statements.

length is a variable(of type int) in Array object and clone() is the method of Object class(Which is the super class of all classes in Java)

Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
0

Well...you're looking at the wrong symbol. The short answer is, "it depends."

In Java, there are two types of things:

  • primitives, which only hold a raw numerical value (in the case of char, often capable of being printed out), and
  • objects, which can not only hold values, but also perform method calls and access instance information.

data is actually a byte[], and array types are special in that they are an Object, but not the instance of a class. That said, there are a few things that it has - clone(), since arrays implement Cloneable, and all of the methods that are found on Object, which it inherits from.

In essence, you can call methods only on objects, but not primitives.

Makoto
  • 104,088
  • 27
  • 192
  • 230