BLOGTIMES
2013/12/30

いろいろな言語で i = i++; してみる

  programming 
このエントリーをはてなブックマークに追加

先日のなぜ i = i++; としてはいけないか続き。
意味はないと分かってはいるものの、ちょっと気になったので i = i++; について他の言語の挙動を調べてみることに。

今回調べたのは Java7, Perl 5.8, PHP5.2, Node.js v0.10.22 の4つ。
Ruby も調べたかったのですが、Rubyには ++ 演算子がない*1*2のをすっかり忘れていました。

ちなみにこの4つの言語では数値が増加する言語はありませんでした

Java7

ソースコード

public class Hoge { public static void main(String[] args) { int i = 0; i = i++; System.out.println(System.getProperty("java.version")); System.out.println(i); } }

実行結果

1.7.0_25 0

Perl 5.8

$ perl -v This is perl, v5.8.8 built for x86_64-linux-thread-multi Copyright 1987-2006, Larry Wall Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using "man perl" or "perldoc perl". If you have access to the Internet, point your browser at http://www.perl.org/, the Perl Home Page. $ perl -e '$i = 0 ; $i = $i++ ; print $i' 0

PHP 5.2

$ php -v PHP 5.2.17 (cli) (built: Feb 21 2011 15:53:52) Copyright (c) 1997-2010 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies with eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, by eAccelerator $ echo '<? $i = 0; $i = $i++ ; echo $i ;' | php 0

Node.js

$ node -v v0.10.22 $ node > i = 0 0 > i = i++ 0

トラックバックについて
Trackback URL:
お気軽にどうぞ。トラックバック前にポリシーをお読みください。[policy]
このエントリへのTrackbackにはこのURLが必要です→https://blog.cles.jp/item/6375
Trackbacks
このエントリにトラックバックはありません
Comments
愛のあるツッコミをお気軽にどうぞ。[policy]
古いエントリについてはコメント制御しているため、即時に反映されないことがあります。
コメントはありません
Comments Form

コメントは承認後の表示となります。
OpenIDでログインすると、即時に公開されます。

OpenID を使ってログインすることができます。

Identity URL: Yahoo! JAPAN IDでログイン