😊 Fluent 😊, ✨Modern ✨, and 🚀 Blazing Fast 🚀endless loops, made easy.
Requires Java 22 or better.
implementation('dev.mccue:forever:2024.07.09')
Let's build a game where the computer gives words of encouragement to the user. To start, we'll make a small little program that asks for their name.
void main() {
var scanner = new Scanner(System.in);
System.out.print("What's your name?: ");
var name = scanner.nextLine();
System.out.println("You're doing a great job " + name + "!");
}
But whoopsie! It only asks them for their name and gives positive feedback once! We want that to go on forever!!
So let's import the library and get this train going.
import dev.mccue.forever.For;
void main() {
var scanner = new Scanner(System.in);
for (; For.ever ;) {
System.out.print("What's your name?: ");
var name = scanner.nextLine();
System.out.println("You're doing a great job " + name + "!");
}
}
And now we will run that code for forever!
For those who love terse code (and you should, less code is best code 😊) you can leave off one of the fors.
import static dev.mccue.forever.For.ever;
void main() {
var scanner = new Scanner(System.in);
for (;ever;) {
System.out.print("What's your name?: ");
var name = scanner.nextLine();
System.out.println("You're doing a great job " + name + "!");
}
}
But we can do even better 💖. In 2014, Java SE 8 saw the introduction of the concept of lambda expressions. Lambda expressions were a powerful addition to the Java language starting in Java 8.
import dev.mccue.forever.For;
void main() {
var scanner = new Scanner(System.in);
For.ever(() -> {
System.out.print("What's your name?: ");
var name = scanner.nextLine();
System.out.println("You're doing a great job " + name + "!");
});
}
- See the Forever FAQ
- Ask questions on Stack Overflow using the forever tag
- Join us on Slack: #forever-users
- Learn how to contribute
- See issues for issues you can help with
- X: forever-project
- Blog: Forever on Medium