きったんの頭

HOME > Java

Java

| Hello World | | データ型 | 演算子 | 予約語 | ログ | Applet | Javadoc | リンク |

Hello World

HelloWorld.java
class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello World!");
  }
}
実行
> javac HelloWorld.java

> java HelloWorld
Hello World!

jar

manifest.mf
※:の後の半角スペース、最後の改行必須
Manifest-Version: 1.0
Main-Class: HelloWorld
作成
> jar cvfm HelloWorld.jar manifest.mf HelloWorld.class
実行
> java -jar HelloWorld.jar
Hello World!

コメントアウト

データ型

サイズ初期値範囲
byte8bit0-128 ~ 127
short16bit0-32768 ~ 32767
int32bit0-2147483648 ~ 2147483647
long64bit0-9223372036854775808 ~ 9223372036854775807
float32bit0.0F±3.40E38 ~ ±1.40E-45
double64bit0.0D±1.79E+308 ~ ±4.94E-324
char16bit'\u0000'2byte の文字コード
boolean1bitfalsetrue または false

型変換

//String str ⇒ int n
int n = Integer.parseInt(str, 10);

//int n ⇒ String str
String str = Integer.toString(n);

//float f ⇒ int n
int n = (int)f;

演算子

優先順位

降順
  1. ! ~ ++ -- + -
  2. * / %
  3. + - =
  4. << >> >>>
  5. < <= > >=
  6. == !=
  7. &
  8. ^
  9. |
  10. &&
  11. ||

予約語

abstract assert boolean break byte case catch char class const continue default do double else extends final finally float for goto if implements import instanceof int interface long native new package private protected public return short static strictfp super switch synchrnized this throw throws transient try void volatile while

ログ

java -Djava.util.logging.config.file=/path/to/logging.properties MainClass
# logging.properties
handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler

.level = ALL

java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

java.util.logging.FileHandler.level = WARNING
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.encoding = UTF-8
java.util.logging.FileHandler.append = false

# %t - temp
# %h - home
# %g - generation
# %u - unique
java.util.logging.FileHandler.pattern = log/%u_%g.log
java.util.logging.FileHandler.count = 7

#java.util.logging.FileHandler.filter =
#java.util.logging.FileHandler.limit = 102400

Applet

AppletSample.java
/*
<applet code="AppletSample.class" width="200" height="50"></applet>
*/
import java.applet.Applet;
import java.awt.Graphics;

public class AppletSample extends Applet {
	public void paint(Graphics g) {
		g.drawString("Hello, World", 70, 30);
	}
}
実行
> javac AppletSample.java
appletviewer
> appletviewer AppletSample.java
HTML
<html>
<head>
<title>AppletSample</title>
</head>
<body>
<applet code="AppletSample.class" width="200" height="50"></applet>
</body>
</html>

Javadoc

/** */コメント内にjavadocタグを付けて記入。HTMLタグも使える。

DocSample.java
実行
javadoc -d doc DocSample.java

タグ

IDE

ツール

スニペット/サンプル

入門