Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Passing Data among Methods

 
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain why it doesn't output SPARKY?

 
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java uses pass-by-value. So, when passing a reference variable, you are passing the value of the reference to the object.
The name parameter of the speak() method receives the copy of the reference, so it points to the same String as the variable name inside the main() method.
Now, remember, Strings are immutable, so you can't change the object's value.
Inside your method speak(), you are assigning a new String to your parameter called name, so it will just point to a different String. The name variable inside the main method still points to "Webby".
Thus, the caller is not affected.
 
Prathyu Krishna
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this example..

My question is:
1. Strings are immutable then why does it append "d" ?
2. totally confused what reference variables are?

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prathyu Krishna wrote:1. Strings are immutable then why does it append "d" ?


Because the statement letters+="d"; is equivalent toSo a new String object "abcd" was created, because strings are indeed immutable!

Prathyu Krishna wrote:2. totally confused what reference variables are?


Are you using a study guide to prepare for this certification exam? If you don't, I strongly advise to purchase and use one during your preparation.
 
João Victor Gomes
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prathyu Krishna wrote:2. totally confused what reference variables are?


A reference variable is a variable that contains a reference to an object in the memory (so, the variable holds/points to an object in the memory, or points to null).
For example, assume that you have a class called Car, we can declare a reference variable of it:

Prathyu Krishna wrote:1. Strings are immutable then why does it append "d" ?


Be careful, you are not appending "d" to your String. You are doing a concatenation, which in this case creates a new String (as Roel already explained).
Your method letters() is returning the new String created by the concatenation.

As you see, you are not appending, you are doing a String concatenation, creating a new String, which means that you are not changing the original object (since Strings are immutable).
And be careful with variables and methods names. Search about good practice when naming variables and methods (like not using the same name of a variable to a method)
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

João Victor Gomes wrote://car is a reference variable of type Car. It holds an object of type Car (by assigning new Car() to it).


That's not 100% accurate. It should be: "car is a reference variable of type Car. It holds a reference to an object of type Car (by assigning new Car() to it)".
 
João Victor Gomes
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

João Victor Gomes wrote://car is a reference variable of type Car. It holds an object of type Car (by assigning new Car() to it).


That's not 100% accurate. It should be: "car is a reference variable of type Car. It holds a reference to an object of type Car (by assigning new Car() to it)".



Yes, I mentioned that on my explanation

João Victor Gomes wrote:A reference variable is a variable that contains a reference to an object in the memory



But when talking about the car variable, I was just trying to be more informal, but maybe it wasn't a good idea.
Thanks for de reminder.
 
Prathyu Krishna
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it. Thanks for taking time to explain this so well!
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

João Victor Gomes wrote:But when talking about the car variable, I was just trying to be more informal, but maybe it wasn't a good idea.


In the OCAJP forum there's no room for being informal We have the Meaningless Drivel forum for that
 
Screaming fools! It's nothing more than a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic