Java基础2-基本语法

复习

  1. jvm : 虚拟机 --> sandbox

  2. jre : jvm + 核心类库

  3. jdk : jre + 工具,javac java

  4. path: 操作系统搜索路径
    classpath: java搜索类的目录顺序

  5. java -classpath d:java;d:;.

  6. java程序制作过程
    1) 编写java源文件

    • 记事本notepad

    • editplus

    2) 编译源文件,生成class文件(字节码文件)
    3) 运行程序class文件

    • java 类名(在classpath下找)

    • java -classpath | -cp 类名

java关键字

  1. 数据类型

    • class //类
    • interface //接口
    • byte //字节 bit:位
    • short //短整型 2 byte
    • int //整型 4 bytes
    • long //长整型 8 bytes
    • float //浮点型
    • double //双精度浮点.
    • char //字符
    • boolean //布尔 false true
    • void //空
    • null //空,不存在
  2. 流程控制

    • if //如果
    • else //否则
    • switch //分支
    • case //情况.
    • default //默认,缺省
    • while //当.. 循环
    • do //
    • for //for 循环
    • break //中断
    • continue //继续
    • return //返回
  3. 访问权控制

    • private //私有的

    • proected //受保护的

    • public //公共的

  4. 类,函数修饰符

    • abstract //抽象

    • final //最终的

    • static //静态的

    • synchronized//同步

    • extends //扩展

    • implements //实现

    • new //新建

    • this //当前

    • super //超类,父类,

    • instanceof //是否是xxx实例

    • try //尝试

    • catch //捕获

    • finally //最终

    • throw //抛出

    • throws //定义类声明时,抛出异常

    • package //包

    • import //导入 export

    • native //本地的

    • transient //瞬时的,暂时的,临时的 persistent

    • asset //断言

  5. 标识符

    • 26个英文字母
    • 数字0-9
    • _ + $

规范

  1. java区分大小写

  2. 包名: aaa.bbb.ccc.ddd,全都小写

  3. 类: helloWorld --> HelloWorld,

  4. 变量和函数: convertStringToInt --> convertString2Int

  5. 常量大写: final String BRAND ="benz" ;

  6. Demo: 演示作用

  7. java的负数采用补码方式存储:取反 + 1 。

  8. java中基本数据类型:primitive

    • 默认整数是int类型,默认浮点数是double类型.
    • 升格自动转换。隐式转换。
    • 降格强制转换。显式转换。

注释

  1. 单行注释

     //xxxxxxxx
    
  2. 多行注释

     /*
      * 多行注释
      */
    
  3. 文档注释: 类顶端

     /**
      * 文档注释
      */
    

存储范围换算

  1. 2^10 = 1024 = 1K

  2. 2^20 = 1024K = 1M

  3. 2^30 = 1024M = 1G

  4. 2^40 = 1024G = 1T

进制

  1. 十进制:0~9,(123,456)

  2. 二进制:0,1

  3. 八进制:0~7,以0开头(076)

  4. 十六进制:0~F,以0x开头(0x12,0xfe)

  5. 定权重:位高权重

  6. 进制转换
    [十进制转二进制]
    使用除法
    2 | 13 ........ 1
    2 | 6 ......... 0
    2 | 3 ......... 1
    2 | 1 ......... 1
    ---> 1101

内存中数据存储形式

  1. byte: 2^8 = 256, -128~127

     3 + (-3) = 
     0000 0011 
         + 
     1000 0011 
     -------------
     = 1000 0110 = -6
    
  2. 负数的表现形式: 补码(正数取反+1)

     124 = 0111 1100 | -124 = 1000 0100
     125 = 0111 1101 | -125 = 1000 0011
     126 = 0111 1110 | -126 = 1000 0010
     127 = 0111 1111 | -127 = 1000 0001
                     | -128 = 1000 0000
    

判断结构

  1. if(exp){
    ....
    }

  2. if(exp1){
    ...
    }
    else{
    ...
    }

  3. if(exp1){
    ...
    }
    else if(exp2){
    ...
    }
    ...
    else{
    ...
    }

switch

支持的类型: byte short int char

循环语句

  1. while(exp){
    ....
    }

  2. //至少执行一次.
    do{
    ...
    }while(exp)

function

main() function

Modifier : public static void

  1. 一段独立程序

  2. 修饰符 返回值 函数名(参数类型1 参数名称1,参数类型2 参数名称2,...){
    函数body
    return ;//只有void类型的返回值才可以不写return.
    }

变量的使用范围

仅在所处的{}中有效,也称之为生命,下面程序编译时会报错:

/**
 *这是我的类,演示版。
 */
class CommentDemo{
	/*
	 * 程序的入口点。args是参数。
	 */
	public static void main(String[] args){
		{
		int age = 20;
	    }
        // 输出helloworld
		System.out.println("hello world" + age);
	}
}

强类型语言

javascript----弱类型语言,var

java-----强类型

int age = 20;

String name = "xxxx";

java中基本数据类型

  1. byte, 1个字节

  2. short, 2个字节

  3. int, 4个字节

  4. long, 8个字节

  5. float, 4个字节

  6. double, 8个字节

  7. boolean, 1个字节

  8. char, 两个字节

整数默认:int,小数默认:double

引用类型:

  1. class

  2. interface

  3. [], 数组

特殊字符

  1. ' ' //tab

  2. ' ' //return

  3. ' ' //new line

所有的byte、short、char 都将提升到int型

运算符

/**
 *这是我的类,演示版。
 */
class CalcDemo{
	/*
	 * 程序的入口点。args是参数。
	 */
	public static void main(String[] args){

		int i = +5;
		int j = -5;
		System.out.println("i = " + i + ",j = " + j);
		System.out.println("i = " + (+i) + ",j = " + (-j));

		// + - * /
        System.out.println(5 + 5);
		System.out.println(5 - 5);
		System.out.println(5 * 5);
		System.out.println("==="+ ((float)5 / 3 * 3));


		//取模
		System.out.println(5 % 3); //求余

		// ++ --
		int a = 5, b = 5;
		System.out.println("a = "+(a++)); //
		System.out.println("b = "+ (++b)); //

		// + 操作符重载
		System.out.println("hello"+ "world"); //
		System.out.println("5 + 5 = "+ (5 + 5)); //

		//赋值 = += -= *= /= %=
		int a1 = 5;
		System.out.println("a1 += "+ (a1+=3)); //		
       
		short s = 2;
		s = (short)(s+2);
		s += 2;
		System.out.println("s = "+ s); //

		//比较运算符 == <> !=
		boolean res = 5<= 5;
		System.out.println(5<=5);
		System.out.println(res);
		System.out.println(5!=5);

		Object o = "hello" ;
		//instanceof 判断对象是否是指定的类型,后面跟的是引用类型
		System.out.println(o instanceof String);
		System.out.println(o instanceof Object);
		//System.out.println(o instanceof int);//报错
		System.out.println(o instanceof Class);

		//逻辑运算符
		// && || 存在短路操作
		int c1 = 8, c2 = 3;
		if(c1>5 && c2>5){
			System.out.println("Ok");
		}
		else{
			System.out.println("no ok");
		}

		if(c1>5 || c2>5){
			System.out.println("Ok");
		}
		else{
			System.out.println("no ok");
		}

		// & | ! ^ 不存在短路
		if((c1 > 5) & (c2>5)){
			System.out.println("Ok");
		}
		else{
			System.out.println("no ok");
		}

		
		//System.out.println(5 && 6);//报错
		System.out.println(true && false);

		// 5 0000 0101
		// 6 0000 0110
		// ------------
		//   0000 0100 = 4
		System.out.println(5 & 6);//结果为4,按位与
		System.out.println(5 | 6);//结果为7,按位或

		//System.out.println(!6);//报错
		//对boolean类型取反
		System.out.println(!true);
		boolean b0 = 3>4;
		System.out.println(!b0);

		//按位取反 ~
		// 0000 0000 0000 0000 0000 0000 0000 0101
		// 1111 1111 1111 1111 1111 1111 1111 1010
		// 0000 0000 0000 0000 0000 0000 0000 0110
		System.out.println(~5);

		//异或 ^,按位异或,相同为假,不同为真
		// 0...0 0101 = 5
		//^0...0 1100 = 12
		//------
		// 0...0 1001 = 9

		System.out.println(5^12);

		//移位运算
		// << 有符号左移
		// >> 有符号右移
		// 0000 1010 = 10
		// 0010 10 00 = 40
		System.out.println("10 << 2 = " + (10<<2));
		System.out.println("6 << 2 = " + (6<<2));

		// 0000 0110 = 6
		// 0000 0001 = 1
		System.out.println("6 >> 2 = " + (6>>2));

		// -6        1111 1010
		// -6 << 2   1110 1000 = -24
		//           0001 1000
		System.out.println("-6 << 2 = " + (-6<<2));

		// -6        1111 1010
		// -6 >> 2   1111 1110 = -2
		//           0000 0010
		System.out.println("-6 >> 2 = " + (-6>>2));

		// >>> 无符号右移,空出的位用0填充
		// -6       1111 1010
		// -6 >>> 2 0011 1110 =
		System.out.println("-6 >>> 2 = " + (-6>>>2));
		System.out.println("-1 >> 3 = " + (-1>>3));
		System.out.println("-1 >>> 3 = " + (-1>>>3));

		// 三元运算符 ? : ;
		int i1 = 10;
		if(i1 < 5){
			System.out.println("<5");
		}
		else{
			System.out.println(">=5");
		}
		System.out.println(i1 < 5 ? "<5" : ">=5");
		int x = 5, y = 6, z = 7;
		System.out.println(x>y? x : y);

		//三元运算符的嵌套
		System.out.println(x>y? (x>z? x : z) : (y>z? y:z));
	}
}
流控制
/**
 * 流程控制
 */
class FlowControl
{
	public static void main(String[] args){
		//1. if
		int a = 10;
		if(a>5){
			System.out.println("a is big");
		}

		//2. if - else,非此即彼
		if(a<5){
			System.out.println("a is small");
		}
		else{
			System.out.println("a is big");
		}

		System.out.println("over");

		//3. if - else if - else
		// month 1-12
		int m = 100;
		if(m<=3 && m >=1){
			System.out.println(m+"是春季");
		}
		else if (4<=m && m<=6){
			System.out.println(m+"是夏季");
		}
		else if (7<=m && m<= 9){
			System.out.println(m+"是秋季");
		}
		else if(m>=10 && m<=12){
			System.out.println(m+"是冬季");
		}
		else{
			System.out.println(m+"是无效的月份");
		}

		//4.另一种写法
		if(m<=0 || m>=13){
			System.out.println(m+"是无效的月份");
		}
		else if(m<=3){
			System.out.println(m+"是春季");
		}
		else if(m<=6){
			System.out.println(m+"是夏季");
		}
		else if (m<=9){
			System.out.println(m+"是秋季");
		}
		else{
			System.out.println(m+"是冬季");
		}

		//5. switch语句
		int b = 2;
		switch (b){
		case 1:
		case 2:
		case 3:
			System.out.println("small");
			break;
		case 4:
			System.out.println("midlle");
			//break;
		case 5:
			System.out.println("big");
			break;
		default:
			System.out.println("??");
			break;
		}

		//6.利用switch判断月份
		int n = 11;
		switch (n){
			case 1:
			case 2:
			case 3:
				System.out.println(n+"是春季");
				break;
			case 4:
			case 5:
			case 6:
				System.out.println(n+"是夏季");
				break;
			case 7:
			case 8:
			case 9:
				System.out.println(n+"是秋季");
				break;
			case 10:
			case 11:
			case 12:
				System.out.println(n+"是冬季");
				break;
			default:
				System.out.println(n+"是非法月份");
				break;
		}

		//7. 简单写法
		int c = 12;
		if (c<1 || c>12){
			System.out.println("非法月份");
			return;
		}
		int season = (c-1)/3;
		switch(season){
			case 0:
				System.out.println("春");
				break;
			case 1:
				System.out.println("夏");
				break;
			case 2:
				System.out.println("秋");
				break;
			default:
				System.out.println("冬");
				break;
		}
        
		//8. while循环
		int i = 0;
		while(true){
			System.out.println(i);
			i ++ ;
			if(i>=10){
				break;
			}
		}
		System.out.println("--------");
        
		//9. do...while循环
		i = 0;
		do{
			System.out.println(i);
			i ++ ;
			if(i>=10){
				break;
			}
		}while(i>1);

		//10.打印直角三角形
		int k = 10;
		int k1 = 1;

		while(k1<=10){
			//输出整个一行
			int k2 = 1;
			while(k2<=k1){
				System.out.print("*");
				k2++;
			}
			System.out.println();
			k1++;
		}

		//11. 99表格
		int line = 9;
		int row = 1;
		//行循环
		while(row<=9){
			//列循环
			int col = 1;
			while(col <=row){
				System.out.print(col + "x" + row + "=" + (col * row)+"	");
				col ++;
			}
			row ++;
			System.out.println();
		}

		//12. for 循环
		for(i = 0;i<=10;i++){
			System.out.println(i);
		}

		//13. 前n项和
		int sum = 0;
		for(i = 0;i<=100;i++){
			sum = sum +i;
		}
		System.out.println(sum);
	}
}

[思考]

  1. 负数的表现形式,为什么如此设计?

  2. -128是在内存中是如何存储的?推算过程?

  3. 如果字节表示为11111111,则其真实数据是多少?

  4. 正负数是否都支持补码的方式?

  5. 正数是否是负数的减一取反?

  6. 空心三角形

  7. 倒三角空心

  8. [定义函数]
    add // +
    subtract // -
    multiple // *
    divide // /
    支持float,doule,long,int。//函数重载.

原文地址:https://www.cnblogs.com/SweetZxl/p/9451659.html