• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

confused by primitive types and object refrence

 
Ranch Hand
Posts: 56
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and what is the difference between instance variable, reference variable, and class variable?
 
Ranch Hand
Posts: 658
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13073
67
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76450
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
A wop bop a lu bob a womp bam boom. Tutti frutti ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic