浮点数拓展跟狂神学java笔记www.kuangstudy.com
银行业务表示
-
float 有限 离散 舍入误差 大约 接近但不等于
-
double 最好完全使用浮点数进行比较
public class Test{
public static void main(String[] args){
float f = 0.1F; //0.1
double d = 1.0/10; //0.1
System.out.println(f==d); //false
float d1 = 2313121311313131313f;
float d2 = d1 + 1;
System.out.println(d1==d2); //true
}
}
转存失败重新上传取消
字符拓展public class Test{
public static void main(String[] args){
char c1 = 'A';
char c2 = '中';
System.out.println(c1);
System.out.println((int)c1); //强制转换
System.out.println(c2);
System.out.println((int)c2); //强制转换
//所有的字符的本质还是数字
//编码 Unicode 表:(97 = a; 65 = A) 2字节 0-65536;
char c3 = 'u0061';
System.out.println(c3);//a
//转义字符
//t 制表符
//n 换行
}
}
转存失败重新上传取消
布尔值拓展boolean flag = true;
if (flag==true){} //新手
if(flag){} //老手
//代码要精简易读
类型转换
int i = 128; //128 byte b = (byte)i;// -128;内存溢出 //强制转换 (类型)变量名 高--低 //自动转换 低--高 System.out.println(i); System.out.println(b); //JDK7新特性,数字之间可以用下划线分割不影响输出 int money = 10_0000_0000; int years = 20; int total = money*years;//-1474836480,计算的时候溢出 long total2 = money*years;//默认是int,转换之前已经存在问题 long total3 = money*((long)years); System.out.println(total3);变量作用域
public class Test{
//类变量 static
static double salary = 3000;//工资
//属性:变量
//实例变量:从属于对象;如果不自行初始化,这个类型的默认值 0 0.0
//布尔值:默认false
//除了基本类型,其余的默认值都是null;
String name;
int age;
//main方法
public static void main(String[] args){
//局部变量;必须声明和初始化值
int i = 10;
System.out.println(i);
//变量类型 变量名字 = new Demo08();
Demo08 demo08 = new Demo08();
System.out.println(demo08.age);
System.out.println(demo08.name);
}
}
常量
public class Demo02{
//修饰符,不存在先后顺序
static final double PI = 3.14;
// 常量 名字
public static void main(String[] args){
System.out.printlne(PI);
}
}
转存失败重新上传取消
运算符算术运算符
public class Demo01 {
public static void main(String[] args) {
//二元运算符
//Ctrl + D :复制当前行到下一行
int a = 10;
int b = 20;
int c = 25;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b);
}
}
public class Demo02 {
public static void main(String[] args) {
long a = 123242342342342L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d);//Long
System.out.println(b+c+d);//Int
System.out.println(c+d);//Int
//向上兼容
}
}
关系运算符
public class Demo03 {
public static void main(String[] args) {
//关系运算符返回的结果: 正确,错误 布尔值
//if
int a = 10;
int b = 20;
int c = 21;
System.out.println(a>b);
System.out.println(a
自增自减运算符
public class Demo04 {
public static void main(String[] args) {
//++ -- 自增,自减 一元运算符
int a = 3;
int b = a++; //a++ 先给b执行赋值操作,在自增 a = a + 1
System.out.println(a);
System.out.println(b);
int c = ++a;
System.out.println(c); //执行代码前,先自增,在给b赋值
//++a a = a + 1
//幂运算 使用工作类
double pow = Math.pow(2,3);
System.out.println(pow);
}
}
逻辑运算符
public class Demo05 {
//逻辑运算符
public static void main(String[] args) {
//与(and) 或(or) 非(取反)
boolean a = true;
boolean b = false;
System.out.println("a && b: "+(a&&b));//逻辑与运算:两个变量都为真,结果才为true
System.out.println("a || b: "+(a||b));//逻辑或运算:两个变量有一个为真,结果就为true
System.out.println("! (a && b) : "+!(a&&b));//逻辑非运算:如果为真,结果变为假,如果为假则变为真
//短路运算
int c = 5;
boolean d = (c<4)&&(c++<4); //同真才为真
System.out.println(d); //false (c<4)为假,则无论后边是真是假都没有意义,所以不再做计算
System.out.println(c); //c=5
}
}
位运算符
public class Demo06 {
//位运算
public static void main(String[] args) {
System.out.println(2<<3);
}
}
字符串连接符
public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b;//a = a+b
a-=b;//a = a-b
System.out.println(a);
//字符串连接符 +运算符两侧只要出现了 , String(字符串类型)就会都转化成String类型并连接
System.out.println(a+b); //30
System.out.println(""+a+b); //1020
System.out.println(a+b+""); //30
}
}
三元运算符
public class Demo08 {
//三元运算符
public static void main(String[] args) {
// x ? y : z 条件运算符
//如果x==true,则结果为y,否则结果为z
int score = 80;//成绩
String type = score <60 ? "不及格" : "及格";//必须掌握
//if
System.out.println(type);
}
}
包机制
-
一般利用公司域名倒置作为包名;
-
为了能够使用某一个包的成员,我们需要在Java程序中明确导入该包。使用“import"语句完成此功能
import package1[.package2...].(classsname|*);
JavaDoc用来生成自己的API文档
参数信息
-
@author作者名
-
@version版本号
-
@since指明需要的最早使用的jkd版本
-
@param参数名
-
@return返回值情况
-
@throws异常抛出情况
-
package com.langzistudy.base;
public class Doc {
String name;
public String test(String name) throws Exception {
return name;
}
}
//命令行 javados 参数 java文件
//学会查找使用IDEA生产JavaDOS文档! 面向百度编程!
Scanner(文本扫描)对象
-
next():
-
一定要读取到有效字符后才可以结束输出
-
对输入有效字符之前遇到的空白,next()方法会自动去掉
-
只有输入有效字符后才能将其后面输入的空白作为分隔符或者结束符。
-
next()不能得到带有空格的字符串
-
-
nextLine():
-
以Enter为结束符,返回的是输入回车之前的所有字符。
-
可以获得空白
-



