-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.

1
  • 3
    There are two types in java: primitive types and reference types. You can only invoke methods on variables of reference types. Commented Feb 19, 2014 at 5:46

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.