I am working with Java8, and I want to know the internal working of ArrayList printing. like if I run my below program, it will give object reference and will not print all of its data members values.
public class Hello{
int a = 1;
int b = 2;
public static void main(String []args){
Hello h = new Hello();
System.out.println(h.a);
System.out.println(h.b);
System.out.println(h);
}
}
Output : 1 2 Hello@2a139a55
And if I use ArrayList it won't print references and will directly print values
import java.util.ArrayList;
public class Hello{
public static void main(String []args){
ArrayList<String> name = new ArrayList<>();
name.add("Monday");
name.add("Tuesday");
System.out.println(name);
}
}
Output: [Monday, Tuesday]
Can you please explain how it is working internally is it dereferencing it like in C language and my second question is how I can implement something similar using object + print in Java?
I read other articles on net on how to print ArrayList using for loop, for each loop and using iterator but none of them accurately specifies how does direct mention of object in print statement returns its values rather than reference.