473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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!!
Jul 22 '05 #1
18 2961
> 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/
Jul 22 '05 #2
Ann

"Matt" <jr********@hotmail.com> wrote in message
news:ba*************************@posting.google.co m...
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.
Jul 22 '05 #3
Matt wrote:
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
Jul 22 '05 #4
"Chris Smith" <cd*****@twu.net> wrote in message
news:MP************************@news.altopia.net.. .
<<snip>>
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.
--
Gary
Jul 22 '05 #5
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).

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

Jul 22 '05 #6

"Matt" <jr********@hotmail.com> wrote in message
news:ba*************************@posting.google.co m...
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
Jul 22 '05 #7
* 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.


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?
Jul 22 '05 #8
> > Try Java Language Specification Second Edition 8.6.7 Default
Constructor.

You mean §8.8.7.

<url:

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

That I do.

--
Tony Morris
http://xdweb.net/~dibblego/
Jul 22 '05 #9
jr********@hotmail.com (Matt) wrote in message news:<ba*************************@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.
Jul 22 '05 #10
Tony Morris wrote:
Try Java Language Specification Second Edition 8.6.7 Default

Constructor.

You mean §8.8.7.

<url:

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

That I do.


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
Jul 22 '05 #11
jr********@hotmail.com (Matt) writes:
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.
Jul 22 '05 #12
(That is what you were referring to, right?)

Yep :)

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

Jul 22 '05 #13
> 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.

Jul 22 '05 #14
"Alf P. Steinbach" <al***@start.no> wrote in message
news:41*****************@news.individual.net...
* 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.

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


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
Jul 22 '05 #15
* Gary Labowitz:
"Alf P. Steinbach" <al***@start.no> wrote in message
news:41*****************@news.individual.net...
* 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.

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.

--
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?
Jul 22 '05 #16
Gary Labowitz wrote:
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
jo******@indiana.edu
Jul 22 '05 #17
Gary Labowitz wrote:
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
Jul 22 '05 #18
"KiLVaiDeN" <Ki*******@CaRaMaiL.CoM> wrote in message news:<41***********************@news.free.fr>...
"Matt" <jr********@hotmail.com> wrote in message
news:ba*************************@posting.google.co m...
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


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.
Jul 22 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: The Directive | last post by:
This code will not compiled: In main function: Dot temp = new Dot( *(new Point( 5, 5 )) ); In Dot class: //Constructor with default arguments. Dot::Dot( Point& point= *(new Point( 5, 5...
10
by: cppaddict | last post by:
Hi, I am writing a program and needs to know one of its object members before it can be initialized. It doesn't really matter for my question (which a C++ question, not a windows question), but...
10
by: Ook | last post by:
I'm having trouble comprehending what exactly "default construction" is. I know how to provide a constructor with initial values, so that if I, for example, in my code do this: MyClass...
2
by: Andrew Robinson | last post by:
I need to create a shared static field for use within a number of different classes. Which one should I be using or are they all really the same thing? public class Widget { private Widget() {}...
7
by: John Grandy | last post by:
make a call to XML Web Service WebMethod ... returns object myArray with no error ... myArray contains objects of type StringKeyStringValue runtime error occurs on accessing properties of...
19
by: Andrew J. Marshall | last post by:
I want to create a class that must receive a parameter when instantiated. In other words, I do not want it to have a "Public Sub New()". 1) Does VB.NET create a default public constructor if I do...
8
dmjpro
by: dmjpro | last post by:
hey i m new in Generic Class in java. look at this code carefully .... class GenerericClass<T> { private T d1,d2; public GenericClass() {
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.