数学中,没有办法用十进制来精确表达1/3.
同样的,计算机中,没有办法用二进制表示0.1
比如:python中,java中 都是一样的。
1>>> a=6.0 2>>> b=4.9 3>>> a-b 41.0999999999999996 5>>> x=3.0 6>>> y=2.9 7>>> x-y 80.10000000000000009 9>>> (x-y)>0.1 10True 11 12>>> (x-y)==0.1 13False 14>>>
在上述代码中: a-b 的值会无限接近于1.1。 x-y 的值会无限接近0.1。
1 1 package day02; 2 2 3 3 import java.util.Scanner; 4 4 5 5 public class inp_demo { 6 6 7 7 public static void main(String[] args) { 8 8 // TODO Auto-generated method stub 9 9 Scanner scanner=new Scanner(System.in); 1010 String name=scanner.next(); 1111 System.out.println(name); 1212 1313 } 1414 1515 }
数据类型的练习:
1 1 package day02; 2 2 3 3 import java.util.Scanner; 4 4 5 5 public class inp_demo { 6 6 7 7 public static void main(String[] args) { 8 8 // TODO Auto-generated method stub 9 9 // Scanner scanner=new Scanner(System.in); 1010 // String name=scanner.next(); 1111 // System.out.println(name); 1212 int a=250; 1313 //int b=100000000000; 1414 System.out.println(5/2); 1515 System.out.println(2/5); 1616 int c=2147483647; 1717 System.out.println(c+1); 1818 1919 //long d=10000000000; 2020 long e=10000000000L; 2121 long f=1000000000*2*10L; 2222 System.out.println("f:"+f); 2323 long g=1000000000*3*10L; 2424 System.out.println("g>>>"+g); 2525 long h=1000000000L*3*10; 2626 System.out.println("e>>>"+h); 2727 long i; 2828 i=System.currentTimeMillis(); 2929 System.out.println("time:"+i); 3030 3131 3232 float j=3.14f; 3333 float k,l; 3434 k=6.0f; 3535 l=4.9f; 3636 System.out.println(k-l); 3737 3838 } 3939 4040 }
结果:
12 20 3-2147483648 4f:20000000000 5g>>>-12949672960 6e>>>30000000000 7time:1535892324164 81.0999999

1 1 package day02; 2 2 3 3 import java.util.Scanner; 4 4 5 5 public class inp_demo { 6 6 7 7 public static void main(String[] args) { 8 8 // TODO Auto-generated method stub 9 9 1010 // //布尔类型,占一个字节。 1111 // boolean b1=true; 1212 // boolean b2=false; 1313 //字符类型,占有两个字节 java的字符编码表是unicode 1414 char o3='m'; 1515 char o4=' '; 1616 char o5=65515;//0~65535 1717 System.out.println(o5); 1818 char o='中'; 1919 System.out.println(o); 2020 2121 char o1='\u4e2d'; 2222 System.out.println(o1); 2323 2424 char o2=65; 2525 System.out.println(o2); 2626 2727 System.out.println("(2+2):"+(2+2)); 2828 System.out.println('2'+'2'); 2929 3030 System.out.println("char2》\t》》\n》"+'2'); 3131 3232 3333 3434 3535 3636 3737 3838 3939 4040 4141 // Scanner scanner=new Scanner(System.in); 4242 // String name=scanner.next(); 4343 // System.out.println(name); 4444 /* 4545 int a=250; 4646 //int b=100000000000; 4747 System.out.println(5/2); 4848 System.out.println(2/5); 4949 int c=2147483647; 5050 System.out.println(c+1); 5151 5252 //long d=10000000000; 5353 long e=10000000000L; 5454 long f=1000000000*2*10L; 5555 System.out.println("f:"+f); 5656 long g=1000000000*3*10L; 5757 System.out.println("g>>>"+g); 5858 long h=1000000000L*3*10; 5959 System.out.println("e>>>"+h); 6060 long i; 6161 i=System.currentTimeMillis(); 6262 System.out.println("time:"+i); 6363 6464 6565 float j=3.14f; 6666 float k,l; 6767 k=6.0f; 6868 l=4.9f; 6969 System.out.println(k-l); 7070 */ 7171 7272 } 7373 7474 }
练习
1//类型强制转换.从小到大,系统自动转换,从大到小,需要强制转换,强转可能会导致进度丢失,或是溢出 2 int age=20; 3 long age_l=(long)age; 4 System.out.println(age_l); 5 6 7 long d=5;//int type into long type 8 double e=5;//int type into double type 9 double f=3.14f;//float type into double type 10 11 double h=25.987; 12 int i=(int)h; 13 System.out.println(i);//print>>> 25