default constructor in Java versus C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt

    default constructor in Java versus C++

    I try to compare the default constructor in Java and C++.

    In C++, a default constructor has one of the two meansings
    1) a constructor has ZERO parameter

    Student()
    { //etc...
    }

    2) a constructor that all parameters have default values

    Student(int age = 10, String name = "Joe")
    { //etc...
    }

    However, In Java, default constructor means a constructor has ZERO parameter only.

    Student()
    { //etc...
    }

    The following will yield compile errors
    Student(int age = 10, String name = "Joe")
    { //etc...
    }

    Any ideas why Java doesn't support that?

    Please advise. Thanks!!
  • Tony Morris

    #2
    Re: default constructor in Java versus C++

    > Any ideas why Java doesn't support that?

    This might help.


    --
    Tony Morris



    Comment

    • Ann

      #3
      Re: default constructor in Java versus C++


      "Matt" <jrefactors@hot mail.com> wrote in message
      news:ba8a039e.0 411141911.cb9a8 ad@posting.goog le.com...[color=blue]
      > I try to compare the default constructor in Java and C++.
      >
      > In C++, a default constructor has one of the two meansings
      > 1) a constructor has ZERO parameter
      >
      > Student()
      > { //etc...
      > }
      >
      > 2) a constructor that all parameters have default values
      >
      > Student(int age = 10, String name = "Joe")
      > { //etc...
      > }
      >
      > However, In Java, default constructor means a constructor has ZERO[/color]
      parameter only.[color=blue]
      >
      > Student()
      > { //etc...
      > }
      >
      > The following will yield compile errors
      > Student(int age = 10, String name = "Joe")
      > { //etc...
      > }
      >
      > Any ideas why Java doesn't support that?
      >
      > Please advise. Thanks!![/color]

      Same reason algol60 doesn't support it.
      Maybe you can tell us why C++ doesn't support all the
      algol60 features.


      Comment

      • Chris Smith

        #4
        Re: default constructor in Java versus C++

        Matt wrote:[color=blue]
        > I try to compare the default constructor in Java and C++.
        >[/color]

        Please note that the two languages use that term in different ways.
        It's an implementation-level concept, and naturally implementation-level
        concerns will differ when the language is changed. In this case, it's
        quite by coincidence that the term happens to have a meaning in both
        languages, and it results in some confusion when the C++ meaning is
        applied to Java.

        You seem to be using the C++ definition of "default constructor" below.
        The C++ definition does not apply in Java. In Java, all creations or
        objects MUST specify an argument list for the constructor, so there is
        no "default" in that sense.

        A "default constructor" in Java, though, generally means a constructor
        that's generated for you by the compiler. For example, the following
        class:

        class A { }

        has a default constructor, generated automatically by the compiler,
        which takes no arguments. The following class:

        class B
        {
        public B() { }
        }

        is identical in functionality, but it does *not* have a default
        constructor. Instead, the no-argument constructor is defined quite
        explicitly.
        [color=blue]
        > However, In Java, default constructor means a constructor has
        > ZERO parameter only.[/color]

        It is true that, in Java, all default constructors have zero parameters.
        However, it is not true that all zero-parameter constructors are
        "default", and hence not true that default "means" zero-parameter.
        [color=blue]
        > The following will yield compile errors
        > Student(int age = 10, String name = "Joe")
        > { //etc...
        > }
        >
        > Any ideas why Java doesn't support that?[/color]

        One of the design goals of Java is to simplify the resolution of syntax.
        The task of default parameters is easily accomplished by overloading, so
        there is no need for the redundant mechanism.

        --

        The Easiest Way To Train Anyone... Anywhere.

        Chris Smith - Lead Software Developer/Technical Trainer
        MindIQ Corporation

        Comment

        • Gary Labowitz

          #5
          Re: default constructor in Java versus C++

          "Chris Smith" <cdsmith@twu.ne t> wrote in message
          news:MPG.1c01f1 8ef94d36c29896d 7@news.altopia. net...
          <<snip>>[color=blue]
          > A "default constructor" in Java, though, generally means a constructor
          > that's generated for you by the compiler. For example, the following
          > class:
          >
          > class A { }
          >
          > has a default constructor, generated automatically by the compiler,
          > which takes no arguments. The following class:
          >
          > class B
          > {
          > public B() { }
          > }
          >
          > is identical in functionality, but it does *not* have a default
          > constructor. Instead, the no-argument constructor is defined quite
          > explicitly.[/color]

          I don't think so, Chris. In Java, any constructor that takes no parameters
          is called a default constructor.
          This could be better checked on comp.lang.java. programmer, however.
          --
          Gary


          Comment

          • Tony Morris

            #6
            Re: default constructor in Java versus C++

            [color=blue]
            > I don't think so, Chris. In Java, any constructor that takes no parameters
            > is called a default constructor.[/color]

            This is not true.
            [color=blue]
            > This could be better checked on comp.lang.java. programmer, however.
            > --[/color]

            When did a Usenet forum become the definitive source?
            Try Java Language Specification Second Edition 8.6.7 Default Constructor.

            Chris did mention however that class A{} is functionally equivalent to class
            B{public B(){}}.
            This is not true.
            I'll leave it up to you to figure out why (hint: 8.6.7 also).

            --
            Tony Morris




            Comment

            • KiLVaiDeN

              #7
              Re: default constructor in Java versus C++


              "Matt" <jrefactors@hot mail.com> wrote in message
              news:ba8a039e.0 411141911.cb9a8 ad@posting.goog le.com...[color=blue]
              > I try to compare the default constructor in Java and C++.
              >
              > In C++, a default constructor has one of the two meansings
              > 1) a constructor has ZERO parameter
              >
              > Student()
              > { //etc...
              > }
              >
              > 2) a constructor that all parameters have default values
              >
              > Student(int age = 10, String name = "Joe")
              > { //etc...
              > }
              >
              > However, In Java, default constructor means a constructor has ZERO[/color]
              parameter only.[color=blue]
              >
              > Student()
              > { //etc...
              > }
              >
              > The following will yield compile errors
              > Student(int age = 10, String name = "Joe")
              > { //etc...
              > }
              >
              > Any ideas why Java doesn't support that?
              >
              > Please advise. Thanks!![/color]

              it doesn't support that same syntax, but it can be done easily by simply
              doing a default constructor ( without any parameter ) and inside declaring
              the default values of the variables.

              Student() {
              age = 10;
              name = "Joe";
              //etc..
              }

              I think Java doesn't support the earlier syntax because it makes the syntax
              of the constructor different than the one of other functions, and removing
              that features, gives a function declaration quasi equal to the one of the
              constructors.

              K


              Comment

              • Alf P. Steinbach

                #8
                Re: default constructor in Java versus C++

                * Tony Morris:[color=blue]
                >[color=green]
                > > I don't think so, Chris. In Java, any constructor that takes no parameters
                > > is called a default constructor.[/color]
                >
                > This is not true.
                >[color=green]
                > > This could be better checked on comp.lang.java. programmer, however.
                > > --[/color]
                >
                > When did a Usenet forum become the definitive source?
                > Try Java Language Specification Second Edition 8.6.7 Default Constructor.[/color]

                You mean §8.8.7.

                <url:
                http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.htm l#16823>

                --
                A: Because it messes up the order in which people normally read text.
                Q: Why is it such a bad thing?
                A: Top-posting.
                Q: What is the most annoying thing on usenet and in e-mail?

                Comment

                • Tony Morris

                  #9
                  Re: default constructor in Java versus C++

                  > > Try Java Language Specification Second Edition 8.6.7 Default
                  Constructor.[color=blue]
                  >
                  > You mean §8.8.7.
                  >
                  > <url:
                  >[/color]
                  http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.htm l#16823>

                  That I do.

                  --
                  Tony Morris



                  Comment

                  • hiwa

                    #10
                    Re: default constructor in Java versus C++

                    jrefactors@hotm ail.com (Matt) wrote in message news:<ba8a039e. 0411141911.cb9a 8ad@posting.goo gle.com>...

                    Java default constructor is one that you don't write. Never.
                    It doesn't take parameters.

                    JLS 8.8.7:

                    If a class contains no constructor declarations, then a default
                    constructor that takes no parameters is automatically provided.


                    C++ default constructor is one that you CAN write.
                    It CAN take parameters.

                    ISO/ANSI C++ 12.1.5:
                    A default constructor for a class X is a constructor of class X that
                    can be called without an argument. If there is no user-declared
                    constructor for class X, a default constructor is implicitly
                    declared. An implicitly-declared default constructor is an inline
                    public member of its class.

                    Comment

                    • Chris Smith

                      #11
                      Re: default constructor in Java versus C++

                      Tony Morris wrote:[color=blue][color=green][color=darkred]
                      > > > Try Java Language Specification Second Edition 8.6.7 Default[/color][/color]
                      > Constructor.[color=green]
                      > >
                      > > You mean §8.8.7.
                      > >
                      > > <url:
                      > >[/color]
                      > http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.htm l#16823>
                      >
                      > That I do.[/color]

                      Yes, you're right; the default constructor for A would have had package
                      access instead of public access. Ya learn something new every day. I'm
                      still searching for a situation where it would matter, though, given
                      that the class itself is package-access, so it can't be referenced or
                      subclassed from outside the package.

                      (That is what you were referring to, right?)

                      --

                      The Easiest Way To Train Anyone... Anywhere.

                      Chris Smith - Lead Software Developer/Technical Trainer
                      MindIQ Corporation

                      Comment

                      • Tor Iver Wilhelmsen

                        #12
                        Re: default constructor in Java versus C++

                        jrefactors@hotm ail.com (Matt) writes:
                        [color=blue]
                        > Any ideas why Java doesn't support that?[/color]

                        C++ inherits C's /laizzes-faire/ parameter passing, which allows for
                        arbitrary numbers of method parameters. In Java, a method's parameters
                        are part of the signature.

                        Comment

                        • Tony Morris

                          #13
                          Re: default constructor in Java versus C++

                          (That is what you were referring to, right?)

                          Yep :)

                          --
                          Tony Morris




                          Comment

                          • bilbo

                            #14
                            Re: default constructor in Java versus C++

                            > C++ inherits C's /laizzes-faire/ parameter passing, which allows for[color=blue]
                            > arbitrary numbers of method parameters. In Java, a method's[/color]
                            parameters[color=blue]
                            > are part of the signature.[/color]

                            This is not true. C++ is just as strict as Java about the number of
                            arguments passed to the function matching the number of parameters in
                            the function declaration. However, C++ does have a couple of
                            mechanisms that change this behavior.

                            1. Default parameter values: This is the C++ feature, which Java
                            doesn't have, which the original poster was asking about. In C++ you
                            can declare a function like:

                            ResourceBundle getBundle(Strin g baseName, Locale locale =
                            Locale.getDefau lt())

                            This is equivalent to declaring two functions in Java

                            ResourceBundle getBundle(Strin g baseName)
                            ResourceBundle getBundle(Strin g baseName, Locale locale)

                            Note, it is not like pre-ANSI C where the compiler simply didn't check
                            the number and type of function arguments. The C++ compiler will only
                            allow you to call this with a (String) or (String, Locale) arguments.
                            I assume Sun left this feature out of Java because it adds complexity
                            to the language specification because of the need to specify what
                            happens in situations like:

                            ResourceBundle getBundle(Strin g baseName);
                            ResourceBundle getBundle(Strin g baseName, Locale locale =
                            Locale.getDefau lt());

                            getBundle("bund leName");

                            It's not intuitive which function should be called in this case.
                            Still, I often find myself missing the default parameter values
                            feature.

                            2. The other feature C++ has that Java doesn't (actually Java 1.5 does)
                            is more a holdover from C, and is not regularly used in C++. This is
                            varargs, which allows functions to be declared which take an arbitrary
                            number and type of arguments. I wouldn't say that the existence of
                            this feature justifies your statement though, since it's never used in
                            the standard C++ library, and is rarely used in any C++ libraries that
                            I've seen.

                            Comment

                            • Gary Labowitz

                              #15
                              Re: default constructor in Java versus C++

                              "Alf P. Steinbach" <alfps@start.no > wrote in message
                              news:41985c4d.1 151729750@news. individual.net. ..[color=blue]
                              > * Tony Morris:[color=green]
                              > >[color=darkred]
                              > > > I don't think so, Chris. In Java, any constructor that takes no[/color][/color][/color]
                              parameters[color=blue][color=green][color=darkred]
                              > > > is called a default constructor.[/color]
                              > >
                              > > This is not true.
                              > >[/color][/color]

                              The JLS says: If a class contains no constructor declarations, then a
                              default constructor that takes no parameters is automatically provided:

                              which is true. But if a class contains constructor declarations, you may
                              supply a default constructor that takes no parameters. I believe the use of
                              the word "default" means a constructor that takes no parameters, whether it
                              is explicit or implicit.

                              What I meant by[color=blue][color=green]
                              > > This could be better checked on comp.lang.java. programmer, however.
                              > > --[/color]
                              >
                              > When did a Usenet forum become the definitive source?
                              > Try Java Language Specification Second Edition 8.6.7 Default Constructor.[/color]

                              You mean §8.8.7.

                              was that the whole discussion is a Java topic and could be better discussed
                              with the Java ng. And I see that it is crossposted. And, yes, it is 8.8.7.
                              --
                              Gary


                              Comment

                              Working...