在不同情况下均等

时间:2019-02-04 14:01:24

标签: java equality

我有这个代码...

class Test {

    public static void main(String[] args) {
        Boolean mySuperBoolean = Boolean.FALSE;

        System.out.print("a");
        if (mySuperBoolean = Boolean.TRUE) {
            System.out.print("b");
        }

        System.out.print("c");
    }
}

我是Java的新手,但我知道使用单等号(=)进行赋值。双等于(==)用于检查对象是否被引用到内存中的同一位置。但是,在这种情况下,我不了解如何用单个等号打印'b',但我知道将其更改为双等号不会打印出来

4 个答案:

答案 0 :(得分:1)

Distinct()将为您的var lstItems = recipes.GroupJoin(imageRecipes, // GroupJoin recipes and imagerecipes recipe => recipe.Id, // from every recipe take the primary key imageRecipe => imageRecipe.recipeId // from every imageRecipe take the foreign key (recipe, imageRecipes) => new // from every recipy with all its imageRecipes { // make one new: Recipe = recipe, ImageRecipes = imageRecipes, }) // Groupjoin the Materials: .GroupJoin(tblMaterials, joinResult => joinResult.Recipe.Id // from the Recipe take the primary key material => material.RecipeId // the the Material take the foreign key (joinResult, materials) => new // from every Recipe-with-its-imageRecipes { // and the matching materials, make one new: // select the image properties you plan to use Id = joinResult.Image.Id, Name = joinResult.Image.Name, ... // all ImageMaterials of this Image ImageMaterials = joinResult.ImageMaterials .Where(imageMaterial => ...) // if you want only some ImageMaterials of this Image .Select(imageMaterial => new { // select the imageMaterials you plan to use Id = imageMaterial.Id, ... }) .ToList(), Materials = materials.Select(material => new { ... // the material properties you plan to use. }) .ToList(), } 变量赋值布尔值TRUE。条件将计算为true,因此,如果内部内容始终执行,则为

答案 1 :(得分:1)

赋值运算符mySuperBoolean的结果将是赋值。因此,a = { list: [ {label: 'ola', value: 174} ] } 将始终等于=

答案 2 :(得分:1)

赋值是一个expression,它可以解析为已分配的内容,在这种情况下,
(mySuperBoolean = Boolean.TRUE)是一个表达式,它可以解析为Boolean.TRUE

这实际上仅在某些特定情况下有用。一种这样的情况是以下成语:

String line;
while ((line = readLine()) != null) {
    //...
}

甚至

i = j = k = 0; // equal to: i = (j = (k = 0))

这是一个有争议的功能,因为它允许像您这样的可能错误进行成功编译。为了减轻这种情况,某些人会反转操作数("yoda condition"):

if (Boolean.TRUE == mySuperBoolean)

之所以可行,是因为如果我忘记了第二个等号,那么编译器将抛出错误,因为Boolean.TRUE是最终的并且无法分配给它。

答案 3 :(得分:0)

本质上,这里发生的事情归结为:

if (mySuperBoolean = Boolean.TRUE)

该赋值将TRUE放入变量中,该变量为布尔值,并检查其当前值(故事结尾)。