Java基本语法

1. Java基本规范

需要遵守的基本规范内容有:

  • 程序结构清晰
  • 程序逻辑简单易懂,代码简洁
  • 尽量使用标准库函数或公共函数实现功能逻辑
  • 避免声明全局变量,尽量使用局部变量
  • 规避二义性,使用括号不易产生二义性

2. Java命名规范

  • 项目 项目名通常使用小写
  • 包 包名全部使用小写。包名通常有若干个标识符组成,标识符之间用点(.)隔开,其中第一个标识符往往表示域名。例如:com.sun.eng,域名是com
  • 类和接口 类名和接口名采用大小写混合的标识符,首字母是大写
  • 方法 方法名采用大小写混合的表示符,首字母是小写
  • 变量名 变量名也采用大小写混合的标识符,首字母是小写 例如:double
  • 实例变量 实例变量从规则上来说等同于变量,一般会选用易于读懂,表示用途的单词,尽量避免单个字符的变量名
  • 符号常量 通常使用大写的标识符命名

注意运用驼峰规则,大小写混合即表示每个单词的首字母是大写

3. Java标识符

类名,变量名及方法名都被称为标识符,命名规范如下:

  • 所有表示符都以字母(A-Z或a-z),美元符($),或者下划线(_)开始
  • 首字母之后可以是字母,美元符,下划线或数字的任何字符组合
  • 关键字不能用作标识符
  • 标识符大小写敏感

合法的标识符如:one, $money , _value等
不合法的标识符 :123a , *one等

4.Java关键字

下列关键字不能用于常量,变量和标识符的名称

类别关键字说明
访问控制private私有的
protected受保护的
public公共的
default 默认
类、方法和变量修饰符abstract声明抽象
class
extends扩充,继承
final最终值,不可改变的
implements实现(接口)
interface接口
native本地,原生方法(非 Java 实现)
new新,创建
static静态
strictfp严格,精准
synchronized线程,同步
transient短暂
volatile易失
程序控制语句break跳出循环
case定义一个值以供 switch 选择
continue继续
default默认
do运行
else否则
for循环
if如果
instanceof实例
return返回
switch根据值选择执行
while循环
错误处理assert断言表达式是否为真
catch捕捉异常
finally有没有异常都执行
throw抛出一个异常对象
throws声明一个异常可能被抛出
try捕获异常
包相关import引入
package
基本类型boolean布尔型
byte字节型
char字符型
double双精度浮点
float单精度浮点
int整型
long长整型
short短整型
变量引用super父类,超类
this本类
void无返回值
保留关键字goto是关键字,但不能使用
const是关键字,但不能使用
null

5. 基本数据类型和运算符号

java提供了八种基本类型

  • byte:
    数据类型是一字节,8位,有符号的以二进制补码表示的整数
    最小值是 -128(-2^7);
    最大值是 127(2^7-1);
    默认值是 0;

public class ByteTest {
	public static void main(String[] args) {
		byte a = (byte) -128;
		System.out.println(a);  //-128
		byte b = (byte) 127; 
		System.out.println(b);  //127
		byte c = (byte) -129;
		System.out.println(c);  //127
		byte d = (byte) 128;
		System.out.println(d);  //-128
	}
}

  • short:
    short数据类型是两字节,16位,有符号的以二进制补码表示的整数
    最小值是 -32768(-2^15);
    最大值是 32767(2^15 - 1);
    默认值是 0;
  • int:
    int 数据类型是32位、有符号的以二进制补码表示的整数;
    最小值是 -2,147,483,648(-2^31);
    最大值是 2,147,483,647(2^31 - 1);
    一般地整型变量默认为 int 类型;
    默认值是 0 ;

public class OperatorTest {
	public static void main(String[] args) {
		int a = 10;
		int b = 3;
		int c = a+b;  //13
		int d = a-b;  //7
		int e = a/b;  //3
		int f = a%b;  //1
		System.out.println(c + "," + d + "," + e + "," +f);
		
		System.out.println(a>>1); //5, 右移 除以2
		System.out.println(a<<1); //20  左移 乘以2
		
		System.out.println((5>2) && ((2<3)||(!false)));  //true
	}
}

  • long:
    long 数据类型是 64 位、有符号的以二进制补码表示的整数;
    最小值是 -9,223,372,036,854,775,808(-2^63);
    最大值是 9,223,372,036,854,775,807(2^63 -1);
    这种类型主要使用在需要比较大整数的系统上;
    默认值是 0L;
public class IntegerTest {
	public static void main(String[] args) {
		short a1 = 32767;
		System.out.println(a1);
		//short a2 = 32768;  error 越界		
		
		int b1 = 2147483647;
		System.out.println(b1);
		//int b2 = 2147483648; error 越界		
		
		long c1 = 1000000000000L;
		System.out.println(c1);
		
		long c2 = 2147483647;  //隐式做了从int变成long的操作
		System.out.println(c2);
		
		long c3 = 2147483648L; //去掉L将报错
		System.out.println(c3);
	}
}
  • float:
    float 数据类型是单精度、32位、符合IEEE 754标准的浮点数;
    float 在储存大型浮点数组的时候可节省内存空间;
    默认值是 0.0f;
  • double:
    double 数据类型是双精度、64 位、符合IEEE 754标准的浮点数;
    浮点数的默认类型为double类型;
    double类型同样不能表示精确的值,如货币;
    默认值是 0.0d;
public class FloatingTest {
	public static void main(String[] args) {
		float f1 = 1.23f;
		// float f2 = 1.23;  error, float赋值必须带f
		double d1 = 4.56d;
		double d2 = 4.56;  //double 可以省略末尾d
		
		System.out.println(f1); //1.23
		System.out.println((double)f1); //转换到double, 输出1.2300000190734863
		System.out.println(d1); //4.56
		System.out.println((float)d2); //4.56
		
		System.out.println(f1==1.229999999f); //true
		System.out.println(f1-1.229999999f); //0.0
		System.out.println(d2==4.559999999999999999d); //true
		System.out.println(d2-4.559999999999999999d); //0.0			
	}
}

  • boolean:
    boolean数据类型表示一位的信息;
    只有两个取值:true 和 false;
    默认值是 false;

public class BooleanTest 
{
	public static void main(String[] args) 
	{
		boolean a = true; //not TRUE  True
		boolean b = 5<3;  //false;
		a = false;  
		// a = 1;  错误,只能给boolean变量赋true或者false 
	}
}

  • char:
    char类型是一个单一的 16 位 Unicode 字符;
    最小值是 \u0000(即为0);
    最大值是 \uffff(即为65,535);
    char 数据类型可以储存任何字符;

public class CharTest {
	public static void main(String[] args) {
		char a = 'a';
		char b = 97;  //根据ascii码转化为a
		char c = '我';
		char d = '\u4e00'; //“一”字  \u4e00--\u9fa5  两万多汉字
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		System.out.println(d);
	}
}

6.选择和循环结构

Java中有三种主要的循环结构:
while循环

while(布尔表达式){
  //循环内容
}

do…while循环

do{
	//代码语句
}while(布尔表达式)

while循环与do…while循环的区别是,后者至少会执行一次,而while循环如不满足条件则不能进入循环

for循环

for(初始化;布尔表达式;更新){
	//代码语句
}

Java 增强 for 循环

Java5 引入了一种主要用于数组的增强型 for 循环。
Java 增强 for 循环语法格式如下:
(声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。)

for(声明语句 : 表达式)
{
   //代码句子
}

public class Test {
   public static void main(String args[]){
      int [] numbers = {10, 20, 30, 40, 50};
 
      for(int x : numbers ){
         System.out.print( x );
         System.out.print(",");
      }
      System.out.print("\n");
      String [] names ={"James", "Larry", "Tom", "Lacy"};
      for( String name : names ) {
         System.out.print( name );
         System.out.print(",");
      }
   }
}

输出结果:
10,20,30,40,50,
James,Larry,Tom,Lacy,

break关键字

break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块。
break 跳出最里层的循环,并且继续执行该循环下面的语句。

public class Test {
   public static void main(String args[]) {
      int [] numbers = {10, 20, 30, 40, 50};
 
      for(int x : numbers ) {
         // x 等于 30 时跳出循环
         if( x == 30 ) {
            break;
         }
         System.out.print( x );
         System.out.print("\n");
      }
   }
}

输出结果:
10
20

continue关键字

continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。
在 for 循环中,continue 语句使程序立即跳转到更新语句。
在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。

public class Test {
   public static void main(String args[]) {
      int [] numbers = {10, 20, 30, 40, 50};
 
      for(int x : numbers ) {
         if( x == 30 ) {
        continue;
         }
         System.out.print( x );
         System.out.print("\n");
      }
   }
}

输出结果:
10
20
40
50

7.条件语句

  • if…else语句
  • if…else if…else语句
  • 嵌套的if…else语句

8. 自定义函数

函数必须放在类的范围内;
函数可以调用其他的函数;


public class FactorialTest {

	public static void main(String[] args) {
		int a = 5;
		int b = factorialCalculation(a);
		System.out.println("The factorial of " + a + " is " + b);

	}

	public static int factorialCalculation(int m) {
		if (m > 1) {
			return m * factorialCalculation(m - 1);
		} else {
			return 1;
		}

	}

}

函数重载

同一个类中,函数名称可以相同,即重载(overload),但是函数参数的个数或者类型必须有所不同;
不能以返回值来区分同名的函数;


public class OverloadTest {

	public static void main(String[] args) {
		int a=1,b=2;
		System.out.println(add(1,2));
		System.out.println(add(1.5,2.5));
	}

	public static int add(int m, int n) 
	{
		return m + n;
	}
	public static double add(double m, double n) //和第9行行数重载overload
	{
		return m + n;
	}
	/*
	 * 以下函数非法,和第9行的函数名相同,形参相同
	public static double add(int m, int n) 
	{
		return (double) (m + n);
	}
	*/
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值