JAVA入门基础--数据类型
数学中,没有办法用十进制来精确表达1/3.
同样的,计算机中,没有办法用二进制表示0.1
比如:python中,java中 都是一样的。
>>> a=6.0 >>> b=4.9 >>> a-b 1.0999999999999996 >>> x=3.0 >>> y=2.9 >>> x-y 0.10000000000000009 >>> (x-y)>0.1 True >>> (x-y)==0.1 False >>>
在上述代码中: a-b 的值会无限接近于1.1。 x-y 的值会无限接近0.1。
1 package day02; 2 3 import java.util.Scanner; 4 5 public class inp_demo { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 Scanner scanner=new Scanner(System.in); 10 String name=scanner.next(); 11 System.out.println(name); 12 13 } 14 15 }
数据类型的练习:
1 package day02; 2 3 import java.util.Scanner; 4 5 public class inp_demo { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 // Scanner scanner=new Scanner(System.in); 10 // String name=scanner.next(); 11 // System.out.println(name); 12 int a=250; 13 //int b=100000000000; 14 System.out.println(5/2); 15 System.out.println(2/5); 16 int c=2147483647; 17 System.out.println(c+1); 18 19 //long d=10000000000; 20 long e=10000000000L; 21 long f=1000000000*2*10L; 22 System.out.println("f:"+f); 23 long g=1000000000*3*10L; 24 System.out.println("g>>>"+g); 25 long h=1000000000L*3*10; 26 System.out.println("e>>>"+h); 27 long i; 28 i=System.currentTimeMillis(); 29 System.out.println("time:"+i); 30 31 32 float j=3.14f; 33 float k,l; 34 k=6.0f; 35 l=4.9f; 36 System.out.println(k-l); 37 38 } 39 40 }
结果:
2 0 -2147483648 f:20000000000 g>>>-12949672960 e>>>30000000000 time:1535892324164 1.0999999
1 package day02; 2 3 import java.util.Scanner; 4 5 public class inp_demo { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 10 // //布尔类型,占一个字节。 11 // boolean b1=true; 12 // boolean b2=false; 13 //字符类型,占有两个字节 java的字符编码表是unicode 14 char o3='m'; 15 char o4=' '; 16 char o5=65515;//0~65535 17 System.out.println(o5); 18 char o='中'; 19 System.out.println(o); 20 21 char o1='\u4e2d'; 22 System.out.println(o1); 23 24 char o2=65; 25 System.out.println(o2); 26 27 System.out.println("(2+2):"+(2+2)); 28 System.out.println('2'+'2'); 29 30 System.out.println("char2》\t》》\n》"+'2'); 31 32 33 34 35 36 37 38 39 40 41 // Scanner scanner=new Scanner(System.in); 42 // String name=scanner.next(); 43 // System.out.println(name); 44 /* 45 int a=250; 46 //int b=100000000000; 47 System.out.println(5/2); 48 System.out.println(2/5); 49 int c=2147483647; 50 System.out.println(c+1); 51 52 //long d=10000000000; 53 long e=10000000000L; 54 long f=1000000000*2*10L; 55 System.out.println("f:"+f); 56 long g=1000000000*3*10L; 57 System.out.println("g>>>"+g); 58 long h=1000000000L*3*10; 59 System.out.println("e>>>"+h); 60 long i; 61 i=System.currentTimeMillis(); 62 System.out.println("time:"+i); 63 64 65 float j=3.14f; 66 float k,l; 67 k=6.0f; 68 l=4.9f; 69 System.out.println(k-l); 70 */ 71 72 } 73 74 }
//类型强制转换.从小到大,系统自动转换,从大到小,需要强制转换,强转可能会导致进度丢失,或是溢出 int age=20; long age_l=(long)age; System.out.println(age_l); long d=5;//int type into long type double e=5;//int type into double type double f=3.14f;//float type into double type double h=25.987; int i=(int)h; System.out.println(i);//print>>> 25