Archived

This topic is now archived and is closed to further replies.

const & Default Value in Functions :: Java

This topic is 5671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am a C++ and MFC programmer. I love C++ and MFC!!! Nonetheless, I have to take either Java or Visual Basic for a degree at my college. So, I decided to take Java for the summer. Java is very, very easy. Java is weak!!! Furthermore, it is so similar to C++ that t seems like I am programming in C++ and using "Java Foundation Class" (a simile to Microsoft Foundation Class). Anyways, I have two fundamental questions. First, in C++, you can declare a function "const" indication it does not make any changes to a member variable. Java as has const. I tried using const for function declaration the same way as I do in C++; however, it did not work. How do you declare a function as constant in Java? Also, how do you declare a variable as constant in Java? Lastly, in C++, you can give default values to function paramenters. For example: ----- class CPlusPLusRules { public: void GetExample(int nValue = 0, double nSpeed = 1.2); } ----- I tried that techinque in Java; however, it did not work. Is there a way to declare a function similar to the C++ example above in Java? Thanks, Kuphryn

Share this post


Link to post
Share on other sites
quote:
Original post by kuphryn
Anyways, I have two fundamental questions. First, in C++, you can declare a function "const" indication it does not make any changes to a member variable. Java as has const. I tried using const for function declaration the same way as I do in C++; however, it did not work. How do you declare a function as constant in Java?

Also, how do you declare a variable as constant in Java?


Java has the 'final' keyword, but it's not the same as const. A final varable can only be set when it is initalized, however, any members of that final variable are not protected and can be changed at any time. Declaring a function final in Java is not the same as declaring one const in C++. In Java it means that that function cannot be over-ridden by subclasses. Declaring a class final keeps it from being inherited from at all.
This is as close as Java gets to const.

quote:
Lastly, in C++, you can give default values to function paramenters. For example...


Java has no default parameters, you have to emulate it with overloading:

class JavaRules
{
public void getExample()
{
getExample(0);
}
public void getExample(int nValue)
{
getExample(nValue, 1.2);
}
public void getExample(int nValue, double nSpeed)
{
//...
}
}




[edited by - wayfarerx on June 4, 2002 5:38:26 PM]

Share this post


Link to post
Share on other sites
quote:
That''s is not impressive for Java at all .


Fans of Java would claim that Java doesnt need the programmer making functions const, the compiler takes care of optimizations without any hints from the programmer.
This has some good and bad effects, of course its nice for the programmer to forget about const or non-const(less complexity is always good), but if the compiler isnt as smart as it thinks and doesnt optimize where it can, then its just a missing feature.

Share this post


Link to post
Share on other sites
quote:
Original post by Ziphnor
This has some good and bad effects, of course its nice for the programmer to forget about const or non-const(less complexity is always good), but if the compiler isnt as smart as it thinks and doesnt optimize where it can, then its just a missing feature.

People seem to forget that in Java the optimizations are done at run-time via the JIT, not at compile time.

Share this post


Link to post
Share on other sites
quote:
People seem to forget that in Java the optimizations are done at run-time via the JIT, not at compile time.


Actually i am aware of the advantage of runtime optimizations, im just saying i dont know how good the compilers/run time enviroments actually are at noticing things like functions not changing any member variables.

About the default values, i think its a bit sad that its missing from Java, i dont actually know why they didnt include it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by kuphryn
it seems like I am programming in C++ and using "Java Foundation Class" (a simile to Microsoft Foundation Class).


Java Foundation Classes (JFC) do in fact exist. It''s the stuff contained in the javax package (Swing etc).

Share this post


Link to post
Share on other sites
quote:
Original post by Ziphnor
About the default values, i think its a bit sad that its missing from Java, i dont actually know why they didnt include it.



Because external references are resolved at run-time, not compile-time. It makes things like default parameters difficult to implement. Besides, the same thing can be achieved with function overloading very easily.


codeka.com - Just click it.

Share this post


Link to post
Share on other sites