Java method returning memory address [duplicate] - java

This question already has answers here:
How to use the toString method in Java?
(13 answers)
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 4 years ago.
the following function returns a memory address instead of the actual Node its supposed to, any input?:
public Node getNode(){
Node nextnode = new Node(this.title, this.disX, this.disY);
return nextnode;
}
When called such as:
AcNode aNode = new AcNode("Test", 0.5, 0.6)
System.out.println("See next node" + aNode.getNode());
AcNode is a subclass of Node, using the same constructor as a super. Any help?

When you concatenate an object onto a String like you are doing there, the toString method will be called. By default the toString method will be called internally if you don't define a toString method, which will print a "memory address" pointing to the object. So you need to define a toString method in your Node class that returns the information you are looking for in that case.
Alternatively, you can call a getter instead of relying on the toString method:
aNode.getNode().getIdentifier()
or whatever your getter method is called (getIdentifier is just an example).

Related

Printing An Array giving random values [duplicate]

This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
How to use Java's lambda expressions to print an array?
(7 answers)
Closed 3 months ago.
I'm trying to print every item in my array, but it gives me a value like PackOfCrisps#653f6b99. I have tried inputting toString() but that just tells me it cannot be converted to a string.
(PackOfCrisps is a separate class)
I'm really new to this
"PackOfCrisps#653f6b99" is the result of the default Object.toString() function. You need to Override the method in your PackOfCrisps class to actually return something useful.
class PackOfCrisps {
...
#Override
public String toString() {
return String.format("PackOfCrisps (flavor: %s)", flavor);
}
}
In toString() you can return whatever you want, and also include whatever attributes your class has (like flavour for example).
Javadocs for Object.toString() in case you're interested: https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString()

What does this.object.method(this) do? [duplicate]

This question already has answers here:
What is the meaning of "this" in Java?
(22 answers)
Closed 3 years ago.
For example object1 has an instance method1:
public void method1() {
this.object2.method2(this);
}
Would the first 'this' refer to the object 1 and the second 'this' object 2 ?
In your code the this keyword give the method a sense of which variable, object or instance it will call, while this can also call the parents variable, object, instance or method. So when it calls this.object2.method2(this) it actually calling for the object2's method2 with a parameter of the class.
In your scope there is a code that instantiate the object2 and it is declared outside any method of the class. See more in here: use of this (javadocs)

What do the instances of "this" mean in this code? [duplicate]

This question already has answers here:
What is the meaning of "this" in Java?
(22 answers)
Closed 4 years ago.
public class MyResults extends Results {
...public MyResults() {
this(5);
}
public double average() {
return this.getSum()/numberOfCourses;
}
}
What do both instances of ―this mean in the code?
First instance is a call to another constructor in the same class. This is also known as Constructor Chaining pattern. Since you didn't post the entire code we don't know if that other constructor is defined (it should be, otherwise you'll have a compile time error).
Second instance is a call to the getSum() method. This method might be defined either in MyResults class or Results class (or some parent class of Results, if any).

Java argument passing [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 8 years ago.
I know there have been several posts explaining argument passing in Java. I also know that the arguments are passed by value but the value is the reference to the object. Say I have the following 2 mthods.
public void append(List<String> list){
list.add("some string");
}
public void incr(int count){
count++;
}
If I call the first method, the object list gets modified and the modified value exists after the method call too. But when I call incr(count), the modified value of count is lost once the method call returns.
Why is it that in some cases, the value gets modified but in the other it does not?
I also know that the arguments are passed by value but the value is the reference to the object.
That is not what java does. Java is pass by value. If something is a reference type, then that reference is passed by value. But that is not the same as being pass by REFERENCE. So, in your second example, the count variable is passed by value, so your changes are lost.

Does a List object get passed by reference? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is Java pass-by-reference?
Does a List object get passed by reference? In other words, if I pass an ArrayList (java.util.ArrayList) object to a class, will it be automatically updated when I change it?
in other word: If I pass an ArrayList (java.util.ArrayList) object to a class, will it be automatically updated when I change it?
Yes
Does the List object passed by reference?
Value of reference would get passed
public updateList(List<String> names){
//..
}
Explanation
When you call updateList(someOtherList); the value of someOtherList which is a reference will copied to names (another reference in method, bit by bit) so now both of them are referring to same instance in memory and thus it will change
See
Is Java "pass-by-reference" or "pass-by-value"?
Yes, a List that you pass to a method is passed by reference. Any objects you add to the List inside the method will still be in the List after the method returns.
If you add to a list in one method, its original reference in first method will also contain the new item.
java is pass by value, and for objects this means the reference is passed by value.
Yes, because You just pass a reference of ArrayList-Object, to your Object.

Categories