DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Demystifying Project Loom: A Guide to Lightweight Threads in Java
  • Auditing Spring Boot Using JPA, Hibernate, and Spring Data JPA
  • How To Validate Archives and Identify Invalid Documents in Java
  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues

Trending

  • How To Aim for High GC Throughput
  • Breaking Down Silos: The Importance of Collaboration in Solution Architecture
  • Message Construction: Enhancing Enterprise Integration Patterns
  • Running Unit Tests in GitHub Actions
  1. DZone
  2. Coding
  3. Java
  4. Borrowing Language Features: Default Argument Values in Java

Borrowing Language Features: Default Argument Values in Java

In PHP, I've come to really enjoy being able to add another argument to a method and give it a default value. Now we'll apply it to Java.

Matthew Schmidt user avatar by
Matthew Schmidt
·
Mar. 28, 08 · Tutorial
Like (1)
Save
Tweet
Share
45.27K Views

Join the DZone community and get the full member experience.

Join For Free

I've been doing a lot of coding in other languages lately, primarily PHP with some Ruby thrown in there, in addition to my regular Java coding and there's one feature that I've been missing from my Java code...

In PHP, I've come to really enjoy being able to add another argument to a method and give it a default value, instantly providing backwards compatibility without needing to add another method that calls yet another method with a default value. Take the following two snippets of code for example:

Java:

public void methodA(String arg1, String arg2) {   methodA(arg1, arg2, "default");}public void methodA(String arg1, String arg2, String arg3) {   // Do something to the arguments there}

PHP:

function funcA($arg1, $arg2, $arg3 = 'default') {  // Do more here}

In our last Javalobby Newsletter, the author argued that Lines of Code is a bad argument, and I tend to agree. My reasoning here isn't because I'm lazy and don't like more lines of code. My argument is that the PHP option is easier to maintain, given that there is only one function. Given Java's speed, what does a function call cost? Is it more expensive to call 2 methods than just a single one?

I'm not saying that this style is for everyone, but I know I would benefit from it. I've seen my share of weirdness from PHP lately, and it has made me appreciate Java and the speed and stability of a Java server more than I ever did before (for example, I never thought that a Java app server was light on memory use :)). Other languages often have nice things we can borrow from them, and I think that this is one of them. What do you think?

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Demystifying Project Loom: A Guide to Lightweight Threads in Java
  • Auditing Spring Boot Using JPA, Hibernate, and Spring Data JPA
  • How To Validate Archives and Identify Invalid Documents in Java
  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: