<html>
<head>
<meta content="text/html; charset=ISO-8859-2"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<blockquote type="cite">Csak amíg ott ez érthető és indokoltnak
tűnik, addig itt miért így kellett?</blockquote>
<br>
Azért, mert megtévesztően a trenary operátor valójában egy érték
típusú kifejezés (<i>conditional expression, </i>ahogy a neve is
mutatja), ezért ebből a szempontból pontosan ugyanúgy viselkedik,
mint egy sima összeadás.<br>
<br>
Ricsi<br>
<br>
<div class="moz-cite-prefix">On 2012.07.21. 18:25, Peter Verhas
wrote:<br>
</div>
<blockquote
cite="mid:680D67B5-E0FC-4546-A2E2-D776A7F5506F@verhas.com"
type="cite">Valóban azt írja a szabvány, hogy típuskonverziót fog
végezni, és az eredmény pontosan ugyanúgy Double lesz, mint ahogy
egy Long és egy Double összeadása során is Double-t kapunk. Csak
amíg ott ez érthető és indokoltnak tűnik, addig itt miért így
kellett? Nah, de mindegy. Lényeg, hogy ez nem bug, hanem feature.
<div><br>
<div>
<span class="Apple-style-span" style="border-collapse:
separate; color: rgb(0, 0, 0); font-family: Helvetica;
font-style: normal; font-variant: normal; font-weight:
normal; letter-spacing: normal; line-height: normal;
orphans: 2; text-align: auto; text-indent: 0px;
text-transform: none; white-space: normal; widows: 2;
word-spacing: 0px; -webkit-border-horizontal-spacing: 0px;
-webkit-border-vertical-spacing: 0px;
-webkit-text-decorations-in-effect: none;
-webkit-text-size-adjust: auto; -webkit-text-stroke-width:
0px; font-size: medium; "><span class="Apple-style-span"
style="border-collapse: separate; color: rgb(0, 0, 0);
font-family: Helvetica; font-style: normal; font-variant:
normal; font-weight: normal; letter-spacing: normal;
line-height: normal; orphans: 2; text-indent: 0px;
text-transform: none; white-space: normal; widows: 2;
word-spacing: 0px; -webkit-border-horizontal-spacing: 0px;
-webkit-border-vertical-spacing: 0px;
-webkit-text-decorations-in-effect: none;
-webkit-text-size-adjust: auto; -webkit-text-stroke-width:
0px; font-size: medium; ">
<div style="word-wrap: break-word; -webkit-nbsp-mode:
space; -webkit-line-break: after-white-space; "><span
class="Apple-style-span" style="border-collapse:
separate; color: rgb(0, 0, 0); font-family: Helvetica;
font-style: normal; font-variant: normal; font-weight:
normal; letter-spacing: normal; line-height: normal;
orphans: 2; text-indent: 0px; text-transform: none;
white-space: normal; widows: 2; word-spacing: 0px;
-webkit-border-horizontal-spacing: 0px;
-webkit-border-vertical-spacing: 0px;
-webkit-text-decorations-in-effect: none;
-webkit-text-size-adjust: auto;
-webkit-text-stroke-width: 0px; font-size: medium; ">
<div style="word-wrap: break-word; -webkit-nbsp-mode:
space; -webkit-line-break: after-white-space; ">
<div><span class="Apple-style-span" style="color:
rgb(136, 136, 136); ">--</span><span
class="Apple-style-span" style="color: rgb(136,
136, 136); "><br>
</span><span class="Apple-style-span"
style="color: rgb(136, 136, 136); ">Verhás Péter</span><span
class="Apple-style-span" style="color: rgb(136,
136, 136); "><br>
</span><span class="Apple-style-span"
style="color: rgb(136, 136, 136); "></span><span
class="Apple-style-span" style="color: rgb(136,
136, 136); "><a moz-do-not-send="true"
href="mailto:peter@verhas.com">peter@verhas.com</a></span><span
class="Apple-style-span" style="color: rgb(136,
136, 136); "><br>
</span><span class="Apple-style-span"
style="color: rgb(136, 136, 136); ">+36(30)9306805</span><span
class="Apple-style-span" style="color: rgb(136,
136, 136); "><br>
</span><span class="Apple-style-span"
style="color: rgb(136, 136, 136); ">skype:
verhas</span></div>
</div>
</span><br class="Apple-interchange-newline">
</div>
</span><br class="Apple-interchange-newline">
</span><br class="Apple-interchange-newline">
</div>
<br>
<div>
<div>On 2012.07.21., at 18:19, Böszörményi Péter wrote:</div>
<br class="Apple-interchange-newline">
<blockquote type="cite">
<div>Szerintem ez nem bug. Magyarazatom nincs, de talan ezek
adnak otletet:<br>
<a moz-do-not-send="true"
href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.25:">http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.25:</a><br>
The type of a conditional expression is determined as
follows:<br>
[...]<br>
* Otherwise, binary numeric promotion (§5.6.2) is applied
to the operand types, and the type of the conditional
expression is the promoted type of the second and third
operands.<br>
<br>
<a class="moz-txt-link-freetext" href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2">http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2</a>:<br>
When an operator applies binary numeric promotion to a
pair of operands, each of which must denote a value that
is convertible to a numeric type, the following rules
apply, in order:<br>
[...]<br>
* If either operand is of type double, the other is
converted to double.<br>
<br>
<br>
On Sat, 21 Jul 2012 17:56:41 +0200, Peter Verhas
<a class="moz-txt-link-rfc2396E" href="mailto:peter@verhas.com"><peter@verhas.com></a> wrote:<br>
<br>
<blockquote type="cite">A következő osztályt alkottam:<br>
</blockquote>
<blockquote type="cite"><br>
</blockquote>
<blockquote type="cite">public class Bug{<br>
</blockquote>
<blockquote type="cite"> public static Number q(Number
in) {<br>
</blockquote>
<blockquote type="cite"> return in instanceof Long
? 1L : 1.0;<br>
</blockquote>
<blockquote type="cite"> }<br>
</blockquote>
<blockquote type="cite">}<br>
</blockquote>
<blockquote type="cite"><br>
</blockquote>
<blockquote type="cite">Ebből a következős sikerült a
Java-nak fordítania:<br>
</blockquote>
<blockquote type="cite">verhasp:test verhasp$ javac
Bug.java<br>
</blockquote>
<blockquote type="cite">verhasp:test verhasp$ javap -v
Bug.class<br>
</blockquote>
<blockquote type="cite">Classfile
/Users/verhasp/projects/test/Bug.class<br>
</blockquote>
<blockquote type="cite"> Last modified 2012.07.21.; size
392 bytes<br>
</blockquote>
<blockquote type="cite"> MD5 checksum
7a7a2b3ddba08525477330df16f4ca78<br>
</blockquote>
<blockquote type="cite"> Compiled from "Bug.java"<br>
</blockquote>
<blockquote type="cite">public class Bug<br>
</blockquote>
<blockquote type="cite"> SourceFile: "Bug.java"<br>
</blockquote>
<blockquote type="cite"> minor version: 0<br>
</blockquote>
<blockquote type="cite"> major version: 51<br>
</blockquote>
<blockquote type="cite"> flags: ACC_PUBLIC, ACC_SUPER<br>
</blockquote>
<blockquote type="cite">Constant pool:<br>
</blockquote>
<blockquote type="cite"> #1 = Methodref #5.#15
// java/lang/Object."<init>":()V<br>
</blockquote>
<blockquote type="cite"> #2 = Class #16
// java/lang/Long<br>
</blockquote>
<blockquote type="cite"> #3 = Methodref #17.#18
//
java/lang/Double.valueOf:(D)Ljava/lang/Double;<br>
</blockquote>
<blockquote type="cite"> #4 = Class #19
// Bug<br>
</blockquote>
<blockquote type="cite"> #5 = Class #20
// java/lang/Object<br>
</blockquote>
<blockquote type="cite"> #6 = Utf8
<init><br>
</blockquote>
<blockquote type="cite"> #7 = Utf8 ()V<br>
</blockquote>
<blockquote type="cite"> #8 = Utf8 Code<br>
</blockquote>
<blockquote type="cite"> #9 = Utf8
LineNumberTable<br>
</blockquote>
<blockquote type="cite"> #10 = Utf8 q<br>
</blockquote>
<blockquote type="cite"> #11 = Utf8
(Ljava/lang/Number;)Ljava/lang/Number;<br>
</blockquote>
<blockquote type="cite"> #12 = Utf8
StackMapTable<br>
</blockquote>
<blockquote type="cite"> #13 = Utf8
SourceFile<br>
</blockquote>
<blockquote type="cite"> #14 = Utf8
Bug.java<br>
</blockquote>
<blockquote type="cite"> #15 = NameAndType #6:#7
// "<init>":()V<br>
</blockquote>
<blockquote type="cite"> #16 = Utf8
java/lang/Long<br>
</blockquote>
<blockquote type="cite"> #17 = Class #21
// java/lang/Double<br>
</blockquote>
<blockquote type="cite"> #18 = NameAndType #22:#23
// valueOf:(D)Ljava/lang/Double;<br>
</blockquote>
<blockquote type="cite"> #19 = Utf8 Bug<br>
</blockquote>
<blockquote type="cite"> #20 = Utf8
java/lang/Object<br>
</blockquote>
<blockquote type="cite"> #21 = Utf8
java/lang/Double<br>
</blockquote>
<blockquote type="cite"> #22 = Utf8 valueOf<br>
</blockquote>
<blockquote type="cite"> #23 = Utf8
(D)Ljava/lang/Double;<br>
</blockquote>
<blockquote type="cite">{<br>
</blockquote>
<blockquote type="cite"> public Bug();<br>
</blockquote>
<blockquote type="cite"> flags: ACC_PUBLIC<br>
</blockquote>
<blockquote type="cite"> Code:<br>
</blockquote>
<blockquote type="cite"> stack=1, locals=1,
args_size=1<br>
</blockquote>
<blockquote type="cite"> 0: aload_0<br>
</blockquote>
<blockquote type="cite"> 1: invokespecial #1
// Method
java/lang/Object."<init>":()V<br>
</blockquote>
<blockquote type="cite"> 4: return<br>
</blockquote>
<blockquote type="cite"> LineNumberTable:<br>
</blockquote>
<blockquote type="cite"> line 1: 0<br>
</blockquote>
<blockquote type="cite"><br>
</blockquote>
<blockquote type="cite"> public static java.lang.Number
q(java.lang.Number);<br>
</blockquote>
<blockquote type="cite"> flags: ACC_PUBLIC, ACC_STATIC<br>
</blockquote>
<blockquote type="cite"> Code:<br>
</blockquote>
<blockquote type="cite"> stack=2, locals=1,
args_size=1<br>
</blockquote>
<blockquote type="cite"> 0: aload_0<br>
</blockquote>
<blockquote type="cite"> 1: instanceof #2
// class java/lang/Long<br>
</blockquote>
<blockquote type="cite"> 4: ifeq 11<br>
</blockquote>
<blockquote type="cite"> 7: dconst_1<br>
</blockquote>
<blockquote type="cite"> 8: goto 12<br>
</blockquote>
<blockquote type="cite"> 11: dconst_1<br>
</blockquote>
<blockquote type="cite"> 12: invokestatic #3
// Method
java/lang/Double.valueOf:(D)Ljava/lang/Double;<br>
</blockquote>
<blockquote type="cite"> 15: areturn<br>
</blockquote>
<blockquote type="cite"> LineNumberTable:<br>
</blockquote>
<blockquote type="cite"> line 3: 0<br>
</blockquote>
<blockquote type="cite"> StackMapTable:
number_of_entries = 2<br>
</blockquote>
<blockquote type="cite"> frame_type = 11 /* same
*/<br>
</blockquote>
<blockquote type="cite"> frame_type = 64 /*
same_locals_1_stack_item */<br>
</blockquote>
<blockquote type="cite"> stack = [ double ]<br>
</blockquote>
<blockquote type="cite"><br>
</blockquote>
<blockquote type="cite">}<br>
</blockquote>
<blockquote type="cite"><br>
</blockquote>
<blockquote type="cite"><br>
</blockquote>
<blockquote type="cite">A biztonság kedvéért command
line-ból fordítottam ezzel:<br>
</blockquote>
<blockquote type="cite"><br>
</blockquote>
<blockquote type="cite">verhasp:test verhasp$ java
-version<br>
</blockquote>
<blockquote type="cite">java version "1.7.0_04"<br>
</blockquote>
<blockquote type="cite">Java(TM) SE Runtime Environment
(build 1.7.0_04-b21)<br>
</blockquote>
<blockquote type="cite">Java HotSpot(TM) 64-Bit Server VM
(build 23.0-b21, mixed mode)<br>
</blockquote>
<blockquote type="cite"><br>
</blockquote>
<blockquote type="cite">Ha értő szem megnézi a kódot,
akkor láthatja (no meg tapasztalhatja is futtatás
közben), hogy ez bizony mindig Double értéket fog
visszaadni, akkor is, ha paraméter 'in' Long. Ha
megfordítom a feltételt, és persze a feltételes
kifejezés két felét is ezzel, illetve ha nem azt
vizsgálom, hogy Long-e, hanem azt, hogy Double, akkor is
mindig Double értéket akar visszaadni a javap
disassembly alapján. Megpróbáltam 1.6.0_33 verziójú
Java-val is (nem az összes verziót, annyi türelmem
nincs), ott is ugyanaz az eredmény.<br>
</blockquote>
<blockquote type="cite"><br>
</blockquote>
<blockquote type="cite">Azért mielőtt bugreportot küldenék
az ORACLE-nak, megkérdezném a tisztelt társaságot, hogy
mit nézek itt nagyon be?<br>
</blockquote>
<blockquote type="cite"><br>
</blockquote>
<blockquote type="cite">A workaround megvan, mert a<br>
</blockquote>
<blockquote type="cite"><br>
</blockquote>
<blockquote type="cite">public class Bug{<br>
</blockquote>
<blockquote type="cite"> public static Number q(Number
in) {<br>
</blockquote>
<blockquote type="cite"> if( in instanceof Long )<br>
</blockquote>
<blockquote type="cite"> return 1L; else return
1.0;<br>
</blockquote>
<blockquote type="cite"> }<br>
</blockquote>
<blockquote type="cite">}<br>
</blockquote>
<blockquote type="cite"><br>
</blockquote>
<blockquote type="cite">működik rendesen.<br>
</blockquote>
<blockquote type="cite"><br>
</blockquote>
<blockquote type="cite">--<br>
</blockquote>
<blockquote type="cite">Verhás Péter<br>
</blockquote>
<blockquote type="cite"><a class="moz-txt-link-abbreviated" href="mailto:peter@verhas.com">peter@verhas.com</a><br>
</blockquote>
<blockquote type="cite">+36(30)9306805<br>
</blockquote>
<blockquote type="cite">skype: verhas<br>
</blockquote>
<blockquote type="cite"><br>
</blockquote>
<blockquote type="cite"><br>
</blockquote>
<blockquote type="cite"><br>
</blockquote>
<blockquote type="cite"><br>
</blockquote>
<br>
<br>
-- <br>
Üdvözlettel,<br>
Böszörményi Péter<br>
_______________________________________________<br>
Javalist mailing list<br>
<a class="moz-txt-link-abbreviated" href="mailto:Javalist@lists.javaforum.hu">Javalist@lists.javaforum.hu</a><br>
<a class="moz-txt-link-freetext" href="http://lists.javaforum.hu/mailman/listinfo/javalist">http://lists.javaforum.hu/mailman/listinfo/javalist</a><br>
</div>
</blockquote>
</div>
<br>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
Javalist mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Javalist@lists.javaforum.hu">Javalist@lists.javaforum.hu</a>
<a class="moz-txt-link-freetext" href="http://lists.javaforum.hu/mailman/listinfo/javalist">http://lists.javaforum.hu/mailman/listinfo/javalist</a>
</pre>
</blockquote>
<br>
</body>
</html>