default constructor in Java versus C++

M

Matt

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!!
 
A

Ann

Matt said:
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!!

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

Chris Smith

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

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.
However, In Java, default constructor means a constructor has
ZERO parameter only.

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.
The following will yield compile errors
Student(int age = 10, String name = "Joe")
{ //etc...
}

Any ideas why Java doesn't support that?

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
 
G

Gary Labowitz

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.

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.
 
T

Tony Morris

I don't think so, Chris. In Java, any constructor that takes no parameters
is called a default constructor.

This is not true.
This could be better checked on comp.lang.java.programmer, however.
--

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).
 
K

KiLVaiDeN

Matt said:
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!!

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
 
H

hiwa

(e-mail address removed) (Matt) wrote in message
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.
 
C

Chris Smith

Tony said:

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
 
T

Tor Iver Wilhelmsen

Any ideas why Java doesn't support that?

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.
 
B

bilbo

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.

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.
 
G

Gary Labowitz

Alf P. Steinbach said:
* Tony Morris:

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
When did a Usenet forum become the definitive source?
Try Java Language Specification Second Edition 8.6.7 Default Constructor.

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.
 
A

Alf P. Steinbach

* Gary Labowitz:
I didn't write that, Tony did.

Learn to quote, Gary.

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.

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.
 
J

John C. Bollinger

Gary said:
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.

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
(e-mail address removed)
 
C

Chris Smith

Gary said:
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.

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
 
H

hiwa

KiLVaiDeN said:
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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,527
Latest member
RoxanneTos

Latest Threads

Top