首页 > 编程语言 > 详细

Java基础知识扩展

时间:2021-06-15 09:32:55      阅读:19      评论:0      收藏:0      [点我收藏+]

Java基础知识扩展

 

浮点数扩展


//银行业务怎么表示?(钱)
//BigDecimal 数学工具类
?
float f = 0.1f;
double d = 1.0/10;
System.out.println(f==d); //false
?
float f1 = 12323465623452345f;
float f2 = f1 + 1;
System.out.println(f1==f2); //true
?
//float 有限;离散;舍入误差;大约;接近但不等于
//double

最好完全避免使用浮点数进行比较

最好完全避免使用浮点数进行比较

最好完全避免使用浮点数进行比较

 

字符扩展


char c1 = ‘A‘;
char c2 = ‘中‘;
?
System.out.println(c1);
System.out.println((int)c1); //强制转换
System.out.println(c2);
System.out.println((int)c2); //强制转换
?
//A
//65
//中
//20013
?
/** 编码方式
ANSI:美国国家标准协会,系统预设的标准文字储存格式。
?
GB2312:简体中文编码,实际上它是ANSI的一个代码页936
?
UTF-8:通用字集转换格式,这是为传输而设计的编码,2进制,以8位为单元对Unicode进行编码,如果使用只能在同类位。
    元组内支持8个位元的重要资料一类的旧式传输媒体,可选择UTF-8格式。
在UTF-8里,英文字符仍然跟ASCII编码一样,因此原先的函数库可以继续使用。而中文的编码范围是在0080-07FF之间,
因此是2个字节表示(但这两个字节和GB编码的两个字节是不同的),用专门的Unicode处理类可以对UTF编码进行处理。
?
java语言使用Unicode字符集,最多包含65536个字符。
*/
?
char c3 = ‘\u0061‘;
System.out.println(c3);
?
//a

所有的字符本质还是数字

 

字符串扩展


int a = 10;
int b = 20;
System.out.println(""+a+b); //1020
System.out.println(a+b+""); //30
?
System.out.println(""+(a+b); //30
System.out.println(a+(b+"")); //1020
?
//+运算符也可作字符串连接符,运算方式为从右往左
//关于运算符优先级的问题,若不记得,可使用圆括号()决定先算谁后算谁

 

转义字符


//制表符:\t
//换行符:\n
//......
?
System.out.println("Hello\tWorld"); //Hello   World
?
System.out.println("Hello\nWorld");
?
//Hello
//world

 

动态区(非常量池)和常量池


String s1 = new String("hello world");
String s2 = new String("hello world");
System.out.println(s1==s2); //false
System.out.println(s1.equals(s2)) //true
   
//对象s1和s2存放的都是引用,表示自己实体的位置(凡是new出来的对象都不在常量池,而在动态区)
   
String s3 = "hello world";
String s4 = "hello world";
System.out.println(s3==s4); //true
?
//String常量也是对象,是用双引号括起来的字符序列,也有自己的实体和引用
//常量池中的数据在程序运行期间再也不允许被改变
   

 

布尔值扩展


boolean flag = true;
?
if (flag==true){} //新手
if (flag){} //老手
?
//less is more 代码要精简易读

 

三元运算符


// x ? y : z
//如果x==true,则结果为y,否则结果为z
//if
?
int score = 60;
String type = score>=60 ? "及格":"不及格";
?
System.out.println(type); //及格

 

增强for循环


int [] arr = {1,2,3,4,5};
?
//数组名.for(IDEA快捷)
//JDK1.5   没有下标       遍历数组
?
for (int i : arr) {
   System.out.println(i);
}
?
//1
//2
//3
//4
//5

 

Java基础知识扩展

原文:https://www.cnblogs.com/ryan51/p/14883829.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!