const & Default Value in Functions :: Java

Started by
7 comments, last by kuphryn 21 years, 10 months ago
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
Advertisement
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]
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
Oh.

That''s is not impressive for Java at all .

Kuphryn
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.
Why doesn''t Java support default parameters?
I hate signatures.
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.
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
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.
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).

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.

This topic is closed to new replies.

Advertisement