TEST.java -> TEST.class (java虚拟机机制) 在windows上和在Linux都可以运行
.java文件 编译后 .class文件 运行后 结果
(源文件) Javac (字节码文件) java
编译:通过编译器将其编译成JVM可以识别的字节码文件 通过javac编译工具对其.java文件进行编译 如果程序没有错误 酒鬼生成字节码文件,即为可以执行的Java程序
运行:本质就是将 .class文件装载到 jvm机执行
注意:对修改后的Java文件需要重新编译后,再生成新的.class文件后才能正常运行
//注释,也代表转义字符的演示
// \n 换行符
// \t 对齐符
public static void main(String[] args){
System.out.println(" ");
}
// \ 输出一个斜杠 写法: \\win11
// 第一个用作转义符 第二个才是真正输出的\
// 如果要输出两个 \ 写法: \\\win11
// 想要输出双引号 “” 写法: "老韩说:\"要好好学习\""
// \r 一个回车
public static void main(String[] args){
System.out.println("韩老师教育\r北京");
}
// 输出结果为:北京师教育
如果改为:
public static void main(String[] args){
System.out.println("韩老师教育\r\n北京");
}
// 输出结果为:韩老师教育
// 北京
// 单行注释
/*
多行注释
*/
细节: 注释掉的代码块是不会被程序 (解释机) 执行
多行注释中不允许再嵌套多行注释 有两个或多个结尾字符!!!!
javadoc 文档注释
基本格式:
javadoc标签
Javadoc -d 文件夹名 -xx -yy 文件.java (-xx和-yy就是标签)
/**
* @author 王帅
* @version 1.0
*/
public static void main(String[] args){
System.out.println(" ");
}
标签
描述
示例
标识一个类的作者
@author description
指名一个过期的类或成员
@deprecated description
{@docRoot}
指明当前文档根目录的路径
Directory Path
标志一个类抛出的异常
@exception exception-name explanation
{@inheritDoc}
从直接父类继承的注释
Inherits a comment from the immediate surperclass.
{@link}
插入一个到另一个主题的链接
{@link name text}
{@linkplain}
插入一个到另一个主题的链接,但是该链接显示纯文本字体
Inserts an in-line link to another topic.
说明一个方法的参数
@param parameter-name explanation
说明返回值类型
@return explanation
指定一个到另一个主题的链接
@see anchor
说明一个序列化属性
@serial description
说明通过writeObject( ) 和 writeExternal( )方法写的数据
@serialData description
说明一个ObjectStreamField组件
@serialField name type description
标记当引入一个特定的变化时
@since release
和 @exception标签一样.
The @throws tag has the same meaning as the @exception tag.
{@value}
显示常量的值,该常量必须是static属性。
Displays the value of a constant, which must be a static field.
指定类的版本
@version info
import java.io.*;
/**
* 这个类演示了文档注释
* @author Ayan Amhed
* @version 1.2
*/
public class SquareNum {
/**
* This method returns the square of num.
* This is a multiline description. You can use
* as many lines as you like.
* @param num The value to be squared.
* @return num squared.
*/
public double square(double num) {
return num * num;
}
/**
* This method inputs a number from the user.
* @return The value input as a double.
* @exception IOException On input error.
* @see IOException
*/
public double getNumber() throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader inData = new BufferedReader(isr);
String str;
str = inData.readLine();
return (new Double(str)).doubleValue();
}
/**
* This method demonstrates square().
* @param args Unused.
* @return Nothing.
* @exception IOException On input error.
* @see IOException
*/
public static void main(String args[]) throws IOException
{
SquareNum ob = new SquareNum();
double val;
System.out.println("Enter value to be squared: ");
val = ob.getNumber();
val = ob.square(val);
System.out.println("Squared value is " + val);
}
}
int n;
int a = 10;
int b = a + n;
import java.awt.*;
import javax.swing.*;
public class Circle99Frame extends JFrame {
public static void main(String args[]){
JFrame frame=new Circle99Frame();
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.setSize(600,600);
frame.setVisible(true);
}
public void paint( Graphics g){
g.drawString("circle 99",20,20);
int x0=getSize().width/2;
int y0=getSize().height/2;
for(int r=0;r<getSize().height/2;r+=10){
g.setColor(getRandomColor());
g.drawOval(x0-r,y0-r,r*2,r*2);
}
}
Color getRandomColor(){
return new Color(
(int)(Math.random()*255),//random本身只产生(0~1)之间的小数,
(int)(Math.random()*255),
(int)(Math.random()*255)
);
}
}
import java.awt.*;
import javax.swing.*;
public class Circle99Frame extends JFrame
{
public static void main(String args[])
{
JFrame frame=new Circle99Frame();
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.setSize(600,600);
frame.setVisible(true);
}
public void paint( Graphics g)
{
g.drawString("circle 99",20,20);
int x0=getSize().width/2;
int y0=getSize().height/2;
for(int r=0;r<getSize().height/2;r+=10)
{
g.setColor(getRandomColor());
g.drawOval(x0-r,y0-r,r*2,r*2);
}
}
Color getRandomColor()
{
return new Color(
(int)(Math.random()*255),//random本身只产生(0~1)之间的小数,
(int)(Math.random()*255),
(int)(Math.random()*255)
);
}
}
变量是程序的基本组成单位。
不论是用通过哪种高级语言编写程序,变量都是基本单位。
类型,名称,数值
public static void main(String [] args){
int a=1;
int b=3;
b=89;
System.out.println(a);
System.out.println(b);
}
用单独的变量来解决问题不利于解决数据的管理
Java的设计者引入类与对象就是基于现有的问题新开发的一种手段和技术
class Cat{
// 属性(成员变量)field
String name;
int age;
String color;
}
//实例化一个猫类
public static void main(String [] args){
Cat cat1 = new Cat(); //用 cat1 来接受new的cat
cat1.name = "小白";
cat1.age = 3;
cat1.color = "白色";
Cat cat2 = new Cat();
cat2.name = "小黑";
cat2.age = 6;
cat2.color = "黑色";
//访问对象属性
System.out.println("第一只猫的信息"+cat1.name+" "+cat1.age+" "+cat1.color)
}
理解类与对象
数据一定有它对应地址
class Person{
int age;
String name;
double str;
boolean isPass;
}
public static void main(String [] args){
// 创建person对象
// p1是对象名(对象引用)
// new Persom() 创建的对象空间(数据)才是真正的对象
Person p1 = new Person();
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章