Basics of Java Syntax
Java: strongly typed (enforced constraint) language
Grammar: The legal constraints in the development language must be obeyed when writing code
// 单行注释 快捷键 ctrl+/
/*
* 多行注释 /*+回车
*/
/**
* 文档化注释 /**+回车
*/
System.out.println("1+2"); //所见即所得 1+2
System.out.println(1+2); //计算内容 3
System.out.println() //输出并换行
System.out.print() //输出不换行
1. The role of annotations
- Block unwanted code
- Add description to the corresponding code
- Assist the rest of the staff to quickly understand your code (enterprise core)
2. Keywords
Keyword | meaning |
---|---|
abstract | Indicates that the class or member method has abstract properties |
assert | Assertions, used for program debugging |
boolean | One of the basic data types, a keyword that declares the Boolean type |
break | Jump out a block ahead of time |
byte | One of the basic data types, byte type |
case | Used in switch statements to indicate one of the branches |
catch | Used in exception handling to catch exceptions |
char | One of the basic data types, character type |
class | Declare a class |
const | Keyword reserved, no specific meaning |
continue | Back to the beginning of a block |
default | Default, for example, used in a switch statement, indicates a default branch. Java 8 also acts on declaring the default implementation of interface functions |
do | Used in the do-while loop structure |
double | One of the basic data types, double-precision floating-point number type |
else | Used in conditional statements to indicate the branch when the condition is not true |
enum | enumerate |
extends | Indicates that one type is a subtype of another type. For a class, it can be another class or abstract class; for an interface, it can be another interface |
final | Used to illustrate the final attributes, indicating that a class cannot derive subclasses, or member methods cannot be overridden, or the value of member fields cannot be changed, used to define constants |
finally | Used to handle exceptions, used to declare a block of statements that will basically be executed |
float | One of the basic data types, single-precision floating-point number type |
for | A guide word with a cyclic structure |
goto | Keyword reserved, no specific meaning |
if | The leading word of the conditional sentence |
implements | Indicates that a class implements a given interface |
import | Indicates that you want to access the specified class or package |
instanceof | Used to test whether an object is an instance object of a specified type |
int | One of the basic data types, integer type |
interface | interface |
long | One of the basic data types, long integer type |
native | Used to declare that a method is implemented by a computer-related language (such as C/C++/FORTRAN language) |
new | Used to create new instance objects |
package | package |
private | 一种访问控制方式:私用模式 |
protected | 一种访问控制方式:保护模式 |
public | 一种访问控制方式:共用模式 |
return | 从成员方法中返回数据 |
short | 基本数据类型之一,短整数类型 |
static | 表明具有静态属性 |
strictfp | 用来声明FP_strict(单精度或双精度浮点数)表达式遵循[IEEE 754](https://baike.baidu.com/item/IEEE 754)算术规范 |
super | 表明当前对象的父类型的引用或者父类型的构造方法 |
switch | 分支语句结构的引导词 |
synchronized | 表明一段代码需要同步执行 |
this | 指向当前实例对象的引用 |
throw | 抛出一个异常 |
throws | 声明在当前定义的成员方法中所有需要抛出的异常 |
transient | 声明不用序列化的成员域 |
try | 尝试一个可能抛出异常的程序块 |
void | 声明当前成员方法没有返回值 |
volatile | 表明两个或者多个变量必须同步地发生变化 |
while | 用在循环结构中 |
3、标识符
Java标识符:俗称姓名(在java中,一般情况,尽可能不要重复定义)
命名约定:
- 标识符有字母、下划线“-”、美元符“$”、或数字组成(4种)
- 标识符必须以字母、下划线、美元符开头。(数字不能作为开头)
- Java标识符大小写敏感,长度无限制。
- “见名知意”
- 约定俗成的规则
- 中文可以定义为标识符,但实际开发中,没有企业这样使用,因为执行效率会降低。
- 所以的关键字都不能作为标识符
4、数据类型
5、运算符
三目运算符 返回变量= 布尔添加 ? true的结果 : false的结果
true和false结果返回的类型必须一致!!!
6、数据类型转换
byte<short<int<long<float<double
数据类型字节小的转换为大的可以自动转换
大的转换为小的需要强制装换
int a=256;
//强制类型转换
byte u=(byte)a;
//输出为0
//遵循圆环定理
自动转换
long L=a;
7、Java流程控制
7.1、选择语句(if)
单分枝
if(条件表达式){
语句块
}
只要返回值是布尔类型的,就是条件表达式
{}官方称号为域,通俗称呼是语句块
语句块中可以放置0行、1行、多行代码
双分枝
条件为真执行if中的代码,为假执行else里面的代码
if(条件表达式){
语句块1
}else{
语句块2
}
多分枝
if(条件表达式){
语句块1
}else if(条件表达式){
语句块2
}
else if(条件表达式){
语句块3
}else{
语句块4
}
多分枝如果判断语句都满足的情况,默认只会走第一个满足的语句
switch(){
case :
break;
}
7.2、循环语句
for循环
for(表达式1;表达式2;表达式3){//默认的表达式2为true,也就是死循环
循环体
}
流程:表达式1->表达式2->循环体->表达式3->表达式2->循环体…直到表达式2返回false,整个循环才会终止。
while循环
while(布尔值){
语句块;
}
do{
}while(布尔表达式);
从 JDK 1.5后有一个 foreach 循环(性能高于普通的for)
int[] a={
1,2,3,4,5};
for (int i : a) {
System.out.println(i);
}
8、人机交互(Scanner)
Scanner sc=new Scanner(System.in);
int i= scanner.nextInt();
//从电脑接收一个整形值
9、数组
8种基本类型+字符串[] arrays=new 类型与前面一致[长度]。
int[] arr=new int[5]; //定义一个数组
arr[0]=10; //给数组赋值
Java中的数组对象(锁死)长度是不可变的,但内容是可变的
new在Java中永远代表新的生成内容
Arrays
在java.util.Arrays有个可以直接输出数组的值
int i= scanner.nextInt();
//从电脑接收一个整形值
9、数组
8种基本类型+字符串[] arrays=new 类型与前面一致[长度]。
int[] arr=new int[5]; //定义一个数组
arr[0]=10; //给数组赋值
Java中的数组对象(锁死)长度是不可变的,但内容是可变的
new在Java中永远代表新的生成内容
Arrays
在java.util.Arrays有个可以直接输出数组的值
Arrays.toString(数组对象);
希望对广大刚学java的同学有帮助,谢谢大家!