Java基础
注释
public class helloworld {
public static void main(String[] args) {
//单行注释
/*
多
行
注
释
*/
//javadoc:文档注释
/**
* (可以加参数,如下)
* @Description Helloworld
* @Author zhihaolin
*/
//java对大小写非常敏感
String author = "zhihaolin";
System.out.println("Hello World!");
}
}
基本数据类型
public class Demo01 {
public static void main(String[] args) {
String a = "Hello World!";
//String是个类,不是关键词
int b = 10;
byte c = 20;
short d = 30;
long e = 465L;//long类型要在数字后面加上L
//浮点数类型
float f = 3.1415F;//浮点数型要在后面加上F
double g = 3.14159265358;
//字符类型
char h = '林';
//bool类型
boolean J = true;
System.out.println(a);
System.out.println(b);
}
}
数据类型知识拓展
public class Demo02 {
public static void main(String[] args) {
//整数拓展; 进制 二进制0b 八进制0 十进制 十六进制0x
int a = 1;
int b = 0b100;
int c = 04;
int d = 0x100;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println("=======================================");
//=======================================================================
//浮点数拓展:有限 离散 舍入误差 接近但不等于
//double
//BigDecimal 数学工具类(以后使用)
//最好不要使用浮点数进行比较,例如下:
float i = 0.1f;
double i2 = 1.0/10;
System.out.println(i==i2);/*运行结果为false,因为浮点数为0.1,而double型1.0除以10位数不同
括号内i==i2是判断,判断i是否等于i2,是则输出true,否则输出false.*/
float n = 3141592653589793f;
float n2 = n + 1;
System.out.println(n==n2);//ture因为表示的位数有限,超出上限无法比较
//=======================================================================
//字符类拓展
char h = 'a';
char h2 = '林';
System.out.println(h);
System.out.println((int)h);
System.out.println(h2);
System.out.println((int)h2);//强制类型转换
//=======================================================================
//转义字符
//\t 制表符,输出为四个空格
//\n 换行,输出时换行
//......
System.out.println("Hello\nWorld!");//输出时hello和world为两行
//=======================================================================
//布尔型拓展
boolean k = true;
if (k==true){ }
if (k){ }//这两行表示的意思相同,不指定if的判定条件时,默认为true
//less is more(代码要精简易读)
}
}