




回
Javaは2005年に10歳の誕生日迎えます。
これを記念して、2005年3月10日から11日に、
"Java Computing 2005 Spring"
が開催され、
JC2005 SEOコンテスト
が実施されます。
| ソースファイル(sample.java) |
| コンパイラ(javac.exe) |
| バイトコード(sample.class) |
| インタプリタ実行(java+sample.class, appletviewer+sample.html+sample.class) |
class hello {
public static void main(String args[]) {
// int argc = args.length;
System.out.println("こんにちは");
System.out.println("args[0] = " + args[0]);
System.out.println("size = " + args.length);
}
}
|
| javac hello.java |
|
java hello arg1 arg2 こんにちは args[0] = arg1 size = 2 |
import java.awt.Graphics;
public class sample extends java.applet.Applet {
public void paint(Graphics g) {
g.drawString("こんにちは", 50, 25);
}
}
|
import java.awt.Graphics;
public class sample extends java.applet.Applet {
public void paint(Graphics g) {
g.drawString("\u3053\u3093\u306b\u3061\u306f", 50, 25);
}
}
|
|
<html> <head> <title>Java Sample Page</title> </head> <body> <applet code="sample.class" width=150 height=50> </applet> </body> </html> |
|
こんにちは arg1 |
| init() |
| start() |
| paint() |
| update() |
| stop() |
| destroy() |
import java.awt.Graphics;
public class sampled extends java.applet.Applet {
public void paint(Graphics g) {
g.drawString("Hello.", 50, 25)
}
}
|
javac sampled.java
sampled.java:4: ';' expected.
g.drawString("Hello.", 50, 25)
^
1 error
|
|
<html> <head> <title>Java Sample Page</title> </head> <body> <applet code="samplea.class" width=150 height=50> <param name=Arg1 value="first argument"> </applet> </body> </html> |
import java.awt.Graphics;
public class samplea extends java.applet.Applet {
String param1;
public void init() {
param1 = getParameter("Arg1");
if(param1 == null)
param1 = "こんにちは";
System.out.println(param1);
System.out.println("width="+size().width);
System.out.println("height="+size().height);
}
public void paint(Graphics g) {
g.drawString(param1, size().width/2, size().height/2);
}
}
|
class A{
B b = new B();
}
|
class B{
}
|
class Complex {
double real;
double imag;
void SetComplex(double x, double y) {
real = x;
imag = y;
}
}
class NewComplex extends Complex {
double GetRealComplex() {
return real;
}
}
| C 1 | C 2 | |
void main(){
int a=1, b=2;
/* scanf("%d", &a); */
printf("a=%d\n", a);
printf("b=%d\n", b);
swap(a, b);
printf("a=%d\n", a);
printf("b=%d\n", b);
}
void swap(int x, int y){
int t;
t = x;
x = y;
y = t;
}
|
void main(){
int a=1, b=2;
/* scanf("%d", &a); */
printf("a=%d\n", a);
printf("b=%d\n", b);
swap(&a, &b);
printf("a=%d\n", a);
printf("b=%d\n", b);
}
void swap(int *x, int *y){
int t;
t = *x;
*x = *y;
*y = t;
}
| |
| Method 1 | Method 2 | |
// Method 1
class swap {
public static void main(String args[]){
int a[]={1}, b[]={2};
System.out.println("a="+a[0]);
System.out.println("b="+b[0]);
this.swap(a, b);
System.out.println("a="+a[0]);
System.out.println("b="+b[0]);
}
static void swap(int x[], int y[]){
int t;
t = x[0];
x[0] = y[0];
y[0] = t;
}
}
|
class swapc {
public static void main( String args[]){
int a=1, b=2;
System.out.println("a="+a);
System.out.println("b="+b);
Swap sw = new Swap();
sw.swap(a, b);
a = sw.X;
b = sw.Y;
System.out.println("a="+a);
System.out.println("b="+b);
}
}
class Swap {
int X;
int Y;
void swap(int x, int y){
Y = x;
X = y;
}
}
|
| C | Java |
main() {
int a[]={0,1,2,3,4,5,6,7,8,9,10};
printf("sum(2-10)= %d\n",
sum(&a[2], 9));
}
int sum(int x[], int n){
int i, t = 0;
for(i=0; i < n; i++)
t += x[i];
return t;
}
|
class sumx {
public static void main(String args[]){
int a[]={0,1,2,3,4,5,6,7,8,9,10};
System.out.println(
"sum(2-10)="+sum(a, 2, 10));
}
static int sum(int x[], int m, int n){
int i, t = 0;
for(i=m; i < n; i++)
t += x[i];
return t;
}
}
|
| Item | Java | C | comments |
| comment | //,/* */ | /* */ | //行単位コメント |
| boolean | 1 bit(false/true) | 0=false, other=true | 基本データ型 |
| byte | 8 bits | char | 基本データ型 |
| short | 16 bits | 16 bits | 基本データ型 |
| int | 32 bits | 16/32 bits(MS-C/MS-C++) | 基本データ型 |
| long | 64 bits | 32 bits | 基本データ型 |
| float | 32 bits | 32 bits | 基本データ型 |
| double | 64 bits | 64 bits | 基本データ型 |
| char | 16 bits | 8 bits | 基本データ型 |
| character | char c='C',k='漢' | char c='C' | Unicode |
| character to string | s=char.toString(); | sprintf(s,"%c",char) | |
| string | String S="str" | char *s="str" | ポインタが無い |
| string compare | S1.equals(S2) | strcmp(s1,s2) | |
| string concatenation | S1+S2,"x1"+"y2" | strcat(s1,s2) | +演算子により結合 |
| c2s | S=String.valueOf(C) | s[0]=c; s[1]=0; | Character to String |
| s2c | C=S.charAt(0) | c=s[0] | String to Character |
| i2s | S=Integer.toString(I) | sprintf(s,"%d",i) | Integer to String |
| s2i | I=Integer.parseInt(S) | i=atoi(s) | String to Integer |
| d2s | S=Double.toString(D) | sprintf(s,"%lf",d) | Double to String |
| s2d | D=new Double(S).doubleValue() | d=atof(s) | String to Double |
| initialization | final int MAX=0 | #define MAX 0 | Pascalのconst |
| array | int a[]; a = new int[10]; int size = a.length; |
int a[10]; int size = sizeof(a)/sizeof(int); | |
| array initialize | int a[]={0,1,2}; | int a[3]={0,1,2}; | Javaではサイズ指定はNG |
| allocation | S = new Class | char *s; s=malloc(size) | Pascalのnew |
| while | while(true) | while(1) | |
| goto | none | goto label | goto less |
| break | break label | break | 多重ループ外 |
| continue | continue label | continue | 多重ループ外 |
| label | label: | label: | |
| exception/例外処理 | try{ } catch() {} finally { } | ||
| main arguments | main(String args[]) int argc = args.length; |
main(int argc, char *args[]) | |
| first argument | args[0] | args[1] | |
| function, method | func(),func(int x) | func(), funcx(int x) | 同名関数(引数依存) |
| function argument | call by reference(オブジェクト、配列) and value(基本データ型) |
call by value | 引数渡し |
| class definition |
class Complex{
double real;
double imag;
double getreal() {
return real;
}
}
|
struct Complex{
double real;
double imag;
} | |
| class usage | Complex X; X=new Complex(); X.real=1.0; double x=getreal(); |
struct Complex X; X.real=1.0; | |
| stdout | System.out.println("str"); | fprintf(stdout, "str"); | |
| stderr | System.err.println("str"); | fprintf(stderr, "str"); |
import java.awt.*
import javax.swing.*
import javax.swing.table.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*
//import java.awt.swing.*
public slider extededs Applet {
JSlider slider = new JSlider(JSlider.VERTICAL, 0, 255, 128);
slider.setPaintLabels(true);
slider.setPaintTicks(true);
slider.setMajorTickSpacing(0);
slider.setMinorTickSpacing(100);
add(slider);
setValueAt("Taro", 1, 1); /* row=1, col=1 */
setValueAt("Boy", 1, 2); /* row=1, col=2 */
setValueAt("Hanako", 2, 1); /* row=2, col=1 */
setValueAt("Girl", 2, 2); /* row=2, col=2 */
}
<html> <head><title>User Agent</title></head> <body> <a href=http://hostname/servlet/UserAgent>User Agent</a> </body> </html> |
import javax.servlet.*;
import java.io.*;
public class UsrAgent extends HttpServlet
{
/*
init counts;
public void init(ServletConfig config) throws ServletException {
super.init(config);
// read initial data (counts)
}
*/
public void doGet (HttpServletRequest request,
HttpServletResponse response) throws IOException
{
//log=/usr/local/jrun/jsm-default/services/jse/logs/event.log
log("UserAgent Test Message");
// for English characters
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
/* for Japanese characters (Servletで日本語を表示:
Shift_JIS, EUC-JP, ISO-2022-JP
resp.setContentType("text/html; charset=EUC-JP");
PrintWriter out = new PrintWriter(
new OutputStreamWriter(response.getOutputStream(), "EUC-JP"));
*/
out.println("<html><head;><title>User Agent</title></head><body>");
/*
out.println("<h2>Host Address<h2><hr>");
String hostaddress = req.getRemoteAddr();
if (hostaddress != null)
out.println("Host-Address : " + hostaddress + "<br>");
*/
out.println("<h2>User Agent<h2><hr>");
String clientBrowser = request.getHeader("User-Agent");
if (clientBrowser != null)
out.println("User-Agent : " + clientBrowser + "<br>");
out.println("</body></html>");
out.flush();
out.close();
}
/*
public void destroy() {
// write final data (counts)
}
*/
}
|
| Servlet |
import javax.servlet.*;
import java.io.*;
import=java.util.Date;
import=java.text.DateFormat;
public class UsrAgent extends HttpServlet
{
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws IOException
{
response.setContentType("text/html");
resp.setContentType("text/html; charset=EUC-JP");
ServletOutputStream out = response.getOutputStream();
out.println("<html><head><title>Servlet Date"
+ "</title></head><body>");
out.println("<h1>Servlet Date</h1>");
out.println(
DateFormat.getDateTimeInstance().format(new Date()));
out.println("</body></html>");
out.flush();
out.close();
}
}
|
Compile: manually compiled at once time URL: http://host/~usr/servlet/date FILE: ~usr/public_html/WEB-INF/classes/date.class Writer: Java programmers Technology: Java |
| JSP |
<-- JSP で 日本語 表示 (SJIS, Shift_JIS, EUC-JP, ISO-2022-JP) --> <%@ page import="java.util.Date, java.text.DateFormat" contentType="text/html; charset=EUC-JP" %> <html> <head> <title>JSP Date</title> </head> <body> <h1>JSP Date</h1> <%= DateFormat.getDateTimeInstance().format(new Date()) %> </body> </html> |
Compile: automatically compiled at once time URL: http://host/~user/date.jsp FILE: ~user/public_html/date.jsp Writer: Web designers Technology: Java Beans |
| SSI (Server Side Include) |
<html> <head> <title>SSI Date</title> </head> <body> <h1>SSI Date</h1> <!-- #exec cmd="date" --> </body> </html> |
Compile: No URL: http://host/~user/date.html FILE: ~user/public_html/date.html Writer: Web designers Technology: HTML |
| オブジェクト | 型 | スコープ | 説明 | 使用例 |
|---|---|---|---|---|
| request | javax.servlet.ServletRequest | request | リクエスト | request.getParameter("param"); |
| response | javax.servlet.ServletResponse | page | レスポンス | |
| out | javax.servlet.jsp.JspWriter | page | 出力ストリームオブジェクト | out.println("<html>"); |
| page | java.lang.Object | page | サーブレットのthisオブジェクト | |
| session | javax.servlet.http.HttpSession | session | セッションオブジェクト | session.getAttribute("name"); |
| application | javax.servlet.ServletContext | application | サーブレットコンテキストオブジェクト |
javax.servlet.ServletConfig sc = getServletConfig(); String st = sc.getServletContext().getInitParameter("name"); application.setAttribute("name", st); |
| config | javax.servlet.ServletConfig | page | サーブレットコンフィグオブジェクト | config.getInitParameter("name"); |
| pageContext | javax.servlet.jsp.PageContext | page | ページコンテキストオブジェクト | |
| exception | java.lang.Throwable | page | 例外オブジェクト |
<html> <head> <title>Java DataBase Connection</title> </head> <body> <applet archive=postgresql.jar code=dbjava.class width=400 height=200> </applet> </body> </html> |
import java.sql.*;
import java.applet.Applet;
public class dbjava extends Applet {
public void init() {
String msg = "Load!";
try{
Class.forName("postgresql.Driver");
}
catch(ClassNotFoundException ex){
msg = "JDBCドライバをロードできません";
}
System.out.println(msg);
}
}
|
| Version | Software | Download | Remarks |
| Version 1 | JDK 1.1.x | Download 1.1.8_008 | Platform, No plugin for bowsers |
| JRE 1.1.x | Download 1.1.8 | Platform, No plugin for bowsers | |
| Version 2 Standard Edition (J2SE) | JSDK | Download 1.4 ('02.02) Download 1.3.1_03 by Sun Download 1.3 for Linux |
Platform, Plugin for bowsers |
| JRE 2 | Download 1.3 | Platform, Plugin for bowsers | |
| Plugin | Download 1.3 | For IE, Netscape and so on | |
| Java 2 SDK, Enterprise Edition | J2EE | Download 1.2.1 Download 1.3.1 |
Solaris SPARC 7, 8 Windows NT 4.0, Windows 2000 Professional Linux Redhat, v. 6.0 |
| JavaServer Web Development Kit | JSWDK | Download | Platform |
|
set PATH=C:\WINDOWS;C:\WINDOWS\COMMAND;C:\jdk1.1.8\bin; CLASSPATH=.;C:\jdk1.1.8\lib\classes.zip; |
|
vi japanese.java native2ascii japanese.java > sample.java javac -O sample.java appletviewer sample.html |
| JDK1.0 | JDK1.1 | JDK1.2=JDK2 |
| resize | setSize | |
| action | actionPerformed(ActionListener) | |
| AWT | AWT | SWING |
int x[10000][10000];
int sum = 0;
for(int i = 0; i < 10000; i++)
for(int j = 0; j < 10000; j++)
sum += x[i][j];
|
/* Variable length Array
x[0][0]=1 x[0][1]=2 x[0][2]=3
x[1][0]=4 x[1][1]=5
x[2][0]=6
*/
class array {
public static void main(String args[]) {
int x[][] = {{1, 2, 3}, {4,5}, {6}}; //Initialization
printArray(x);
}
public static void printArray(int x[][]) {
for(int i = 0; i < x.length; i++) {
for(int j = 0; j < x[i].length; j++)
System.out.print("x["+i+"]["+j+"]="+x[i][j]+" "
);
System.out.println(" ");
}
}
}
|