[Java lista] final formalis parameter
Zoltan Mozes
Zoltan_Mozes at epam.com
2006. Dec. 15., P, 11:17:35 CET
szia!
Egyrészt optimálisabb lesz (C esetén ilyenkor ha tudja, akkor proci-regiszterbe teszi ehlyből, nem stack/heap whatever , hogy java-nál hogy van azzal kapcsolatban majd tapasztaltabban kisegítenek minket :))
Thinking in Java idevonatkozó része: (jó, static-ról beszél, na) :L)
"static final variables can be optimized by the JVM to improve program speed. Program constants should thus be declared as static and final. "
Sokkal fontosabb hogy ha nem fog változni a metóduson belül akkor azért (si) érdemes finalt-t elétenni (és itt jön be a Thinking in Java és főle persze MÁS könyvek is), mert ez közelebb viszi a kódodat a DBC szemléletű fejlesztéshez.
DBC == Design By Contract, aminek egyik eleme az amiről msot beszélünk:
"Invariants" = a metódustörzsben jelen esetben. (assert ill/és/vagy final)
(de a kék+zöld könyv is jó, csak abból ilyeneket nem fogsz kiolvasni...)
Persze mindig az a kérdés hogy mi a cél.
szóval az idevonatkozó rész:
" Design by Contract (DBC) is a concept developed by Bertrand Meyer, creator of the Eiffel programming language, to help in the creation of robust programs by guaranteeing that objects follow certain rules that cannot be verified by compile-time type checking.[93] These rules are determined by the nature of the problem that is being solved, which is outside the scope of what the compiler can know about and test."
(...)itt ugrunk pár bekezdést(...)
"
Invariants
An invariant gives guarantees about the state of the object that will be maintained between method calls. However, it doesn't restrain a method from temporarily diverging from those guarantees during the execution of the method. It just says that the state information of the object will always obey these rules: Feedback
Upon entry to the method.
Before leaving the method.
In addition, the invariant is a guarantee about the state of the object after construction. Feedback
According to the this description, an effective invariant would be defined as a method, probably named invariant( ), which would be invoked after construction, and at the beginning and end of each method. The method could be invoked as:
assert invariant();
This way, if you chose to disable assertions for performance reasons, there would be no overhead at all. Feedback
"
Best Regards,
Zoltán Mózes
EPAM Systems
Budapest office, Hungary (GMT+1)
Office phone: +36 (1) 327-74-XX
Office fax: +36 (1) 327-74-20
Mobile phone: +36 (20) XXX-XXX
E-mail: Zoltan_Mozes at epam.com
http://www.epam.com
CONFIDENTIALITY CAUTION AND DISCLAIMER
This message is intended only for the use of the individual(s) or entity(ies) to which it is addressed and contains information that is legally privileged and confidential. If you are not the intended recipient, or the person responsible for delivering the message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. All unintended recipients are obliged to delete this message and destroy any printed copies.
-----Original Message-----
From: javalist-bounces at javagrund.hu [mailto:javalist-bounces at javagrund.hu] On Behalf Of Jabba Laci
Sent: Thursday, December 14, 2006 9:52 PM
To: java
Subject: [Java lista] final formalis parameter
Sziasztok!
Ha a formalis parameterben final-t hasznalunk, akkor az optimalisabb
(gyorsabb) kodot fog eredmenyezni? Szamos esetben biztosan tudom, hogy
a formalis parameter erteke fix, nem valtozik a fuggvenyben. Ilyenkor
erdemes eletenni a "final"-t?
Egy eroltetett pelda:
public int mult(int x) {
return 2 * x;
}
vagy:
public int mult(final int x) {
return 2 * x;
}
Azon tul, hogy a 2. esetben x erteket nem lehet megvaltoztatni, mi még
a kulonbseg?
Koszonettel:
Lac
_______________________________________________
Javalist mailing list
Javalist at javagrund.hu
http://javagrund.hu/mailman/listinfo/javalist
További információk a(z) Javalist levelezőlistáról