简体   繁体   English

从Java的构造函数创建对象时出现奇怪的行为

[英]I am getting a weird behavior while creating a object from constructor in Java

I have a constructor which takes some params something like this 我有一个构造函数,需要一些这样的参数

public XYZ(Long param1, boolean param2, Long param3){
  //...
}

when I try to create an object for this class by using new Operator like 当我尝试通过使用新的运算符为此类创建对象时

XYZ xyz = new XYZ(1L, false,4L);

(notice the absence of space between false,4) (请注意,false之间没有空格,4)

My param3 is always getting passed as null to the constructor in java 7. But while I try to evaluate the same expression while debugging in IntelliJ Idea, the object creation works as normal. 我的param3总是作为null传递给Java 7中的构造函数。但是,当我尝试在IntelliJ Idea中进行调试时评估相同的表达式时,对象创建仍然正常进行。

Is this behavior happening due to comma operator ? 由于逗号运算符会发生这种现象吗? . I tried reading java language specs but couldn't find anything regarding this. 我尝试阅读Java语言规范,但找不到与此有关的任何内容。 More over it is written at several places that while object creation comma(,) works as a Separator not as an operator. 它的更多内容写在多个地方,而对象创建逗号(,)充当分隔符而不是运算符。

If I put normal spacing between params everything works normal. 如果我在参数之间放置正常间距,则一切正常。 Can somebody explain this behavior. 有人可以解释这种行为。 If the comma is working as operator then shouldn't there be a compile time error while object creation ? 如果逗号用作运算符,那么在创建对象时是否应该没有编译时错误?

EDIT: 编辑:

Thanks every one for your answers. 谢谢大家的回答。 I am not able to able to replicate the issue outside the production environment, perhaps I am overlooking something important. 我无法在生产环境之外复制该问题,也许我忽略了一些重要的事情。 Please mark this Question as Closed. 请将此问题标记为“已结束”。

This is what I tried: 这是我尝试的:

public class TestClass {

    public TestClass(Long param1, boolean param2, Long param3) {
        System.out.println(param1);
        System.out.println(param2);
        System.out.println(param3);
    }

    public static void main(String[] args) {
        new TestClass(1L, false,2L);
    }
}

And the output is: 输出为:

1
false
2

Maybe you need to give us more information? 也许您需要给我们更多信息?

Java is Strong typed language. Java是强类型语言。 In your code example this means that if your class has single constructor with 3 parameters, then the compiler ensures that the constructor must be explicitly called with 3 parameters. 在您的代码示例中,这意味着如果您的类具有3个参数的单个构造函数,则编译器确保必须使用3个参数显式调用该构造函数。 Run time situations in which a comma is seen as the operator can not happen, because in that case the result is only 2 parameters. 在运行时将逗号视为运算符的情况不会发生,因为在这种情况下,结果只有2个参数。

In Weak typed languages like JavaScript two parameters instead of three can produce null as the third parameter, but not in Java. 在JavaScript之类的弱类型语言中,用两个参数代替三个参数可以将null作为第三个参数,但是在Java中则不能。

So the answer to your question is This cannot happen in Java . 因此,您的问题的答案是: 这在Java中不会发生

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

相关问题 为什么我在 Java 中得到奇怪的运行时? - Why am I getting weird runtime in Java? 在 Java 中创建 object 时未调用默认构造函数 - Default Constructor not called while creating object in Java 在 Cucumber 中共享测试上下文,同时在 java 中创建对象以在所有场景中共享相同的状态,我遇到了异常 - Share Test Context in Cucumber,while creating object in java to share same state for all scenarios i am getting exception 从命令行创建JAR文件时,为什么会得到这个奇怪的输出? - Why am I getting this weird output when creating a JAR file from commandline? 从Java创建Scala对象:构造函数未定义 - Creating a scala object from java: The constructor is undefined 为什么我从静态块中初始化的构造函数中获取Hashmap时获取ExceptionInInitializerError - why I am getting ExceptionInInitializerError while acessing Hashmap from constructor which is initialized in static block 序列化对象时出现异常 - I am getting an Exception while serializing an object 在Java中使用参数化构造函数创建对象时出错 - Error while creating object using parameterized constructor in Java 为什么我的scaleImage方法会得到奇怪的结果? - Why am I getting a weird results from my scaleImage method? 为什么从字符串创建 JSONObject 时会得到一个空对象? - Why am I getting a null object when creating a JSONObject from string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM