随笔 - 597  文章 - 4  评论 - 445  阅读 - 424万

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

复制代码
 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'--65
17         //byte b3=b1+b2;报错    
18         //byte b3=(byte)(b1+b2);ok
19         byte b4=3+4;
20         //byte short char ->int->long
21     
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+10
17         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 2
24         //d 3+3+20
25         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 flase
17         System.out.println(a<b & a<c);//true true ture
18         System.out.println(a<b & a>c);//true false false
19         System.out.println(a>b & a<c);//false true flase
20         System.out.println("------------");
21         System.out.println(a>b | a>c);//false false false
22         System.out.println(a<b | a<c);//true true true
23         System.out.println(a>b | a<c);//false true true
24         System.out.println(a<b | a>c);//true false true
25         System.out.println("----------");
26         //相同为false,不同为true;逻辑异或
27         System.out.println(a>b ^ a>c);//false false false
28         System.out.println(a<b ^ a<c);//true true ture
29         System.out.println(a>b ^ a<c);//false true true
30         System.out.println(a<b ^ a>c);//true false true
31         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 }
复制代码

 

posted on   别先生  阅读(986)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示