02_Java语法

1.标识符的命名规则

(1)标识符必须以字符,下换线_,美元符$开头。

(2)标识符其他部分可以是字母,下划线“_”,美元符“$”和数字的任意组合。

(3)不可以是Java的关键字或者保留名。

(4)标识符的命名一般要有意义(见名知意)。

(5)Java标识符大小写敏感,且长度无限制(建议不要超过15个字符)。

2.常量与变量

(1)常量:也称为数据常量,是程序运行中其值不能发生改变的量。

(2)变量:其实是内存中的一块存储空间,使用变量名访问这块空间;每个变量使用前必须先声明,然后赋值,才可以使用,变量中存储的数据就是常量。

3.基本数据类型

(1)数值型:整数类型(byte  1、short  2、int  4、long  8)。

 long L = 123456L;

    浮点类型:(float  4、double  8)。

float f = 1.2f

(2)字符型:(char  2)。

(3)布尔型:(boolean)  只有两个值:true 或者 false。

   如果使用boolean声明一个基本类型的变量时,那么该变量占4个字节。boolean f =false;

   如果使用boolean声明一个数组类型的时候,那么该每个数组的元素占1个字节。Boolean []  arr = {false,true,true};

4.基本数据类型的转换

(1)自动类型转换:数据类型取值范围小的转为取值范围大的

     byte ---> short--->int--->long--->

     float --->double

     char---> int

     byte,short,char三者在计算时会转换成int类型。

     int整型常量和字符常量在合理范围内可以直接赋值给 byteshortintchar 。

(2)强制类型转换:数据类型取值范围大的转为取值范围小的. 需要加强制转换符,也就是(要转换的数据类型)。但有可能造成精度降低或数据溢出,使用时要小心。

long l = 100L;
int i = (int)l;

5.i++   ++i  有什么区别?

(1) i++  ++i的共同点 都是变量自增1---->  i=i+1。

(2)如果i++,++i是一条单独的语句,两者没有任何区别。

(3)如果i++,++i不是一条单独的语句,有区别:

     i++  先运算后增1

     ++i  先增1后运算 

 6.代码例子

 1 /**
 2  *  功能:用最简单的方式获取两个变量中较大的数,并输出 
 3  * @author jliu.l
 4  * @2020年7月1日
 5  * 
 6  */
 7 public class Demo06 {
 8     public static void main(String[] args) {
 9         Scanner sc = new Scanner(System.in);
10         //准备数据
11         System.out.print("请输入数据a:");
12         int a = sc.nextInt();
13         System.out.print("请输入数据b:");
14         int b = sc.nextInt();
15         
16         //处理数据
17         if(a > b) {
18             System.out.println(a);
19         }else if(a < b) {
20             System.out.println(b);
21         }else {
22             System.out.println("你输入的两个数字相等!");
23         }
24     }
25 }
 1 /**
 2  * 功能:计算一个人的心脏每分钟跳动70次,活了70年,求一共跳动了多少次?
 3  * @author jliu.l
 4  * @2020年7月1日
 5  * 
 6  */
 7 public class Demo05 {
 8     public static void main(String[] args) {
 9         // TODO 自动生成的方法存根
10         
11         long l = 70L*24*60*365*70;
12         System.out.println("一共跳动了f:"+l);
13     }
14 }
 1 /**
 2   * 功能:求一个四位数的每个位数上的数并输出
 3  * @author jliu.l
 4  * @2020年7月1日
 5  * 
 6  */
 7 public class Demo04 {
 8     public static void main(String[] args) {
 9         
10         //准备数据
11         Scanner sc = new Scanner(System.in);
12         System.out.print("请输入一个四位数:");
13         int num = sc.nextInt();
14         //处理数据
15         int g = num % 10;
16         int s = num / 10 %10;
17         int b = num /100 %10;
18         int q = num /1000 %10;
19         
20         //输出数据
21         System.out.println(num+"的"+"千位:"+q+"	"+"百位:"+b+"	"+"十位:"+s+"	"+"个位:"+g);
22     }
23 }
 1 /**
 2  * @author jliu.l
 3  * @2020年7月1日
 4  * 
 5  */
 6 public class Demo03 {
 7     public static void main(String[] args) {
 8         // TODO 自动生成的方法存根
 9         int num = 5;
10         System.out.println(num+(++num));        //11
11         System.out.println(num);    //6
12     }
13 }
 1 /**
 2   * 模拟用户登陆
 3  * @author jliu.l
 4  * @2020年7月1日
 5  * 
 6  */
 7 public class Demo02 {
 8     public static void main(String[] args) {
 9         
10         Scanner scanner = new Scanner(System.in);
11         
12         System.out.print("请输入用户名:");
13         String username = scanner.nextLine();
14         
15         System.out.print("请输入密码:");
16         String pwd = scanner.nextLine();
17         
18         if("admin".equals(username) && "123456".equals(pwd)) {
19             System.out.println("登陆成功!");
20         }else {
21             System.out.println("登录失败!");
22         }    
23     }
24 }
原文地址:https://www.cnblogs.com/jliu-l/p/13223082.html