Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

pyramid of numbers

 
Marshal
Posts: 68985
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you marked all the methods static in the second version?
Why do your methods in the first version return this rather than an int[] or similar? If you are reading into a field of the current object, why not make the method void? There may be a good reason for returning this: you can write
myObject.foo(...).bar(...).baz(...).biz(...).buzz(...);
If you go through the API for StringBuilder you will see that is how must of its methods are designed.
I suggest you indent your Streams code differently:-You can replace the sort() call with this:-It is probably better to sort the IntStream in line 3 than the Stream<String> in line 2, otherwise "19" will come before "2".

For consecutive numbers, consider starting with the IntStream#range() and IntStream#rangeClosed() methods.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I marked all the static methods in the second version to simplify the code.
In my first version, my methods return this because I wanted to write something like object.method1().method(). It was only for that.

Thanks for your suggestions, it makes the code clearer.    
 
Sheriff
Posts: 7072
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sai rama krishna wrote:
i wonder why the method returns App like below

public App createArrayOfDigits()


It is so the methods can be chained together like this:

[edit] Oops, didn't see that this had already been answered.
 
Ranch Hand
Posts: 542
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saisir les chiffres: 1384
  1
 333
44444
8888888

i am able to see output.

I never saw a method returning its own class object? any other similar use cases or links or examples where method returns its own class object? it looks bit odd as i am familiar with returning String int etc mostly
 
Knute Snortum
Sheriff
Posts: 7072
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sai rama krishna wrote:I never saw a method returning its own class object? any other similar use cases or links or examples where method returns its own class object? it looks bit odd as i am familiar with returning String int etc mostly


The builder pattern uses this technique.  I use this pattern a lot.  It's great for creating objects that have many fields or a lot of optional parameters.
 
sai rama krishna
Ranch Hand
Posts: 542
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sure. I will do that



in above code what is ::

i never seen such operator? please advise
 
sai rama krishna
Ranch Hand
Posts: 542
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instead of  scanner.nextLine() can i use scanner.next()?
what is meaning of nextLine() method here?
i am entering input like
23192
etc in the same line right not next line?
 
Campbell Ritchie
Marshal
Posts: 68985
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The nextLine method returns from the current position of the cursor to the next instance of line end characters.
The next method returns the net token whether it is on the same line or not.
For the :: operator go through the Java™ Tutorials section and look for the part called method references.
 
Knute Snortum
Sheriff
Posts: 7072
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner#nextLine() returns everything up to and including the text separator.  Scanner#next() returns the next token.

In practical terms, next() is the missing nextString() function of Scanner -- with one limitation: it cannot return an empty String.  If you can live with that, only use next().  If you need to be able to get an empty String, things get complicated.  
 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic