Java的从浅至深绕坑而行的学习

来源:互联网 发布:Linux下配置gnu 编辑:程序博客网 时间:2024/05/21 17:11
 1 package day02; 2 /** 3  * 1:java初学习,避免面试时一些HR挖的坑。 4  * @author biexiansheng 5  * 6  */ 7 public class Test02 { 8  9     public static void main(String[] args) {10         // ctrl+shift+f自動排版11         int a=120;12         byte b=9;13         b=(byte)(a+b);//需要强制类型转换14         System.out.println("b="+b);15         16         System.out.println('a'+1);17         System.out.println('A'+1);18         System.out.println("hello"+'a'+1);19         //任何数据类型加字符串等于字符串20         System.out.println('a'+1+"hello");21         System.out.println("hello"+1+'a');22         23     }24 25 }

运行结果:

b=-127
98
66
helloa1
98hello
hello1a

 1 package day02; 2  3 /** 4  * 1:java初学习,避免面试时一些HR挖的坑。 5  *  6  * @author biexiansheng 7  * 8  */ 9 public class Test02 {10 11     public static void main(String[] args) {12         // ctrl+shift+f自動排版13         //b1,b2时两个变量,变量里面存储的值都是变化的,所以在程序运行中jvm是无法判断里面具体的值14         //byte类型的变量在进行运算的时候,会自动转换为int类型15         byte b1=3;16         byte b2=4;                    //'0'--48 'a'--97 'A'--6517         //byte b3=b1+b2;报错    18         //byte b3=(byte)(b1+b2);ok19         byte b4=3+4;20         //byte short char ->int->long21     22     }23 24 }

 

 1 package day02; 2  3 /** 4  * 1:java初学习,避免面试时一些HR挖的坑。 5  *  6  * @author biexiansheng 7  * 8  */ 9 public class Test02 {10 11     public static void main(String[] args) {12         // ctrl+shift+f自動排版13         System.out.println(10/3);14         System.out.println(10/3.0);15         System.out.println(10*3.0);16         System.out.println("3+4="+3+4);17         //+号代表第一加号,第二连接,第三代表正数。18         int a=3;19         System.out.println("a++="+(a++));//先输出再++20         System.out.println("a="+a);//输出上面的++后面的值21         System.out.println("++a="+(++a));22         int b=3;23         int c;24         c=b++;//后++,先使用再++25         //c=++b;先++,先++后使用26         System.out.println("b="+b);27         System.out.println("c="+c);28         29         30     }31 32 }

 

 1 package day02; 2  3 /** 4  * 1:java初学习,避免面试时一些HR挖的坑。 5  *  6  * @author biexiansheng 7  * 8  */ 9 public class Test02 {10 11     public static void main(String[] args) {12         // ctrl+shift+f自動排版13         int a = 4;14         int b =(a++)+(++a)+(a+10);15         //a 5 6  16         //b 4+6+6+1017         System.out.println("a="+a);18         System.out.println("b="+b);19         20         21         int c = 4;22         int d = (--c)+(c--)+(c*10);23         //c 3 224         //d 3+3+2025         System.out.println("c="+c);26         System.out.println("d="+d);27         28         29     }30 31 }

答案:

a=6
b=26
c=2
d=26

 1 package day02; 2  3 /** 4  * 1:java初学习,避免面试时一些HR挖的坑。 5  *  6  * @author biexiansheng 7  * 8  */ 9 public class Test02 {10 11     public static void main(String[] args) {12         // ctrl+shift+f自動排版13         byte a=3;14         a++;15         System.out.println("a="+a);//先使用再++16         //System.out.println("a++="+(a++));17         a=(byte)(a+1);//byte类型自动转换为int类型的,所以需要强制转换一下。18         System.out.println("a="+a);19         20         short s=1;21         s=(short)(s+1);22         short s1=2;//这是坑,面试的坑23         s1+=1;//<==>s1=(short)(s1+1);24         25     }26 27 }

 

 1 package day02; 2  3 /** 4  * 1:java初学习,避免面试时一些HR挖的坑。 5  *  6  * @author biexiansheng 7  * 8  */ 9 public class Test02 {10 11     public static void main(String[] args) {12         // ctrl+shift+f自動排版13         int a=10;14         int b=20;15         int c=30;16         System.out.println(a>b & a>c);//false false flase17         System.out.println(a<b & a<c);//true true ture18         System.out.println(a<b & a>c);//true false false19         System.out.println(a>b & a<c);//false true flase20         System.out.println("------------");21         System.out.println(a>b | a>c);//false false false22         System.out.println(a<b | a<c);//true true true23         System.out.println(a>b | a<c);//false true true24         System.out.println(a<b | a>c);//true false true25         System.out.println("----------");26         //相同为false,不同为true;逻辑异或27         System.out.println(a>b ^ a>c);//false false false28         System.out.println(a<b ^ a<c);//true true ture29         System.out.println(a>b ^ a<c);//false true true30         System.out.println(a<b ^ a>c);//true false true31         System.out.println("------------");32         System.out.println(!true);33         System.out.println(!!true);34         35         36     }37 38 }

 

 1 package day02; 2  3 /** 4  * 1:java初学习,避免面试时一些HR挖的坑。 5  *  6  * @author biexiansheng 7  * 8  */ 9 public class Test02 {10 11     public static void main(String[] args) {12         // ctrl+shift+f自動排版13         //短路与&&,前错后不执行14         //短路或||,前对后不执行15         int x=3;16         int y=4;17         //System.out.println((x++)==3 && (++y==4));//前对后执行18         //System.out.println((x++)==3 & (++y==4));//前对后执行19         //System.out.println((++x)==3 && (++y==4));//前错后不执行20         System.out.println((++x)==3 & (++y==4));//前错后不执行21         System.out.println("x="+x);22         System.out.println("y="+y);23         24     }25 26 }

 

 1 package day02; 2 import java.util.Scanner; 3 /** 4  * 1:java初学习,避免面试时一些HR挖的坑。 5  *  6  * @author biexiansheng 7  * 8  */ 9 public class Test02 {10 11     public static void main(String[] args) {12         Scanner s=new Scanner(System.in);13         System.out.println("请输入三个数值a:");14         int a=s.nextInt();15         System.out.println("请输入三个数值b:");16         int b=s.nextInt();17         if(a==b){18             System.out.println("a和b相等哟!");19         }else{20             System.out.println("a和b的值不相等哟!");21         }22         System.out.println("--------------------");23         int num=a<b? a:b;24         System.out.println("a,b两个数之间的最小值:"+num);25         System.out.println("--------------------");26         System.out.println("请输入三个数值c:");27         int c=s.nextInt();28         /*int min=a<b? a:b;29         int min2=min<c? min:c;30         System.out.println("a,b,c三个数的最小值:"+min2);*/31         int min=a<b? (a<c? a:c):(b<c? b:c);32         System.out.println("a,b,c三个数的最小值:"+min);33         34     }35 36 }

 

0 0
原创粉丝点击