• Post Reply Bookmark Topic Watch Topic
  • New Topic

primitive data type in Java  RSS feed

 
anish jain
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C++, using '&' we can change the variable of any type including int,float etc.
In Java, Can we change the variable, say of type int, using pass by reference. I know that in Java only objects are called by reference.

But still I want to know if there is any alternative to that.

Also If we can't change the int variable using pass by reference in Java, then why this functionality is removed from java?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not "removed" in Java, it was never added. Presumably the designers thought the benefits weren't worth the added complexity - though you'd have to ask one of them to be sure.

Anyway, there's no direct way to introduce pass-by-reference in Java, but you can encapsulate a primitive within an object and get a similar effect in many cases.
 
anish jain
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since all the wrapper classes are immutable so we can't achieve the similar effect
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your own wrapper class, not one of the built-in ones.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See also this thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!