java学习day1
idea Java
快捷键
psvm 生成一个main方法
sout输出
注释
// 单行注释
/* */多行注释
/** */JavaDoc 文档注释
关键字

数据类型
public class Demo02 {
public static void main(String[] args) {
//八大基本数据类型
//整数
byte num2=20;
short num3=30;
long num4=30L;//Long类型要在数字后面加个L
String a="陈曦";
int num =10;//最常用
//浮点数类型
float num5 = 50.1F;//float类型要在数字后面加个F
double num6 = 3.1415926;
//字符类型
char name ='A';//只能一个字符
//字符串,String不是关键字,是类
//String name="王畅";
//布尔值:是 否
boolean flag =true;
//boolean flag =false;
}
}
数据类型扩展
public class Demo03 {
public static void main(String[] args) {
//整数拓展 进制 二进制0b 十进制 八进制0 十六进制0x
int i=10;
int i2=010;// 八进制
int i3=0x10;//十六进制 0~9 A~F
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
//浮点数拓展 银行业务怎么表示?钱
//BigDecimal 数学工具类
//float 有限 离散 舍入误差 大约 接近但不等于
//double
//最好避免使用浮点数进行比较
//最好避免使用浮点数进行比较
//最好避免使用浮点数进行比较
float f =0.1f; //0.1
double d =1.0/10; //0.1
System.out.println(f==d); //false
float d1 =233133233f;
float d2 =d1 + 1;
System.out.println(d1 == d2);
//字符类拓展
char c1 ='a';
char c2 ='中';
System.out.println(c1);
System.out.println((int)c1);//强制转换
System.out.println(c2);
System.out.println((int)c2);//强制转换
//所有字符的本质都是数字
//编码 Unicode 2字节 0-65536 Excel 只有2的16次方
// U0000 -UFFFF
char c3 ='\u0061';
System.out.println(c3);// a
//转义字符
// \t制表符 相当于tab
// \n 换行
System.out.println("Hello \tWorld");
String sa = new String("hello world");
String sb = new String("hello world");
System.out.println(sa==sb);
String sc ="hello world";
String sd ="hello world";
System.out.println(sc==sd);
//对象 从内存分析
//布尔值扩展
boolean flag = true;
//两个if一样的
if (flag==true){}//新手
if (flag){}//老手
//Less is More! 代码需要精简易读
}
}
类型转换
public class Demo04 {
public static void main(String[] args) {
int i =128;
byte b= (byte)i;
double c = i;
//强制转换 (类型)变量名 高到低
//自动转换 低到高
System.out.println(i);
System.out.println(b);
System.out.println(c);
/*
注意点:
1.不能对布尔值进行转换
2.不能把对象类型转换为不相干的类型
3.把高容量转换到低容量的时候,强制转换
4.转换的时候可能存在内存溢出,或者精度问题
*/
System.out.println((int)23.7);//23
System.out.println((int)-45.89f);//-45
char c1 = 'a';
int d = c1+1;
System.out.println(d);
System.out.println((char) d);
}
}
public class Demo05 {
public static void main(String[] args) {
//操作比较大的数的时候,注意溢出问题
//JDK7新特性,数字之间可以用下换线分割
int money = 10_0000_0000;
int years = 20;
int total = money*years;//-1474836480
System.out.println(total);
long total2=money*years;//默认是int,转换之前已经存在问题了
long total3=money*(long)years;
System.out.println(total3);
//l L
}
}
浙公网安备 33010602011771号