Java笔记(基础篇)

0 篇文章 0 订阅

本内容参考自《 Java Programing language 4th edition》, 英文内容并非全为原文内容

Java基本概念

类(Class)

类是Java编程的基础。程序都是从类里面建立的。
Classes is the foundation of Java programming, programs are built from classes.

从一个类中可以生成任意数量的对象(object),类生成的对象则是该类的一个实例(instance)。
We can create any number of objects that are known as instances of that class.

类包含三种成员(member),分别是字段(field)、方法(method)以及嵌套类和嵌套接口(nested class & nested interface),字段和方法分别对应面向对象编程 中的属性(attribution)和行为(behavior)。成员可以隶属于这个类本身或是这个类所生成的对象。
Class contains two kinds of members, field , method, as well as nested class and nested interface. Field and method correspond to attribution and behavior in the concept of object oriented programming. Members can belong to class itself or the object of the class.

类的声明使用"class"关键字。声明时,要包括类名、类体。另外,一般来说,类的声明还会添加修饰符来表面类的属性。
We use keyword class to declare a class. It should contain name, body. In generally, class modifier will also be added to declare the attribution of the class.

public class Point{
	public int x = 0;
	public int y = 0;
	
	public void Move(int delta_x, int delta_y)
	{
		this.x = this.x+delta_x;
		this.y = this.y+delta_y;
		return;
	}
}
上述例子的"public"为修饰符,"Point"为类名,大括号内的内容则为类体。类体中包含了两个字段以及一个方法。 Keyword *public* is the class modifier and *Point* is the name of the class. All the content within the brace is the body of the class, which shows that there are two fields and one method in the class.

变量(Variables)

变量(variables)可以是类的对象或者是Java的原始数据类型(primitive data type)。Java支持的原始数据类型如下表:

名称大小数据内容
boolean1-bittrue 和 false
char16-bitUnicode 字符
byte8-bit字节数据
short16-bit带符号的整数
int32-bit带符号的整数
long64-bit带符号的整数
float32-bit浮点数(IEEE754)
double64-bit浮点数IEEE754)
Variables can be objects of classes or primitive data type of Java. The primitive data types are shown as below:
Namesizedata type
boolean1-bittrue & false
char16-bitUnicode characters
byte8-bitdata of one byte
short16-bitsigned integer
int32-bitsigned integer
long64-bitsigned integer
float32-bitfloating-point (IEEE 754)
double64-bitfloating-point (IEEE 754)

变量使用前需要进行声明。声明时可以赋予其初始值。声明操作如下:
Variables can only be used after it is declared and it can be initialized with a default value. This is how a variables is declared :

int a;
int b = 8;
同类型的多个变量可以写在同一个语句中,使用 " , " 进行分割。 Multiple variables of the same type can be declared in the same line, using "," to seperate them.
int a, b = 8;

变量可以进行赋值操作,即可以将一个变量的值赋予到另一个变量上。使用 "="进行该操作。
The value of variable can be assigned from another variable by using operater “=”, which means the value of the variable is copied to another variable.

//现在a等于8
// now a equals to 8
 a = b;

变量可以进行计算操作,并且存在优先级顺序。同一优先级的的运算符则遵从左到右的顺序。运算符及其优先度如下图显示 Variables can be calulated by using different operators. Operators have their own priority. Operaters of same priority will be calculated from left to right. Operaters and their priority are shown in the following picture.

Operators and their priority


具名常量(Named Constants)

Java允许程序员对常量进行命名。这可使代码中的某些计算常量的可读性更高。命名的常量一旦被初始化其值不能再改变。常量命名使用“final”关键字。另外,通常来说我们不希望常量字段与类的实例有关,常量声明时通常还会加上“static"关键字。
Java allows programmer to name a constant value. It makes some constant term in calaulation more readable. Once the named contants are initialized, they cannot be modified any more. Naming a constant requires keyword final. In general, because we don’t want the named constant field to be associeated with instances of the class, keyword static will be also added in declaration.


方法(Method)

方法用于操作对象中的数据并得出结果,它定义了对象的行为。方法的定义包括方法名、方法参数、返回值以及方法体。除此之外,还可添加修饰符对方法的属性进行声明。
We use method to operate the data of an object and obtain the result. It define the behavior of the object. The definition of a methed include name, parameter, result as well as body. In addition, modifier can be also used in the definition in order to declare the attribution of the method.

// Java方法定义示例
// An example of definition of method in Java

public double Distance(int x1, int y1, int x2, int y2)
{
	double xdiff = x1-x2;
	double ydiff = y1-y2;
	return Math.sqrt(xdiff*xdiff+ydiff*ydiff);
}

示例中,"public"为修饰符,"double"声明返回值的类型,distance为方法名,括号中的x1、x2、y1、y2均为参数,它们均需要声明其类型,在这里均为int类型。大括号中的内容为方法体,它定义方法如何执行。当方法执行到"return"语句时方法执行结束,并返回"return"语句中的值。
As we seen in the example, keyword public is the modifier. double declare the type of the result value. distance is the name of the method. Terms x1, x2, y1, y2 in the parentheses are the parameters of the method. Types of these parameters should be declare as we seen in the example as int. Content within the brace is the body, which define the operation of the method. When the method is invoked, it will be executed until it reach the return statement and return a value in the return statement.

注意到,Java允许创建相同名字的方法,但此时参数必须不同。在这种情况下,定义的这两个方法被视为不同的方法。
Notice that two methods with same name are allowed to create but the parameters must be different from each other. In such situation, two defined methods are regarded as two differnt methods.

调用方法时,需要写出方法名,并在括号中按照定义的顺序写入方法定义要求的类型的参数。方法调用语句本身将视为一个值,该值则为方法的返回值。
While a method is invoked, name of the method is required. Parameters with right types which is defined in the definition of the method should be written in the parenthese in the defined sequence. The statement itself is regarded as a value which is the return value of the method.

double dis = Distance(1,1,2,2);

注意到,以上调用语句仅当语句本身与方法定义处于同一类下并对该方法拥有访问权时才能生效。若要调用其它类的方法,还需声明类名或者对象名才能调用。
Notice that above invocation is only legal when such statement and the defined methed is in the same class and when the statement is accessible to the methed. When it comes to method in other class, additional class name or object name is also required.


##对象
对象是类的一个实例,它由一个类生成。对象的创建使用new关键字。
An object is an instance of a class. It is generated from a class. Using keyword new to create an object.

Point p1 = new Point();

其中,“Point()"方法为Point类的默认构造器,它的方法名与类名相同。Java允许程序员自行为类创建自定义的构造器,它可以添加任意的参数。创建对象时也可调用自定义构造器。
Point() method is the default constructor of class Point. Its name is the same as the class name. Programmers are allowed to create their own customed constructors. Any number of parameter can be added to the constructors. It’s legal to invoke user defined constructor to create an object.

对对象进行操作时,使用”.“操作符来调用对象中可访问的成员。
While operating an object, accessible members of the object can be called by using “.” operator.

p1.x = 1;
p1.y = 1;
p1.Move(1,1);

对象变量是对象的引用而非对象本身。将一个对象变量的值赋值给另一个变量并不是对对象进行拷贝,而是将其引用进行拷贝。赋值的变量与被赋值的变量代表同一个对象,对其中一者进行对象的修改操作,另一者观测到的其对象会发生改变。
Variable of an object is a reference to the object rather than the object itself. So assigning a object variable to another variable means copying the reference of the object instead of copying the object itself. When one object variable is assigned to another object variable, two variables represent one specific object. If you modify one object variable, you can see that the other one is also modified.


//将Point对象变量p1赋值给p2
//Assign object p1 to p2.
Point p2 = p1;

//修改p2后,p1.x与p1.y均变为10
//After modify p2, p1.x & p1.y also change to 10
p2.x = 10;
p2.y = 10;

System.out.println(p1.x+" "+p1.y);


字符串对象(String Objects)

字符串对象类型是一系列有顺序的字符组成的数据类型。它为Java编程提供语言级别的支持。字符串常量使用"“符号进行表示。可以使用”+"、"+="操作符进行字符串的拼接。
A String class type object is an data object with sequences of character. It provide language-level support for Java programming.

//声明两个字符串对象
//Declare two string object
String a = "hello";
String b = "world!";

//hello变量内容为"hello world!"
//Content of hello is "hello world!"
String hello = a+" "+b;

由于字符串的不可更改的常量性质,以及字符串变量的对象变量性质,字符串的修改并不是在原字符窜对象上进行修改,而是创建一个新的修改后的值的字符串。因此修改后的字符串对象的变量的引用实际上被改变了。
Due to fact that string is a constant and that the variable of a string is an variable of object, the modification of string is not modification the string variable itself, but create a new string object with modified value. Therefore, the reference of the modified variable of string object is actually changed.

//字符串变量b在a被修改前指向同一个字符串
//String variable b is refer to the same string as a

String a = "hello";
String b = a;


//字符串变量a被修改,此时a指向的字符串被改变,而b由于还是指向改变前的变量,因此没有变化。
//String variable a is modified. Now the reference of a is changed. However, because b is still refer to the unmodified string, b is still unchanged.

a = a+" world!";


//打印变量a和变量b,a为"hello world!"而b为"hello"
//Print variable a and variable b, a shows "hello world!" while b shows "hello"

System.out.println(a);
System.out.println(b);

注释(Comment)

程序员可以在代码中添加注释以提高代码的可读性。在编译时,注释的内容会被编译器忽略。注释有以下三种方式:
Programmers can add some comments in their code to promote readablility. When compiling the code, compiler will ignore the comment. There are three ways to add comment:

第一种使用"/*“以及”*/"。被两者包围的任意行数的内容均为注释,会被编译器忽略。
The first way is using “/*” and “*/”. Any number of lines of content surrounded by two notation is all regarded as comment, which will be ignored by the compiler.

/* 这些均为注释内容。
   All here is comments.
   可以添加任意行数的注释。
   Any number of lines of comments can be added in it.*/

第二种为使用"//"。该符号可以让该行的所有内容变为注释内容。
The second way is using “//” to make the content of a line the comments.

//该内容为注释。
//This is content.

第三种方法为使用"/**" 开头并以"*/"结尾。该注释方法通常用于文件开头,被称为文档注释,用于描述文件的声明。许多工具会提取此类注释来生成说明文档。
The third way is using “/**” as begin and “*/” as end. Such kind of comment usually is implemented in the very top of the file, called documentation comment, which is used as the decription of declaration. A number of tools can abtract such comments in order to generate reference document.

/** 
	这是该文件的声明
	Here is the declaration of the document
*/

控制流(Control Flow)

控制流用于控制程序的执行。它决定了程序的控制顺序。控制流语句有三种“if else” 语句、“for”语句、“switch”语句以及“do while”语句。
Countrol flow is used in control the execution of the program. It deciede the order of execution. Control flow statement include three kinds of statement: “if” statement, “for” statement, “switch” statement as well as “do while” statement.

“if” 语句用于判断某些程序块是否执行,若“if”语句括号中的值为true,则执行程序块,否则跳过或执行else 语句下的程序块。
If statement serves as the judgement of whether a code block is executed. If the value in paratheses in if statement is true, the block will be executed. If not, it will be ignored or execute the code under else statement.

int i = 0;
int j = 1;

if(i == 0)
{
	i++;
	j--;
}

else
{
	i = 0;
	j = 1;
}

(未完待续)

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值