Java Basic


概要 (Java 2 プラットフォーム SE v1.4.0)

宣言 String str = "hello";  
  String str = new String("hello");  
配列定義 float fltAbc[] = {0,0,0};  
  float ltAbc[] = new float[3];
String str[] = new String[3];
 
  String str[] = abc();
for (int i = 0 ; i < str.length ; i++) {
System.out.println("str=" + str[i] + " i=" + i);
}
public static String[] abc() {
String strabc[] = {"a","b","c"};
return strabc;
}
 
  String p_info [] = {"エラー(あ="+p_xxxx1+" い="+p_xxxx2+")"};
p_info [0] = "エラー(あ="+p_xxxx1+" い="+p_xxxx2+")";   ???

String p_error []
 = {"あ","い","う"};
error(p_error[0]);
error(p_error[1]);
error(p_error[2]);
 
  String str[];
str = new String[3];
 
  int ix1 = 0;
String strAbc[] = {"a","b","c"};
     ix1 = 1;
     System.out.println("strAbc 1:" + strAbc[ix1]);         //strAbc 1:b
     System.out.println("strAbc 2:" + strAbc[ix1+1]);     //strAbc 1:c
     System.out.println("strAbc 3:" + strAbc[2-1]);       //strAbc 1:b
 
キャスト intWork = (int)100 ;  
型変換

 

 

 

 

 

 

 

 

 

float flt1 = Float.parseFloat(str1.toString()); String -> float
int int1 = Integer.parseInt(str1.toString()); String -> int
pstmt.setBigDecimal(1,new BigDecimal("1")); String -> BigDecimal
String str1 = String.valueOf(int1); int -> String
String str1 = String.valueOf(flt1); float -> String
t_Upd.setsnd(snd.toString()); BigDecimal -> String
t_Upd.setsnd(String.valueOf(snd)); BigDecimal -> String
flosnd = snd.floatValue(); BigDecimal -> float
int in1 = integer1.intValue(); Integer -> int
float snd = (float)intcnt; int -> float
BigDecimal add(BigDecimal val) 値が (this + val)
subtract(BigDecimal val) 値が (this - val)
multiply(BigDecimal val) 値が (this * val)
divide(BigDecimal val,int scale,int roundingMode) 値が (this / val)
ROUND_UP, ROUND_DOWN, ROUND_CEILING, ROUND_FLOOR, ROUND_HALF_UP,
ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_UNNECESSARY
ynd = ynd.add(t_Sel.getZND(0));
ynd = ynd.subtract(snd);
bihm = bighm.divide(bignin,2,BigDecimal.ROUND_HALF_UP)
if (nhm.add(nhm).compareTo(new BigDecimal("7.0")) >= 0) {
hnd = hnd.add(new BigDecimal("1.0"));
}
if (hm.compareTo(new BigDecimal("0.0")) == 0) {
}
BigDecimal en = new BigDecimal(0);
BigDecimal zan = new BigDecimal(0);
zan = zan.add(en);
DecimalFormat formatter0 = new DecimalFormat ("0.00");
BigDecimal biga = new BigDecimal(11); 
BigDecimal bigb = new BigDecimal(1.00); 
biga = biga.subtract(bigb);
System.out.println("="+formatter0.format(biga)); //=10.00
biga = new BigDecimal(0); 
bigb = new BigDecimal(0.0); 
if (biga.compareTo(bigb) == 0) {
System.out.println("=="); // ==
} else {
System.out.println("<>"); 
}

0.1を100回加算誤差
BigDecimal 10.0000000000000005551115123125782702118158340454101562500
float 10.000002
double 9.99999999999998

0.1を1000回加算誤差
BigDecimal 100.0000000000000055511151231257827021181583404541015625000
float 99.99905
double 99.9999999999986
 
比較 if (str1.toString().trim().equals("")) {}  
  if (Integer1.equals(Integer2)) {}  
  if ( "2".equals(str1) ) {  
  if (str1 == null) {}  
  if (str1 != null) {}  
  if (!chk("a")) {} boolean chk(String kbn)
  if (str1.compareTo(str2) < 0) {} ← 推奨
if (str1.compareTo(str2) = 0) {}
if (str1.compareTo(str2) > 0) {}
str1<str2 → 0 より小さい
str1=str2 → 0
str1>str2 → 0より大きい
  if (kbn.equals("1")) {
}
else if (kbn.equals("2")) {
}
else if (kbn.equals("3")) {
}
 
分岐 switch(border) {
   case 1:
      style.setBorderRight(HSSFCellStyle.BORDER_DASH_DOT);
      style.setRightBorderColor(HSSFCellStyle.BLACK);
      break ;
   default:
 }
 
     
長さ if (str1.toString().length() == 1) {}  
String aaa = "漢字123456";
System.out.println("a:" + aaa.getBytes().length);
System.out.println("b:" + aaa.length());
 
a:10
b:8
文字操作 if (str1.substring(0,1).toString().equals("1")) {} 先頭から1桁
  "hamburger".substring(4, 8) では "urge" が返される  
  StringTokenizer st = new StringTokenizer("this is a test");
while (st.hasMoreTokens()) {
 println(st.nextToken());
}
 
  "smiles".substring(1, 5) では "mile" が返される  
  String str = "1,Java00";
byte [] bytes = str.getBytes();
System.out.println("1:" + new String(bytes,0,1));     //1:1
System.out.println("2:" + new String(bytes,1,1));     //2:,
System.out.println("3:" + new String(bytes,2,8));     //3:Java
System.out.println("4:" + new String(bytes,10,2));   //4:00
 
  row.createCell((short)8).setCellValue(symd.substring(0,4)); //日付
row.createCell((short)9).setCellValue(symd.substring(4,6)); //日付
row.createCell((short)10).setCellValue(symd.substring(6,8)); //日付
 
連結 concat(String str) 指定された文字列をこの文字列の最後に連結します。
return ans.concat("11111");
 
改行 strText = strText + "\n";  
パス c:\\test\\test.txt  
置換 String strman1 = "U" + aline.substring(IXS[5],IXE[5]).replace(' ','0');
a.replace(' ',' ')
 
フォーマット SimpleDateFormat formatter = 
        new SimpleDateFormat ("yyyyMMddHHmmss");
 
  DecimalFormat formatter0 = new DecimalFormat ("0000000");  
     使用例 java.util.Date currenttime = new java.util.Date();
String stdate = formatter.format(currenttime).substring(0,8);    //日
String sttime = formatter.format(currenttime).substring(8,14);   //時間
formatter0.format(inputcount)
 
TimeStamp java.util.Date currenttime = new java.util.Date();
java.sql.Timestamp timestampvalue = new java.sql.Timestamp(currenttime.getTime());
pstmt.setTimestamp(4,timestampvalue);
 
結合 "aaa" + "bbb"  
  StringBuffer textfile = new StringBuffer("");
textfile.append("aaa");
textfile.append("bbb");
 
ループ for (int i = 0 ; i < 2 ; i++) {
}
2回
break ラベル;
continue ラベル;
  String wbk = strtbl[0];
while (strtbl[cnt].equals(wbk)) {  //偽の時抜ける
}
String str[] = new String[10];
str[0] = "0";
str[1] = "1";
str[2] = "2";
int i = 0 ;
while (str[i]!=null) {
i = i + 1;
System.out.println("i-cnt="+i);
}
System.out.println("i-end="+i);
//i-cnt=1
//i-cnt=2
//i-cnt=3
//i-end=3
 
  int cnt = 0;
String strtbl[] = {"111","111","222","333","333","333","444"};
String wbk = strtbl[0];
for (int int1 = 0 ; int1 < 7 ; int1++) {
     if (int1 == 0) {
          wbk = strtbl[0];
     }
     System.out.println("wbk="+wbk);
     if (strtbl[int1].equals(wbk)) {
          cnt = cnt  + 1 ;
     } else {
          wbk = strtbl[int1];
     }
}
System.out.println("wbk="+wbk);
System.out.println("cnt="+cnt);
 
条件 && -> and , || -> or  
引数 if (args[0].endsWith("xml")) {
    System.out.println("*** test1 ***");
} else {
    System.out.println("*** test2 ***");
}
 
public static void main(String[] args) {
System.out.println("args0:" + args[0]);
System.out.println("args1:" + args[1]);
}
C:\j2sdk1.4.0_01\bin\java test "AAA" "BBB"
 
文字コード JISAutoDetect Shift-JIS、EUC-JP、ISO 2022 JPの検出および変換(Unicode変換のみ)
SJIS Shift-JIS、日本語
MS932 Windows日本語
Cp943C IBM OS/2日本語拡張
UTF8 8ビットUnicode変換形式
 
//rs_z010.getString("ADDRESS1") -> 大浜西町2−2
String text5 = new String(rs_z010.getString("ADDRESS1").getBytes("SJIS"));
System.out.println("text5=" + text5); //text5=大浜西町2−2

System.out.println( new String(rs_z010.getString("NM").getBytes("SJIS")));
System.out.println( new String(rs_z010.getString("NM").getBytes("Cp943C")));
 
//出力ファイル
targetfile = new File(backfile);
pwout = new PrintWriter(
     new BufferedWriter(
          new OutputStreamWriter(
               new FileOutputStream(targetfile.getPath(),true),"Cp943C")
     ),false
    );
 
     
禁断? System.exit(0);  
Exception Exception appErr = new Exception();
throw appErr;
} catch(Exception e) {
cat.fatal(e);
} finally {
}
catch (Throwable e)
heep java.lang.Runtime.getRuntime().totalMemory();
java.lang.Runtime.getRuntime().freeMemory();
long tota = Runtime.getRuntime().totalMemory();
long free = Runtime.getRuntime().freeMemory();
System.out.println("heapsize," total_First - free);
class -mx xxxxxxx
excel起動 String os = System.getProperty( "os.name" );
Runtime runtime = Runtime.getRuntime();
System.out.println("os:" + os);
if( os.equals( "Windows 98" ) || os.equals( "Windows Me" ) ) {
    Process proc = runtime.exec( "start test.xls /e");
    proc.waitFor();
} else {
    Process proc = runtime.exec( "cmd /C start /MIN test.xls /e" );
    proc.waitFor();
}




←確認済


←確認済 Windows2000

システム strTIME = String.valueOf( System.currentTimeMillis()); 時間
gc java.lang.System.gc();  
" \"->"  
sleep public static void main(String[] args) {
SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss");
java.util.Date currenttime = new java.util.Date();

currenttime = new java.util.Date();
String sttime = formatter.format(currenttime).substring(8,14);

System.out.println("sttime1 = "+sttime);

sleep(1000); //1秒お待ち

currenttime = new java.util.Date();
sttime = formatter.format(currenttime).substring(8,14);
System.out.println("sttime2 = "+sttime);

}

static void sleep(int time) {
try 
{
Thread.sleep(time);
}
catch (InterruptedException e)
{
}

}
 
try catch try {
//} catch (Throwable theException) {
} catch (Throwable e) {
  System.out.println("abend=" + e);     
} finally {
xxx.closeResultSet();
}
 
try finally public boolean del(String strcd) {
Del01DBBean Del01 = new Del01DBBean();
try {
  Del01.setCd(strcd);
  Del01.execute();
  return(true);
}
catch (Throwable theException) {
  return(false);
}
finally  {
  Del01.closeResultSet();
}
}
 
Sample public xxxBean(int index) {
  strCheck = new String[index];
}
protected String strKBN = "";
public String getKbn() {return(strKBN);}
public void setKbn(String value) {strKBN = value;}
protected String strCheck[];
public String getCheck(int index) {return(strCheck[index]);}
public void setCheck(String value, int index) {strCheck[index] = value;}
public String cKBN(String strKBN[]) {
  if (strKBN == null){
    return("");
  }
  if (strKBN[0].equals("on")){
    return("checked");
  }
  return("0");
}
----------------------------------------------------------------
B800.setKbn(new String(getParameter(request, "KBN", true, true, true,
null)));
xxx.setCheck(xxx.cKBN(request.getParameterValues("CHECK" +
String.valueOf(i))),i);
String wktest[] = request.getParameterValues("TEST");
xxx.setTest(test[index],index);
----------------------------------------------------------------
<INPUT <%= xxx.getChe(i) %> type="checkbox" name="CHECK<%=xxx.getIx(i) %>">
----------------------------------------------------------------
int p_rscnt = 0;
int p_pgcnt = 0;
int p_max = 10;
p_rscnt = rs.getNumResult();
p_pgcnt = (rscnt + p_max - 1) / p_max //最終ページ
Integer.toString(p_pgcnt);
 

Calendarクラスの持つ主なフィールド
フィールド名

説 明

DATE 日付を整数で示す
DAY_OF_MONTH DATEと同じ内容を示す
DAY_OF_WEEK 曜日を示す
DAY_OF_WEEK_IN_MONTH 現在の月の何度目の曜日かを示す
DAY_OF_YEAR 現在の年の何日目かを示す
HOUR 12時間制の時刻を示す
HOUR_OF_DAY 24時間制の時刻を示す
MILLISECOND 現在の時刻のうち、ミリ秒の部分を示す
MINUTE 現在の時刻のうち、分の部分を示す
MONTH 月を示す(1月が0、2月が1・・・となる)
SECOND 現在の時刻のうち、秒の部分を示す
WEEK_OF_MONTH 現在の月の何週目かを示す
WEEK_OF_YEAR 現在の年の何週目かを示す
YEAR 年を示す