Connecting Tech Pros Worldwide Help | Site Map

default constructor in Java versus C++

Matt
Guest
 
Posts: n/a
#1: Jul 22 '05
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
Guest
 
Posts: n/a
#2: Jul 22 '05

re: default constructor in Java versus C++


> Any ideas why Java doesn't support that?

This might help.
http://www.google.com/search?hl=en&l...zation+C%2B%2B

--
Tony Morris
http://xdweb.net/~dibblego/


Ann
Guest
 
Posts: n/a
#3: Jul 22 '05

re: default constructor in Java versus C++



"Matt" <jrefactors@hotmail.com> wrote in message
news:ba8a039e.0411141911.cb9a8ad@posting.google.co m...[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.


Chris Smith
Guest
 
Posts: n/a
#4: Jul 22 '05

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.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Gary Labowitz
Guest
 
Posts: n/a
#5: Jul 22 '05

re: default constructor in Java versus C++


"Chris Smith" <cdsmith@twu.net> wrote in message
news:MPG.1c01f18ef94d36c29896d7@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


Tony Morris
Guest
 
Posts: n/a
#6: Jul 22 '05

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
http://xdweb.net/~dibblego/



KiLVaiDeN
Guest
 
Posts: n/a
#7: Jul 22 '05

re: default constructor in Java versus C++



"Matt" <jrefactors@hotmail.com> wrote in message
news:ba8a039e.0411141911.cb9a8ad@posting.google.co m...[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


Alf P. Steinbach
Guest
 
Posts: n/a
#8: Jul 22 '05

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.html#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?
Tony Morris
Guest
 
Posts: n/a
#9: Jul 22 '05

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.html#16823>

That I do.

--
Tony Morris
http://xdweb.net/~dibblego/


hiwa
Guest
 
Posts: n/a
#10: Jul 22 '05

re: default constructor in Java versus C++


jrefactors@hotmail.com (Matt) wrote in message news:<ba8a039e.0411141911.cb9a8ad@posting.google.c om>...

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.
Chris Smith
Guest
 
Posts: n/a
#11: Jul 22 '05

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.html#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?)

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Tor Iver Wilhelmsen
Guest
 
Posts: n/a
#12: Jul 22 '05

re: default constructor in Java versus C++


jrefactors@hotmail.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.
Tony Morris
Guest
 
Posts: n/a
#13: Jul 22 '05

re: default constructor in Java versus C++


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

Yep :)

--
Tony Morris
http://xdweb.net/~dibblego/



bilbo
Guest
 
Posts: n/a
#14: Jul 22 '05

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(String baseName, Locale locale =
Locale.getDefault())

This is equivalent to declaring two functions in Java

ResourceBundle getBundle(String baseName)
ResourceBundle getBundle(String 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(String baseName);
ResourceBundle getBundle(String baseName, Locale locale =
Locale.getDefault());

getBundle("bundleName");

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.

Gary Labowitz
Guest
 
Posts: n/a
#15: Jul 22 '05

re: default constructor in Java versus C++


"Alf P. Steinbach" <alfps@start.no> wrote in message
news:41985c4d.1151729750@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


Alf P. Steinbach
Guest
 
Posts: n/a
#16: Jul 22 '05

re: default constructor in Java versus C++


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

I didn't write that, Tony did.

Learn to quote, Gary.

[color=blue]
> 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.[/color]

Nope.

The Java standard's term for an argument-less constructor in general
is "nullary constructor".

A Java default constructor is not the same as a C++ default constructor.

--
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?
John C. Bollinger
Guest
 
Posts: n/a
#17: Jul 22 '05

re: default constructor in Java versus C++


Gary Labowitz wrote:[color=blue]
> 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.[/color]

Specifically, the first part of JLS(2e) 8.8.7 says:
If a class contains no constructor declarations, then a _default
constructor_ that takes no parameters is automatically provided:

* If the class being declared is the primordial class Object, then
the default constructor has an empty body.
* Otherwise, the default constructor takes no parameters and simply
invokes the superclass constructor with no arguments.

A compile-time error occurs if a default constructor is provided by the
compiler but the superclass does not have an accessible constructor that
takes no arguments.

A default constructor has no throws clause.

It follows that is the nullary constructor of the superclass has a
throws clause, then a compile-time error will occur.

--- end quote ---

Emphasis on the first occurrence of "default constructor" is from the
source -- i.e. the quote is a *definition* of the term. A default
constructor is thus defined to be one provided automatically by Java for
some class, which happens when no other constructor is provided. It has
the properties of taking no arguments, throwing no checked exceptions,
and, except for the constructor for java.lang.Object, doing nothing but
invoking the superclass' no-argument constructor. (And note that the
superclass' constructor is not referred to as a "default constructor".)
Note also in the last sentence quoted, the use of the term "nullary
constructor" to refer to a constructor that takes no arguments.

It is true that some Java programmers rather loosely refer to any
nullary constructor as a default constructor, but this usage is
technically inaccurate, and no one who employs it should be shocked or
offended at being corrected. This is especially true in the context of
discussion occurring in some part in a Java-oriented technical forum
such as comp.lang.java.programmer.


John Bollinger
jobollin@indiana.edu
Chris Smith
Guest
 
Posts: n/a
#18: Jul 22 '05

re: default constructor in Java versus C++


Gary Labowitz wrote:[color=blue]
> 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.[/color]

A couple others have pointed to the language specification's definition
of default constructor, and I don't see any way of reconciling your
interpretation to the document. This is clearly a definition. The word
"default constructor" is italicized in the text, and is contained in a
section entitled "Default Constructor". It contains obviously normative
statements about the default constructor. The text even contains the
phrase "a default constructor that takes no arguments", which would be
somewhat clumsy if the definition of a default constructor were simply
that it takes no arguments.

The JLS uses the phrase "nullary constructor" as others have said, but I
find that relatively few developers are familiar with that term and it
causes confusion, so I prefer to say "no-argument constructor".

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
hiwa
Guest
 
Posts: n/a
#19: Jul 22 '05

re: default constructor in Java versus C++


"KiLVaiDeN" <KiLVaiDeN@CaRaMaiL.CoM> wrote in message news:<419857d6$0$15080$626a14ce@news.free.fr>...[color=blue]
> "Matt" <jrefactors@hotmail.com> wrote in message
> news:ba8a039e.0411141911.cb9a8ad@posting.google.co m...[color=green]
> > 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=green]
> >
> > 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[/color]

And you can this in Java:

public Student(){ //no-arg constructor
this(10, "Joe", /*etc.*/); //call another constructor
}

public Student(int age){
this(age, "Joe", /*etc.*/); //call another constructor
}

//etc.
Closed Thread