Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Stream API

 
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Can anyone explain how IntStream is being used here? I don't understand the syntax. It looks like a class that is being instantiated, but there is no 'new' keyword. The API says it is an interface too.

 
Saloon Keeper
Posts: 13481
304
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm assuming you're referring to IntStream.range(5, 10). It's a static factory method. It's a static method that creates an instance of some class that implements IntStream. Static methods can be called on a type without creating an instance of that type first (in fact, calling a static method on an object reference is considered poor programming style).
 
Marshal
Posts: 74632
335
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:. . . a static method that creates an instance of some class . . .

One of the things you might notice about the classes introduced in Java8, e.g. those described in this part of the Java™ Tutorials, is that they often have their constructors hidden away and use factory methods to provide instances. There is a section in Joshua Bloch's Effective Java (page 5) about factory methods.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic