忍者ブログ
メモ、備忘録、その他雑記を記載します。 ただし、このHPに記載している情報を利用した結果 損失・損害等が発生したとしても筆者は責任を持ちません。
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

Stringはint型等と同じ扱いで、値渡しはできるが参照渡しできない。
参照渡ししたい場合はStringBufferを使用する。


import java.io.*;

/**
 * メインクラス
 * @author
 * @version 1.0
 */
public class Main
{
    /**
     * アプリケーション起動処理
     * @param args    コマンドライン引数
     * @return    なし
     */
    public static void main( String[] args )
    {
        System.out.println("Hello");
       
        String str = "123";
        System.out.println( str );
        Func1( str );
        System.out.println( str );
        System.out.println();

        StringBuffer sb = new StringBuffer( "123" );
        System.out.println( sb );
        Func2( sb );
        System.out.println( sb );
        System.out.println();
    }

    public static void Func1( String str )
    {
        str = "456";
        // これは値渡しでコピーしたstrを変更しているだけ
        // ×
    }

    public static void Func2( StringBuffer sb )
    {
        sb.append( "456" );
        // 参照渡ししたオブジェクトに対して文字列操作
        // ○
    }
}


拍手[0回]

PR
ファイルをバイナリ形式で読み込む処理の雛形

import java.io.*;

        System.out.println("Hello");
        try
        {
            int n;
            FileInputStream is = new FileInputStream("test.txt");

            while ( true )
            {
                n = is.read();
                if ( n == -1 )
                {
                    break;
                }
                System.out.print(Integer.toHexString( n ) + " ");
            }
            is.close();
        }
        catch ( IOException e )
        {
            System.out.println( e );
        }

拍手[0回]

一番単純なjavaプログラムです。

/**
 * メインクラス
 * @author
 * @version 1.0
 */
public class Main
{
    /**
     * アプリケーション起動処理
     * @param args    コマンドライン引数
     * @return    なし
     */
    public static void main( String[] args )
    {
        System.out.println("Hello");
    }
}

拍手[0回]

1. JDKのダウンロード、インストール
インストール後、javac.exeへパスを通す。

拍手[0回]

テキストファイルを1行毎に読み込む処理の雛形

    /**
     * ファイル読込処理
     * @param strFile    ファイル名
     * @return    なし
     */
    public static void ReadFile( String strFile )
    {
        String strLine;

        try
        {
            // ファイル存在確認
            File file = new File( strFile );
            if( file.exists() == false )
            {
                return;
            }

            // ファイルオープン
            FileReader        fr      = new FileReader( strFile );
            BufferedReader    br      = new BufferedReader( fr );

            // ファイル1行読込ループ
            for ( ;; )
            {
                // BufferedReaderで1行読込
                strLine = br.readLine();

                // 終了判定
                if ( strLine == null )
                {
                    break;
                }

                // 任意の処理

            }

            // 終了化処理
            if ( br != null )
            {
                br.close();
                br = null;
            }
            if ( fr != null )
            {
                fr.close();
                fr = null;
            }
        }
        catch ( Exception ex )
        {
            ex.printStackTrace();
            System.err.println( ex );
        }

        return;
    }


拍手[0回]

 HOME | 1  2  3 
Admin / Write
カレンダー
06 2025/07 08
S M T W T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
フリーエリア
最新コメント
最新トラックバック
プロフィール
HN:
Ace
性別:
非公開
バーコード
ブログ内検索
P R
忍者アナライズ
忍者ブログ [PR]