简体   繁体   English

覆盖布尔值时会发生什么?

[英]What happens when you overwrite a Boolean value?

This question is in respects to Boolean not boolean (but I would be curious to know if its the same case for both).这个问题是关于布尔而不是布尔的(但我很想知道两者是否相同)。 Let's say you have a function that is performing multiple checks on data, and in each case it has a specific response.假设您有一个函数正在对数据执行多项检查,并且在每种情况下它都有一个特定的响应。 You want the function to show case all the invalid inputs (and why they are invalid) so you want to check all the inputs (not just one and stop and return).您希望该函数显示所有无效输入的大小写(以及它们为何无效),因此您想检查所有输入(不仅仅是一个并停止并返回)。 Would it be a better approach to use a single Boolean value (like Boolean x = isValidEmail(String email)) and then deal with it if its invalid and reuse x in future checks so that you don't allocate additional memory, or should you do (Boolean x = isValidEmail(String email), Boolean y = isValidPassword(String password)).使用单个布尔值(如 Boolean x = isValidEmail(String email))然后在其无效时处理它并在以后的检查中重用 x 以便您不分配额外的内存,或者您应该做(布尔 x = isValidEmail(字符串电子邮件),布尔 y = isValidPassword(字符串密码))。 I hope this question isn't too confusing in the way I've formulated it.我希望这个问题在我制定它的方式上不会太令人困惑。 I've placed an example of the code Im trying to implement to give you an idea.我已经放置了一个我试图实现的代码示例来给你一个想法。

        //Checking if email is valid
        Boolean isValidEmail = registrationService.emailIsValid(user.getEmail());
        if(!isValidEmail) {
            model.addAttribute("invalidEmail", true);
        }
        
        //Checking if email length is valid
        Boolean emailIsValidLength = registrationService.emailIsValidLength(user.getEmail());
        if(!emailIsValidLength) {
            model.addAttribute("invalidEmailLength", true);
        }

As you see here, I create separate Boolean values.正如您在此处看到的,我创建了单独的布尔值。 Is it better to reuse the first Boolean one and do重用第一个布尔值并做会更好吗

IsValidEmail = registrationService.emailIsValidLength(user.getEmail()); 

Instead of creating two separate instances of Boolean?而不是创建两个单独的布尔实例?

It costs the same amount either way, after javac and the JIT have had the chance to optimize.在 javac 和 JIT 有机会优化之后,无论哪种方式,它的成本都是一样的。

Write the clearest thing -- which means using a new variable.写最清楚的东西——这意味着使用一个新变量。

(Note that this is related to how you're not "creating new instances," almost certainly only creating references to existing objects.) (请注意,这与您如何不“创建新实例”有关,几乎可以肯定只创建对现有对象的引用。)

it depends on your case, are you going to use the boolean later on, or validation is a one time thing, this doesn't concern only the Object Boolean,这取决于你的情况,你以后要使用布尔值,还是验证是一次性的,这不仅仅涉及对象布尔值,

for one single use, I usually do this:对于一次使用,我通常这样做:

        if(!registrationService.emailIsValidLength(user.getEmail());) {
            model.addAttribute("invalidEmail", true);
        }
        
        //Checking if email length is valid
        if(!registrationService.emailIsValidLength(user.getEmail());) {
            model.addAttribute("invalidEmailLength", true);
        }

if I need the boolean later on, I store the result in a variable, since why would you call the function again?如果我以后需要布尔值,我将结果存储在一个变量中,因为你为什么要再次调用该函数?

Storing the result in one single variable is also valid if you want to return a global error message.如果要返回全局错误消息,将结果存储在一个变量中也是有效的。

Using seperate Boolean, doesn't hinder java performance at all, If I were you I would pay attention to data structures and time complexity.使用单独的布尔值,完全不会影响 java 性能,如果我是你,我会注意数据结构和时间复杂度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM