Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8384331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:22:50+00:00 2026-06-09T17:22:50+00:00

java.lang.Boolean is perfect to handle trinary logic, because it can exactly have three states:

  • 0

java.lang.Boolean is perfect to handle trinary logic, because it can exactly have three states: Boolean.TRUE (it is the case that), Boolean.FALSE (it is not the case that) and null (we don’t know what the case is). It would be a nice design to handle this using switch statements, eg. in this constructor:

public class URN {
private String value = null;    

public URN (String value, Boolean mode){
    switch (mode){
        case TRUE:
            if(!isValidURN(value))
                throw new MalformedURLException("The string could not be parsed.");
            this.value = value;
        break;
        case FALSE:
            this.value = value.concat(checkByteFor(value));
        break;
        case null:
            if(isValidURN(value))
                this.value = value;
            else
                this.value = value.concat(checkByteFor(value));
        break;
    }
    return;
}

Unluckily, Java doesn’t allow that, complaining “Cannot switch on a value of type Boolean.” Implementing this results in an obfuscated control flow and unnice code:

public URN (String value, Boolean mode){
    Boolean valid = null;
    if (!Boolean.FALSE.equals(mode)){
        valid = isValidURN(value);

        if (Boolean.TRUE.equals(mode) && !valid)
                throw new MalformedURLException("The string could not be parsed.");

        if(Boolean.TRUE.equals(valid)) {
            this.value = value;
            return;
    }   }

    this.value = value.concat(checkByteFor(value));
}

Doing it the nice way requires to implement an enum class (which, in real life, is more complex than in this example, because .equals() must be rewritten so that Trinary.NULL.equals(null) becomes true) and converting:

private enum Trinary {TRUE, FALSE, NULL};
public URN (String value, Boolean toConvert, String x){

    Trinary mode;
    if(toConvert == null)
        mode = Trinary.NULL;
    else
        mode = toConvert.equals(Boolean.TRUE) ? Trinary.TRUE : Trinary.FALSE;

    switch (mode){
        case TRUE:
            if(!isValidURN(value)) throw new MalformedURLException("The string could not be parsed.");
            this.value = value;
        break;
        case FALSE:
            this.value = value.concat(checkByteFor(value));
        break;
        case NULL:
            if(isValidURN(value))
                this.value = value;
            else
                this.value = value.concat(checkByteFor(value));
        break;
    }
    return;
}

To my eyes, this is the better since more readable solution, but the another half of the origin method size of code just to convert is annoying, and in real life you have to care about two different nulls with the same semantic. Is there a better way to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:22:51+00:00Added an answer on June 9, 2026 at 5:22 pm

    Using a null object to convey information like this is not optimal. Remember that you cannot do any method calls on a null object, which again would mean that if you in the future ever would want to call any .getClass, .equals, .compare etc., you would have to rewrite your code.

    Your best option is definitely to go with the enum option.

    enum Ternary {TRUE,FALSE,UNKNOWN}
    

    You can furthermore expand the class to have a method of getting such object,

    public Ternary getByValue(Boolean o) {
        if(o == null)
            return UNKNOWN;
        if(o)
            return TRUE;
        return FALSE;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Java you can write Boolean.valueOf(myString) . However in Scala, java.lang.Boolean is hidden by
Exception in thread main java.lang.ArrayIndexOutOfBoundsException: -1 i have an array of boolean type which
Consider the following: scala> val a:java.lang.Boolean = true a: java.lang.Boolean = true scala> val
I wrote the following code: import java.lang.*; import DB.*; private Boolean validateInvoice(String i) {
public static JFreeChart createPieChart(java.lang.String title, PieDataset dataset, boolean legend, boolean tooltips, boolean urls) I
I would like to cast org.python.core.PyObject to java.lang.Boolean. Something similar to: boolean i =
Here's my code: package com.eggproject_hu.WPECommerceAdminSales.client; import java.lang.Boolean; import com.google.gwt.core.client.GWT; import com.google.gwt.user.client.Window; public class AblakVillogo
I have met the error when I am implementing a method in Netbeans. java.lang.IllegalArgumentException:
java.lang.VerifyError: (class: a method: parse signature: ()Z) Incompatible argument to function public boolean parse()
The javafx api is defined like this: void addListener(ChangeListener<? super java.lang.Boolean> listener) The following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.