12

is it possible to pass primitive data to a method as argument in java, without wrapping them into corresponding class objects ???

Synesso
  • 37,610
  • 35
  • 136
  • 207
Mahesh Gupta
  • 2,688
  • 9
  • 30
  • 46
  • 1
    Just to be a bit pedantic as I completely understand what you're asking, nothing is passed by reference in Java, everything is pass by value. – Chris Thompson Jan 05 '11 at 05:16
  • Also, see this http://stackoverflow.com/questions/2113656/how-to-overcome-the-fact-that-primitives-are-passed-by-value – Chris Thompson Jan 05 '11 at 05:18

4 Answers4

19

Don't do this, but since you asked....you can put them in 1 element lengthed arrays, and pass the array.

MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
13

In Java, it is not possible to pass primitives by reference. To emulate this, you must pass a reference to an instance of a mutable wrapper class.

See How do I pass a primitive data type by reference? for more info on mutable wrapper classes.

Community
  • 1
  • 1
Mike Clark
  • 10,027
  • 3
  • 40
  • 54
2

No, it is not possible in Java.

卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130
2

Short Answer: No Way!!

Long Answer: Primitive data are pass by value not by reference, why? Because they are not object.

"Primitive values do not share state with other primitive values." Oracle Official Tutorial

Nevertheless if you still are intending to do this you may wanna take a look into Wrapper classes, each primitive has its homologue as a Wrapper

If you have a question when to use primitive or wrapper take a look to "When to use primitive and when reference types in Java"

Community
  • 1
  • 1
Necronet
  • 6,704
  • 9
  • 49
  • 89