Javaプログラムの出力結果についての疑問

このQ&Aのポイント
  • 出力結果が得られる理由が分かりません。二つのプログラムの実行フローを解説してください。
  • 一つ目のプログラムでは、配列の参照を変更しても元の配列の要素が変化していることが分かります。
  • 二つ目のプログラムでは、オブジェクトの参照を変更しても元のオブジェクトと同じ値を持つ別のオブジェクトが作成されることが分かります。
回答を見る
  • ベストアンサー

出力結果が得られる理由が分かりません

一つ目のプログラムソース: public class Array{ public static void main(String[] args){ int[] a={1,2,3,4}; int[] b={5,6,7,8}; System.out.print(a[2]); System.out.print(b[2]); b=a; System.out.print(a[2]); System.out.print(b[2]); b[2]=0; System.out.print(a[2]); System.out.print(b[2]); } } 出力結果:  373300 二つ目のプログラムソース: class Box{ private int value; public Box(){value=0;} public Box(int v){setValue(v);} public void setValue(int v){value=v;} public int getValue(){return value;} public Box copy(){ Box b=new Box(); b.setValue(getValue()); return b; } } public class BoxTest{ public static void main(String[] args){ Box x=new Box(1); Box y=new Box(2); Box z=new Box(3); x=z.copy(); y=z; System.out.print(x.getValue()); System.out.print(y.getValue()); System.out.println(z.getValue()); System.out.println(x==y); System.out.println(y==z); System.out.println(z==x); } } 出力結果:  333 false true false この二つのプログラムがどうしてこのような出力結果になるのかが分かりません。 良かったら教えてください。

  • Java
  • 回答数2
  • ありがとう数1

質問者が選んだベストアンサー

  • ベストアンサー
  • uta3
  • ベストアンサー率70% (21/30)
回答No.2

最初の37は問題ないですよね。 次のb=aが分からなくなっている原因かな。 b=aを実行すると、bはaと同じ配列になります。 これは値がコピーされるわけではなく、同じ領域を共有するという意味です。 a,bは配列の先頭を指すポインタだと思ってください。 ですからb=a以降のa[2]とb[2]は名前こそ違うものの、まったく同じものです。

hajimen
質問者

お礼

ありがとうございます! よくわかりました。 同じ領域を共有すのですね!!そこがわかりませんでした。 ありがとうございました!!

その他の回答 (1)

noname#118337
noname#118337
回答No.1

どの辺が分かりませんか? それを書いてくれないと、説明も難しいです

hajimen
質問者

補足

はい。二つ目のプログラムは自力で解けましたので、もう大丈夫です。 一つ目のプログラムについてですが、 このプログラムを配列だと思って考えてたのですが、それが違かったので、どのような手順(処理)で373300になるかが分かりません。

関連するQ&A

  • コンストラクタについて

    これも試験問題らしくて自分でやってみましたのであってるかどうか自信なくて どなたかみてみていただけたらと思います.よろしくお願いします。 下のソースファイルをコンストラクタを用いたものに修正しなさい class Sconst{ int x,y,z; void print(){ System.out.println(x); System.out.println(y); System.out.println(z); } } class ExConstTest{ public static void main (String[] args ){ Sconst sc=new Sconst(); sc.x=10; sc.y=30; sc.z=5; } } ------------------------------------------------------------- class Sconst{ int x,y,z; Sconst(){ x=10; y=30; z=5; } } class ExConstTest{ public static void main (String[] args ){ Sconst a1; Sconst a2; Sconst a3; a1=new Sconst(); a2=new Sconst(); a3=new Sconst(); System.out.println(a1.x); System.out.println(a2.x); System.out.println(a3.x); } }

    • ベストアンサー
    • Java
  • オーバーロードで

    オーバーロードでメソッドgetvalueへコマンドライン引数から取得した値を渡したいのですが、どのようにすればいいのでしょうか。エラー:シンボルが見つけられません。 ×としたところでol.getvalue( args[i] );←ここに値をいれたいのですが、できません。なにか方法はありますでしょうか。 class OverLoad{     void getvalue(Boolean value){   System.out.println("Boolean型:" + value);     }     void getvalue(int value){   System.out.println("int型:" + value);     } public class Capsule{     public static void main(String[] args){   OverLoad ol = new OverLoad(); ×   ol.getvalue( args[i] ); ○   ol.getvalue(false); ○     ol.getvalue(80);     } }

  • java

    次のコード中の括弧内で下に示す11通りの各コードを実行した場合の実行画面を正確に答えよ。 class A{ public void func1(){System.out.println("A1");} public void func2(){System.out.println("A2");} } class B extends A{ public void func1(){System.out.println("B");} } class C{ public int x=0, y=1; } class D extends C{ public int x=2; public void func1(int x){System.out.println(x);} public void func2(int x){System.out.println(this.x);} public void func3(int x){System.out.println(super.x);} public void func4(int x){System.out.println(this.y);} public void func5(int x){System.out.println(super.y);} } class E{ public void func1(int n){ try{ System.out.println("E1"); int[] ary=new int[n]; System.out.println("E2"); }catch(NegativeArraySizeException e){ System.out.println("E3"); }finally{ System.out.println("E4"); } } } (1) A a=new A(); a.func1(); (2) A a=new B(); a.func1(); (3) B b=new B(); b.func1(); (4) B b=new B(); b.func2(); (5) D d=new D(); d.func1(3); (6) D d=new D(); d.func2(3); (7) D d=new D(); d.func3(3); (8) D d=new D(); d.func4(3); (9) D d=new D(); d.func5(3); (10) E e=new E(); e.func1(5); (11) E e=new E(); e.func1(-2); (1) A1 (2)B (3)B (4) A2 (5) 3 (6) 2 (7) 0 (8) 1 (9) 1 (10) E1E2E4(11) E1E3E4 と答えになるんですがなぜこうなるのかわかりません。教えてください

    • ベストアンサー
    • Java
  • このプログラムのどこがいけないかの指摘をお願いします!

    import java.io.*; public class ex32h_1{ public static void main(String args[]) throws Exception{ BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.print("x="); int x=(new Integer(in.readLine())).intValue(); System.out.print("y="); int y=(new Integer(in.readLine())).intValue(); System.out.print("z="); int z=(new Integer(in.readLine())).intValue(); boolean result= (x*y*z==0) && ((x^2+y^2)!=0) && ((y^2+z^2)!=0) && ((x^2+z^2)!=0); System.out.println("0は一つ?;"+result); } } x,y,zに0が一つだけあるかどうかを調べるというものです。x=0,y=1,z=1(0,1,1)の組み合わせのときはなぜかfalseになってしまいます。他のとき(0,1,2)のときなどはきちんとtrueになります。 どこがいけないかわかるかたご指摘の程をお願いします。 (幅の都合上左よりですが実際のプログラムをきちんとスペースが入っています。)

    • ベストアンサー
    • Java
  • エラーの訂正でアドバイスください。

    javaプログラミング超初心者です import java.io.*; public class ex22b { public static void main(String[] arg) { System.out.print("x: "); int x = (new Integer (in.readLine())). intValue(); System.out.print("y: "); int y = (new Integer (in.readLine())). intValue(); int a; while (y > 0) { a = a + x; System.out.println("kekka = " + a); System.out.println("y = " + y); y = y - 1; } System.out.println("乗算結果は " + a); } } というソースを書いたら、コンパイルの際に シンボルを見つけられません シンボル:変数 in 場所  :ex22bのクラス int x = (new Integer (in.readLine())). intValue();            ^ int y = (new Integer (in.readLine())). intValue();            ^ というエラーが出ました。 どこをどう直したらいいのでしょうか。

    • ベストアンサー
    • Java
  • javaで課題を出されています

    題名の通りなのですが、javaのプログラミングで課題を出されていて うまく書けません お題としては 1 if分と論理演算を使うこと。 2 forまたはwhileを使用すること 3 メソッドを使用すること。(main以外で) 4 配列を使用すること 以上なのですが、一応昔スロットプログラムを作っていたのでこれをベースに作ろうかなと考えています import java.io.*; public class Slot01{ public static void main(String[] args){ try{ //String line =reader.readLine(); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("こんにちは!スロットゲームへようこそ!"); System.out.println("EnterKeyを押して当たりを出してくださいね!"); while(true){ //while文は処理を繰り返すためのものです。 // ▽これよりソースを打ち込んでください //try{ int x=(int)(Math.random()*9)+1; int y=(int)(Math.random()*9)+1; int z=(int)(Math.random()*9)+1; int[]kakuritu; int sum; System.out.print(x); System.out.print(y); System.out.print(z); System.out.println(""); String line =reader.readLine(); //System.out.println(""); if(x==7&&y==7&&z==7){ System.out.println("スーパー大当たり"); }else if(x==y&&y==z){ System.out.println("大当たり"); }else if(x==y||y==z||x==z){ System.out.println("小当たり"); }else{ System.out.println("外れ"); } }//while文の終わり }catch (IOException e){ System.out.println(e); }catch (NumberFormatException e){ System.out.println("正しい形式で表示してください。"); } } } このプログラムなのですが、1と2はこの中に入ってます。考えてみたのですが、このプログラムに当たり確率を表示させるプログラムなら3と4も満たせそうなのですが、いまいちソースが思いつきませんのでご教授お願いします。 文章おかしいところだらけで申し訳ありません。

    • ベストアンサー
    • Java
  • コンパイルはできるのに・・・・

    import java.io.*; public class ex32 { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int x = (new Integer(in.readLine())).intValue(); int y = (new Integer(in.readLine())).intValue(); while(x != y) { System.out.print("int x> "); System.out.print("int y> "); if(x > y) { x = x - y; } else{ y = y - x; } } System.out.println("GCM = " + x); } } このソースで、コンパイルとdouble x >の出力まではできるのですが、 そこで数字を入力してエンターを押すと、そこから進みません。 どう直したらいいでしょうか。

    • ベストアンサー
    • Java
  • この2つのプログラムは全く同じものでしょうか?

    class Base{ protected int x; Base() { this.x=0;} Base(int x) {this.x=x;} void print(){ System.out.println("Base.x="+x);} } class Derived extends Base{ int x; Derived(int x1,int x2){ super(x1); this.x=x2;} void print(){ super.print(); System.out.println("Derived.x="+x);} } public class SuperTester{ public static void main(String[] args){ Base a =new Base(10); System.out.println("----a----"); a.print(); Derived b=new Derived(20,30); System.out.println("----b----"); b.print(); } } このプログラムと、 このプログラムのDerivedクラスのコンストラクタの部分を Derived(int x1,int x2){ super.x=x1; this.x=x2;} に変えたプログラムです。 かなりややこしいのですが、この2つのプログラムはソースは一部違いますが、 意味するものは全く同じでしょうか? ちなみにこの2つは実行結果は同じになります。 【実行結果】 ----a---- Base.x=10 ----b---- Base.x=20 Derived.x=30 つまり、Derivedクラスでint x;を書かず、 よろしくお願いします。

    • ベストアンサー
    • Java
  • NoSuchMethodErrorが解決できません。

    実行時エラーNoSuchMethodErrorが出て困っています。 どこを修正すればいいのでしょうか? class A implements Runnable{ int x; int y; public void run(){ for(int i = 0;i < 100;i++){ x++; y++; System.out.println("x="+x+"y="+y); } } } class B{ public static void main(String args[]){ new Thread(new A()).start(); new Thread(new A()).start(); } }

    • ベストアンサー
    • Java
  • synchronizedによる同期化について

    Javaで開発しています。 synchronizedで同期化したく、サンプルを作ってみたのですが上手く同期化が出来ていないようなので質問しました。 以下プログラム public class Synch{ public static void main(String[] args){ final Something obj = new Something(); new Thread(){ public void run(){ synchronized(this){ try{ obj.write(); } catch(Exception e){ }notify();} } }.start(); new Thread(){ public void run(){ try{ obj.read(); } catch(Exception e){ } } }.start(); } } class Something{ private int x = 10; private int y = 100; public synchronized void write(){ if(x < y){ System.out.println("write:x < y"); } else if(x > y){ System.out.println("write:x > y"); } for(int n = 0;n < 100;n++){ x++; y++; } for(int m = 0;m < 150;m++){ y--; } if(x < y){ System.out.println("write:x < y"); } else if(x > y){ System.out.println("write:x > y"); } } public synchronized void read(){ if(x < y){ System.out.println("read:x < y"); } else if(x > y){ System.out.println("read:x > y"); } } } このプログラムを実行すると、時々readのほうが先に表示されてしまいます。 実行環境はEclipse2.1.3です。 readが先に表示されるのは仕方のないことなのでしょうか? それともプログラムがいけないのでしょうか? ご存知の方いらっしゃいましたら教えて頂けないでしょうか。 不足がありましたら仰ってください。

    • ベストアンサー
    • Java