Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Constructor

Thread view: 
Gondal - 03 Feb 2006 04:42 GMT
Hello !!
i hava confusion about Constructor is function or procedure with
respect to c/c++
bcos we generaly we define .... function return value but procedure....
plz argu. that!!
Paulus de Boska - 03 Feb 2006 08:16 GMT
A ctor is not a function or, rather, method, hence it never has a
return type. Same in C++.
So you can't even compare it to a method returning void.
A ctor is a ctor, a special code block executed only once when an
instance is created.

---
Paul Hamaker, SEMM
http://javalessons.com
Roedy Green - 03 Feb 2006 11:08 GMT
>i hava confusion about Constructor is function or procedure with
>respect to c/c++
>bcos we generaly we define .... function return value but procedure....
>plz argu. that!!

it is neither fish nor fowl. Constructors are their own thing.  If you
look at one  on the byte code level it is just some code that
initialises fields in an object, so it is closest to an instance
method that returns no value. The constructor does not allocate ram.
It gets passed the ram for the new object pre-zeroed.

see http://mindprod.com/jgloss/contructor.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

zero - 03 Feb 2006 13:51 GMT
"Gondal" <gondal.sgh@gmail.com> wrote in news:1138941736.009024.214550
@g43g2000cwa.googlegroups.com:

> Hello !!
> i hava confusion about Constructor is function or procedure with
> respect to c/c++
> bcos we generaly we define .... function return value but procedure....
> plz argu. that!!

Like the others have said, a constructor is neither.  As an aside, I
personally think the distinction between function and procedure is
nonsensical in languages like C/C++ & Java.  Functions that don't return
anything are just that - functions with a void return type.  IMO the
different names stem from different languages (eg pascal uses procedures),
not from a real functional difference.
Oliver Wong - 03 Feb 2006 17:14 GMT
> As an aside, I
> personally think the distinction between function and procedure is
> nonsensical in languages like C/C++ & Java.  Functions that don't return
> anything are just that - functions with a void return type.  IMO the
> different names stem from different languages (eg pascal uses procedures),
> not from a real functional difference.

   The different names comes from Math. Math defines a function as a
mapping between two sets, called the "domain" and "range". For every element
in its domain (the input), it must map to exactly one element in the range
(the output). Thus, technically, something which "returns" two values is not
considered a function (though it could return a single element, where that
element it itself a set or list or something with cardinality greater than
one; this is like returning a Vector<String> in Java -- you're not returning
multiple strings; you're returning a single vector of strings).

   Functions, according to the mathematical definition, should have not
side effects. A function shouldn't "print to the console" or anything like
that; it should just, given an input, return an output.

   Also note that functions shouldn't have state, nor return something
different given the same input.

   "Sine" and "Cosine" are considered to be functions in Math.

   A procedure, on the other hand, comes from the English term, meaning a
way of doing something. There is nothing implying that a value should be
returned or anything like that. "Place the key in the ignition, and turn
clockwise to start the engine" is a procedure, for example.

   According to the Java Language Specification, Java doesn't have
"functions" or "procedures". They have "methods".

   - Oliver
Stefan Ram - 03 Feb 2006 17:33 GMT
>    Functions, according to the mathematical definition, should have not
>side effects. A function shouldn't "print to the console" or anything like
>that; it should just, given an input, return an output.

 Even in programming languages, functions do not have effects.

 Only the /evaluation/ of a function application might have
 an effect.

 This also happens in mathematics: For example, the evaluation
 of the function "sin( 0 )" by a person might have the side effect
 that he misses his train, because the evaluation takes time.

 The difference is that in mathematics the effect
 of the evaluation is not /specified/ by the function. While the
 evaluation of "sin( 0 )" might have the effect that someone
 misses a train, this is not specified for "sin" to happen.

 In programming, some effects of evaluations of an operation
 invocation are specified for the operation.

>Also note that functions shouldn't have state, nor return
>something different given the same input.

 That is a true difference. However, in slightly complicated
 way a call to a getter can be interpret as a mathematical
 function, that returns a unique get-operation (always the
 same), which then uniquely maps a state to a value. With the
 additional convention that this value is considered as the
 value of the original expression - which is where it
 deviates from mathematical conventions. (This is the essence
 of the use an meaning of monads in functional languages.)
Ranganath Kini - 03 Feb 2006 15:41 GMT
> Hello !!
> i hava confusion about Constructor is function or procedure with
> respect to c/c++
> bcos we generaly we define .... function return value but procedure....
> plz argu. that!!

The Constructor is a special method in a Java class. It is special
because unlike other methods in Java, a class's constructor cannot be
inherited/overridden by a subclass.

Also like C/C++, Java asserts that all methods declare to return
something, or declare to return a void. But constructors get a special
treatment, in that they are not required to declare to return anything.

Hope it helps!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.