Printing An Array giving random values [duplicate] - java

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()

Related

Understanding getClass() in Java [duplicate]

This question already has answers here:
Calling a method inside another method in same class
(5 answers)
Why can a method be called without instance?
(3 answers)
Closed 8 months ago.
getClass() in java is a method which returns runtime class of an object. For example:
String str = "something";
Then str.getClass() will return java.lang.String. That's okay. However, what does it mean when the getClass() called "dependently". I mean, in this case getClass() is called after the . operator. But in some cases, it can be called like getClass().getResourceAsStream(). I don't know what it is actually.

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).

How to access method inside object of object through method refrence [duplicate]

This question already has answers here:
Java 8 chained method reference?
(1 answer)
Multiple lambda method references
(3 answers)
Closed 4 years ago.
There are two classes A & B. For example given below:
class A{
B b;
B getB(){ return b; }
}
class B{
String x = "Test";
String getX(){
return x;
}
}
I have object of A in .map(method_refrence_here_for_getX) and I want to call getX so there is any way to do it?
Although i can do like below:
.map(A::getB)
.map(B::getX)
or
.map( a -> a.getB().getX())
But I want to do something like .map(A::B::getX) I know it's absolutely wrong but I want something like this so anyone can help?
You simply can't. There is no syntax for that and it's called a method reference (method) and you potentially show a field reference, which is not supported and looks weird to begin with IMO.
There is a difference between the two (map(A::getB).map(B::getX) and map(a -> a.getB().getX())) is that you will save an additional method internally, but this is one tiny optimization and you should really look into what is more readable in your opinion.

Java method returning memory address [duplicate]

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).

what is the difference between passing array to a method with array parameter and var-args in java? [duplicate]

This question already has answers here:
varargs and the '...' argument
(3 answers)
Closed 6 years ago.
I have gone through a note on var-args in java. I wondered what is the difference it make with the array parameter while calling a method.
public void doSomething(int[] a){
// some logic here
}
public void doSometing(int... a){
// some logic here
}
the above two methods were called by
int[] x={1,2,3,4,5};
doSomething(x);
is both of them are same or some difference exists?
and is it possible to overload these two methods?
The two method signatures are the same, and they do not allow overloading.
The only difference is that calling doSomething(1, 2, 3) with the vararg signature is allowed, while calling the same with the array signature results in an error.

Categories