第一部分:Java基础编程-Java基本语法

本文详细介绍了Java编程中的关键字、标识符和变量。关键字是Java语言预定义的,如`int`、`double`等,所有字母小写。标识符用于命名类、变量等,遵循特定的命名规则。变量是存储数据的容器,包括基本数据类型如`byte`、`char`、`boolean`等,并讲解了变量间的运算规则和类型提升。同时,文章还提到了强制类型转换可能导致的精度损失。
摘要由CSDN通过智能技术生成

1.关键字(keyword)和保留字(reserved word)

        关键字定义:被Java语言赋予了特殊含义,用作专门用途的字符串(单词)

        关键字特点:关键字中所有字母都是小写

        保留字:目前还没赋予特定含义,以后有可能会赋予

2.标识符(identifier)

        Java对各种变量、方法、类命名时使用的字符串叫标识符

        凡是自己起名字的都叫标识符!

        定义要求:

/*
标识符的使用:
1.凡是自己可以命名的都叫做标识符
	例如:类名、变量名、包名...
2.命名规则:如果不遵守就编译不通过
	*由26个英文字母大小写,0-9,_ $ 组成
	*不能由数字开头
	*Java中严格区分大小写,长度没有限制
	*不能使关键字,但是可以包含关键字
	*不能含有空格

命名的规范:如果不遵守,编译还是可以通过的,建议大家遵守
1.包名:多个单词所有的字母都是小写
2.类名、接口名:所有单词首字母单词大写,大驼峰
3.变量名、方法名:多单词组成时,就第一个单词小写,
	第二个单词开始就首字母大写,例如xxxYyyOoo,小驼峰
4.常量名:所有字母都大写。多单词时,用下划线连接,例如SS_OO_PPP_JJ

在起名字时要起到见名知意
注意:因为Java采用的是unicode字符集,所以标识符也可以用中文,但是不建议使用。
*/
class IdentifierTest{
	public static void main(String[] args){	
		int myNumber = 100;
		System.out.println(myNumber);
	}
}

3.变量

        变量 = 变量类型+变量名+储存的值       Java是强类型变量:变量的类型必须确认!

/*
变量的使用
1.
	Java定义变量的格式:变量类型 变量名(小驼峰) = 变量值;

2.声明:
		变量必须先声明再使用,也就是说使用之前要定义+赋值好。

	作用域:变量都定义在作用域内,只有在作用域内才是有效的,
			出了作用域就失效了。
			在同一个作用域内不能定义同名的变量。
*/
class VariableTest {
	public static void main(String[] args) {
		//变量的定义
		int myAge = 12;
		//变量的使用
		System.out.println(myAge);
		int myNumber;
		myNumber = 10086;
		System.out.println(myNumber);
	}
}

        3.1变量类型

/*
Java的数据类型

一、变量按照数据类型来划分:
	①基本数据类型(8种)
		数值型:
			整数类型:byte(1个字节 = 8bite) short int long
			浮点类型: float double
		字符型:char
		布尔型:boolean

	②引用数据类型(3种)
		类(class) - 字符串是一个类
		接口(interface)
		数组(array)


二、变量在类中声明的位置
	
		成员变量

		局部变量
*/



class  VariableTest{
	public static void main(String[] args) {
		//1.整型:byte(1字节= 8bite) short(2字节) int(4字节) long(8字节)
		//①byte范围:-128 ~ 127
		byte b1 = 12;
		byte b2 = -128;
		//b2 = 128;//超出上述范围,编译不通过
		System.out.println(b1);
		//System.out.println(b2);
		//② 声明:long型变量必须以l或L来结尾!
		short s1 = 22;
		int i2 = 56556;
		long l6 = 5445464464L;
		//③常用int来定义整型变量。

		/*2.浮点类型: float(单精度,4字节,尾数精确到七位有效数字) 
		double(双精度,8字节,精度高),通俗来书就是带小数点的
		*/
		//①表示带小数点的数值
		//②float表示数值的范围比long还大
		//定义float类型变量时,必须以f或者F结尾
		double f1 = 3.14F;
		System.out.println(f1);
		//通常定义浮点型使用double!
		
		//3.字符型:char(一个字符等于两个字节)
		//①定义char变量 通常 使用一对单引号 '',    >>    内部只能写一个字符!
		char c1 = 'a';
		//编译不通过
		//c1 = 'AB';
		System.out.println(c1);

		char c2 = '1';
		char c3 = '中';

		//②表示方式:1.声明一个字符 2.转义字符 3.直接使用Unicode值来表示字符型常量
		
		char c5 = '\n';//换行符
		System.out.print("hello" + c5);
		System.out.println("world");
		
		char c6 = '\t';//制表符
		System.out.print("hello" + c6);
		System.out.println("world");
		
		//Unicode字符集:
		char c7 = '\u0043'; 
		System.out.println(c7);

		//4.布尔型:boolean
		//①只能够有两个值之一,true、false
		//②常常在条件判断、循环结构中使用
		boolean bb1 = true;
		System.out.println(bb1);

		boolean isMarried = true;
		if(isMarried){
			System.out.println("不能参加\"单身\"party");
		}else{
			System.out.println("多交朋友");	
        ④通常用‘’
        char cc = '1';
        char cc1 = 97;// 输出cc1后,输出结果是a,在开发中很少见
                      // 因为97对应的ascl码是a
		}
	}
}

补充:char 字符型变量也可以不用单引号:

class ReviewTest {
	public static void main(String[] args) {

		char c1 = 'a';
		char c2 = 97;

		System.out.println(c2);//输出结果为a ,也就是说输出了97对应的阿斯克码值。
		char c3 = 98;
		char c4 = '5';
		//上面两个5是不一样的
		System.out.println(c3);//输出结果为5对应的阿斯克码值:b
	}
}

3.2变量之间的运算规则:

        1.自动类型提升:

/*
8种基本数据类型之间的运算规则:

前提:不包含第八种布尔型boolean,所以这里只讨论7中变量之间的运算

1.自动类型提升:
	byte 、char、short--> int--> long--> float--> double
	当容量小的数据类型变量和容量大的数据类型变量做运算时,结果自动提升为容量大的数据类型。
		特别的:当byte 、char、short三种变量互相互相或者本身做运算时,结果是int类型!

2.强制类型转换:是自动类型提升的逆运算。

说明:此时的容量大小指的是,表示数的范围的大和小。比如:float的容量要大于long的容量。
*/
class VariableTes2{
	public static void main(String[] args) {
		byte b1 = 2;
		int i1 = 12;
		//编译不通过:
		//byte i3 = b1 + i1;
		int i3 = b1 + i1;
		System.out.println(i3);
		float f = b1 + i1;
		System.out.println(f);
		//****************************
		char c1 = 'a';//97
		int i5 = 10;
		int i4 = c1 + i5;  
		System.out.println(i4);
		short s2 = 10;
		int s3 = s2 +c1;
	}
}

        2.强制类型转换:

/*
2.强制类型转换:是自动类型提升的逆运算。
1.需要使用强转符:()
2.注意点:强制类型转换 可能 会导致精度损失。
*/
class VariableTest3 {
	public static void main(String[] args) {
		
		//精度损失1:
		double d1 =12.3;
		int i1 = (int)d1;//截断操作!
		System.out.println(i1);
		
		//没有精度损失1:
		long l1 = 16L;
		short s1 = (short)l1;
		System.out.println(l1);
		
		//精度损失2:
		int i2 = 128;
		byte b1 = (byte)i2;
		System.out.println(b1);//-128
	}
}

        补充两个可能遇到的情况>>定义float和long类型变量是直接在末尾加上F、L

class VariableTest4 {
	public static void main(String[] args) {
		//1.编码情况:
		long l = 123465;
		System.out.println(l);
		//过大的整数,编译失败
		//long l = 15554556658648645;//不加L,默认为int,超过了int的数值范围,所以报错了

		//********************

		float f1 = 12.3;
		//2.编码情况2:
		//对于整型常量,默认为int类型;
		//对于浮点型常量,默认为double类型;
		byte b = 12;
		byte b1 = b + 1;
	}
}

Programming is an art. Although traditional art imitates life, programming simulates life. Every abstract concept in programming, and to a great extent in the fi eld of computer science, has its roots in our daily life. For example, humans and possibly all other living forms were multiprocessing long before the term entered into computer science lingo. Th erefore, any concept in programming can in fact be illustrated through examples from our dayto-day life. Such an approach not only enables the student to assimilate and internalize the concept presented in a programming situation but also provides a solid foundation for the very process of programming, namely, the simulation of the real world. Unfortunately, textbooks currently on the market do not exploit this fact through examples or meaningful discussions. Th us, for many students, an abstract concept remains abstract. Th is is especially true in the case of object-oriented programming. Th e “wow moment” one gets by seeing programming as a simulation of the real-world situation is never realized. Th is book on Java programming teaches object-oriented design and programming principles in a completely integrated and incremental fashion. Th is book allows the reader to experience the world we live in as object-oriented. From the very outset the reader will realize that everything in this world is an object. Every concept of object-oriented design is fi rst illustrated through real-life analogy. Corresponding Java language constructs are introduced in an integrated fashion to demonstrate the programming required to simulate the real-world situation. Instead of compartmentalizing all the object-oriented concepts into one chapter, this book takes an incremental approach. Th e pedagogy of this book mirrors the classroom style the author has developed over the years as a teacher of computer science. In particular, every programming concept is introduced through simple examples followed by short programming examples. Case studies at the end of each chapter illustrate various design issues as well as the usefulness of many new concepts encountered in that chapter. Java has emerged as the primary language for soft ware development. From a soft ware engineering perspective, object-oriented design has established itself as the industry standard. Th us, more and more teaching institutions are moving toward a CS1 course that teaches Java programming and object-oriented design principles. A common approach followed in many textbooks on the market is to introduce object-oriented concepts from the very beginning and ignore many traditional programming techniques completely. Th e objective of this book is to present object-oriented programming and design without compromising the training one needs on traditional programming constructs and structures.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值