No, length of array is not defined in type
specify the length of the array that it returns
No, declaring the return type of a method as being an array does not include a length.
But, effectively, that does not really matter. To get a similar effect:
- Return a non-modifiable
List rather than an array.
- The calling code can simply ask the list for its size.
…
return List.of( "Bob" , "Newhart" ) ;
Of course the calling code can ask an array for its size as well as asking a list. But using a non-modifiable list locks in that size.
Define a class
Rather than hack an array or list to have certain values in certain slots to communicate meaning implicitly, define a class to represent the semantics of your values explicitly.
Records
The new Records feature of Java 16 (previewed in 14 & in 15), makes this utterly simple.
record Name ( String givenName, String surname ) {}
So you would return an object of this type.
…
return new Name( "Bob" , "Newhart" ) ;
Point example
In your example, define a record named Point.
record Point( int x , int y ) {}
Instantiate.
…
return new Point( 7 , 42 ) ;
Access the data.
System.out.println( "x = " + myPoint.x() ) ;
x = 7