[Java lista] ket alap kerdes

Zoltan Mozes Zoltan_Mozes at epam.com
2007. Feb. 13., K, 12:39:56 CET


1) double i = 2.65d
2) 1.4 és 5.0 (1.5 másnéven) JAVA-ban a sima String-ekre egy úgynevezett string pool lett létrehozva. Magyarul

String a = "aa";
létrehoz a JVMben szereplő string pool-ban egy string ojjektumot és az "a" referenciádat erre az obejktumra állítja. Namost ezután ha mész tovább így:

String b = "aa";

akkor mivel már szerepel egy string a pool-ban azzal a tartalommal hoyg "aa" ezért a "b" referenciádat is ugyanarra az ojjektumra fogja ráállítani.

Így tehát az a == b true-t ad vissza mivel télleg konkrétan ugyanarra a (JVM string poolban lévő ) string ojjektumra fog mutatni a két referenciád.


" It is important to be aware of what happens when you use a string literal ("immutable" in
both examples). Every string literal is represented internally by an instance of String. Java
classes may have a pool of such strings. When a literal is compiled, the compiler adds an appropriate
string to the pool. However, if the same literal already appeared as a literal elsewhere in
the class, then it is already represented in the pool. The compiler does not create a new copy.
Instead, it uses the existing one from the pool. This process saves on memory and can do no
harm. Because strings are immutable,"

Complete Java(r) 2
Certification: Study Guide,
Fifth Edition
Philip Heller
Simon Roberts
SYBEX(r)


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 Fisha
Sent: Tuesday, February 13, 2007 12:31 PM
To: javalist at javagrund.hu
Subject: [Java lista] ket alap kerdes

Sziasztok!

Ket alap nyelvi kerdesem lenne.

1. Szamabrezolosdi:

a kod:
double i = 2.65f;
System.out.println(i);

az eredmeny:
2.6500000953674316

Gondolom, hogy a binaris abrazolas pontatlansagabol ered a problema.
De orulnek, ha valaki pontosan le tudna irni, hogy mi is tortenik a
hatterben valojaban.

2. Optimalizalosdi:

a kod:
String a = "aa";
String b = "aa";
System.out.println(a == b);

az eredmeny: true

Gondolom, hogy valami optimalizacios folyamat van a hatterben.
Tapasztalataim szerint a 1.3-as javaban meg biztosan nem volt ilyen.
Mit lehet errol tudni? Miota van? Vagy nem is verzio, hanem jvm fuggo?

Elore is koszi,
Fisha                            mailto:fisha at freemail.hu


_______________________________________________
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