This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

confused by primitive types and object refrence  RSS feed

 
Ahmad Auada
Ranch Hand
Posts: 56
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so i was reading this chapter about type wrappers in my book, when I came across this:

"Despite the performance benefit offered by the primitive types, there are times when you will need an object representation. For example, you can’t pass a primitive type by reference to a method."

wait... can't I do this?:


just as I can do this:


I'm confused now!
 
Ahmad Auada
Ranch Hand
Posts: 56
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and what is the difference between instance variable, reference variable, and class variable?
 
Puspender Tanwar
Ranch Hand
Posts: 471
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahmad Auada wrote:and what is the difference between instance variable, reference variable, and class variable?

instance variable- are those which are declared inside a class and are out of all the loops and are not static .
e.g

reference variables - are the variable that refer to any object/instance of a class or the variables that are reference to the object/instance of a class.


class variable - are the variable that are declared static. They are compile time variable.

instance variable share a copy of them with every instance of a class whereas class/static variables share the original value.
means, different instances of a class will have different value of instance variable but will contain the same unique value of class/static varibale

hope it helps
Puspender
 
fred rosenberger
lowercase baba
Bartender
Posts: 12541
48
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can't pass a primitive type by reference to a method. That is key.

A reference is like telling you where something is. If i pass that into a method, the method can then access the same original object and change it. The change is seen after the method exists.

Primitives just pass the value.

think of it this way...

If i give you my home address, you can go to my house and paint my bedroom a different color. When I go home, I'll see that change.

But if instead I hand you a piece of paper that says "my bedroom walls are blue", and you erase that word "blue" and replace it with "green", when I go home, my bedroom walls are still blue.

 
Knute Snortum
Sheriff
Posts: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Despite the performance benefit offered by the primitive types, there are times when you will need an object representation. For example, you can’t pass a primitive type by reference to a method."

Where did you see this? Can you give us the source?

...you can’t pass a primitive type by reference to a method.

This implies that you could pass a variable by reference if it were a wrapper type. This is not true. You can pass nothing by reference in Java. You can pass the value of a reference and work on that variable inside the method, but you're still passing a value.

A better illustration of why you sometimes need a wrapper class is generics. You might be familiar with something like this:

This works because String is a class. But what if you wanted to do this?

It won't compile because int is a primitive, not a class. So you use the wrapper class Integer:
 
Ahmad Auada
Ranch Hand
Posts: 56
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:you can't pass a primitive type by reference to a method. That is key.

A reference is like telling you where something is. If i pass that into a method, the method can then access the same original object and change it. The change is seen after the method exists.

Primitives just pass the value.

think of it this way...

If i give you my home address, you can go to my house and paint my bedroom a different color. When I go home, I'll see that change.

But if instead I hand you a piece of paper that says "my bedroom walls are blue", and you erase that word "blue" and replace it with "green", when I go home, my bedroom walls are still blue.



Your analogy explained it very well
Thank you!
 
Ahmad Auada
Ranch Hand
Posts: 56
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Where did you see this? Can you give us the source?



MCGraw-Hill.Osborne Java A Beginner's Guide 6th edition
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are they suggesting that you can pass a reference type by reference in that book? Java® does not support pass‑by‑reference nor any way to mimic it.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahmad Auada wrote:wait... can't I do this?:

Yes you can; and if the sentence you quoted was verbatim then I'd say it's badly worded. What you can't do (as Knute showed you) is supply a primitive name as a generic type.

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!