java学习一
流火的时节,北方金城当然也是爆热的 ,好在这几天下了几场雨,顿感凉快
了许多,早上起来,不远处山间起了雾气,空气也是人特喜欢那种,好久没有这种感觉了
天气好,精神也好了许多,就把这几天学习java做一个小结。
1、 JDK的下载和安装
JDK又叫做J2SE(Java2 SDK Standard Edition),可以从Sun的Java网站上下载到,在
C:\j2sdk1.4.2下安装一套JDK。然后我们需要在环境变量PATH的最前面增加java的路径
C:\j2sdk1.4.2\bin。这样JDK就安装好了。
2、 JDK的命令工具
JDK的最重要命令行工具:
java: 启动JVM执行class
javac: Java编译器
jar: Java打包工具
javadoc: Java文档生成器
appletviewer:applet小程序查看器。
这些命令行必须要非常非常熟悉,对于每个参数都要很精通才行。对于这些命令的学习,
JDK Documentation上有详细的文档。
二、 JDK Documentation
Documentation在JDK的下载页面也有下载连接,建议同时下载Documentation。
Documentation是最最重要的编程手册,涵盖了整个Java所有方面的内容的描述。可以这样
说,学习Java编程,大部分时间都是花在看这个Documentation上面的。没有中文版的也就
罢了,偏偏有日文版的,别的我什么都不说了,下个英文版吧,没得选择了。我在sun的网
站上下的,40多K,一会就下完了,要是ADSL会更快一些。
三。几个例子
关于如何编译,运行我就不说了吧。简单。
1)HelloWorld
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
2)简单字符串
class Str
{
public static void main(String[] args)
{
int x=0,y=1,z=2;
String string="x,y,z";
System.out.println(string+x+y+z);
System.out.println(y+z+string);
}
}
//java中对输出基本上都有+法运算。而且各种对象都能用System.out.println(),
3)精神病医生:你和该医生对话,你一句,他回答一句。我并没有实现AI,只是演示如何
写java程序。
import java.io.*;
public class Doctor
{
public static void main(String [] args) throws IOException
{
Doctor dr=new Doctor();
dr.talk(System.in,System.out);//System 类中in为InputStream,out 为
OutStream类型
}
void talk(InputStream in,OutputStream out) throws IOException
{
BufferedReader rd=new BufferedReader(
new InputStreamReader(in));
PrintWriter pw=new PrintWriter(
new OutputStreamWriter(out),true);
pw.println("Hello,I am a Robot-Doctor,I know many things,what are
you want to know?");
while(true)
{
String question=rd.readLine();
reply(pw,question);
}
}
void reply(PrintWriter pw,String question) throws IOException
{
if(0==question.compareTo("I love you,Miss Ma!"))
pw.println("I love you,too");
else if(question.compareTo("Who are you?")==0)
pw.println("I am Dactor Ma!");
else if(0==question.compareTo("1+1="))
pw.println("haha ,I know,It's 2!");
else pw.println("I am sorry!I don't know what You said!
"+question+" what's it meas!!! mm mm...");
}
}
4)多线程例子,本例中有两个线程,主线程和t1线程。实现Runnable接口。
public class SimpleRunnable implements Runnable
{
private String message;
public static void main(String[] args)
{
SimpleRunnable r1=new SimpleRunnable("Hello");
Thread t1=new Thread(r1);
t1.start();
for(;;)
{
System.out.println("Bye-bye");
}
}
public SimpleRunnable(String message)
{
this.message=message;
}
public void run() //覆盖接口的方法
{
for(;;)
{
System.out.println(message);
}
}
}
输出结果如下:Hello,Bye-bye不断交替,是个死循环,按Ctrl+ C结束运行。
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Bye-bye
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
5)文件IO操作:本例可以用来打开文本文件,运行时输入:java FileIO filename
import java.io.*;
public class FileIO
{
public static void main(String[] args) throws Exception
{
if(args.length!=1)
{
System.out.println("Invalid parameters!!!");
System.exit(0);
}
File fl = new File(args[0]);
FileReader fileReader = new FileReader(fl);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String currentLine;
while( (currentLine = bufferedReader.readLine()) != null )
{
System.out.println(currentLine);
}
}
}
6)网络编程:本例实现如何查看一个域名的IP地址。
运行时输入:java WhoAmI 域名 (不知道这个词用得对不对)
例:1 输入java WhoAmI qihl
输出为:219.246.84.55 (注:qihl是我的计算机名,IP是我的IP)
2 输入java WhoAmI www.yahoo.com
输出为:www.yahoo.com/68.142.197.66
import java.net.*;
public class WhoAmI
{
public static void main(String[] args) throws Exception
{
if(args.length!=1)
{
System.err.println("Usage:WhoAmI MachineName");
System.exit(1);
}
InetAddress a=InetAddress.getByName(args[0]);
System.out.println(a);
}
}
7)Applet编程:本例实现一个简单的画板,注意它可在IE中运行,画图。
先编译通过,然后将Paint.class 和Paint.html 放在同一个目录下。然后用IE打开
Paint.html ,即可实现画图,速度很慢,要有点耐心才行啊,不过大家都还年轻,有的是
时间。
也可以用jdk的工具appletviewer:即在命令窗口下输入:appletviewer Paint.java 即
可看到运行结果。
//html文件如下:
<html><title>画笔</title>
<body>hao
<applet code="Paint.class" width=200 height=200></applet>
</body>
</html>
//一下是Paint.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Paint extends Applet
{
Vector v = new Vector();
public void paint(Graphics g)
{
int [] x,y;
x=new int[v.size()];
y=new int[v.size()];
for(int i=0;i<v.size();++i)
{
Point p=(Point)v.get(i);
x[i]=p.x;
y[i]=p.y;
}
g.drawPolyline( x,y, v.size());
}
public void init()
{
addMouseMotionListener(
new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
Point
p=e.getPoint();
v.add(p);
System.out.println(p);
System.out.println(v.size());
repaint();
}
});
}
}
//<applet code="Paint.class" width=200 height=200></applet>
四。一点感想
以前一直用c---c++---vc,这几天学习java以后觉得java里面好多东西都比vc下
要来得简单。不过速度方面可能要稍逊一点。vc功能之强大是有目共睹的。然java在很多
方面更显优势,比如网络编程,数据库编程等等,还是那几句话:
-----“捉的住老鼠就是好猫”( 小平的观点,无论它的名字叫java还是c++)
-----“编程贵在思想,语言只是工具” (大家的观点,特别是高手)
-----“没有做不到,只有想不到”(我个人的目标,有些时候真的觉得技术已经掌握,可
就是想不到很多东西)
五)感慨之后,还是继续hard hard study,但愿能day day up!