java回顾与拓展

1.publicstaticvoidmain(String[] args){}

String[]表示的是字符串类型的数组,args表示的是传入的参数名,所以整体的意思就是主方法main(String[] args)可以接收一个字符串类型的数组,数组名字为args。(相当于入参

2.抽象方法的声明以分号结尾,例如:public abstract sample();

3.synchronized 关键字声明的方法同一时间只能被一个线程访问。

4.transient 修饰符
序列化的对象包含被 transient 修饰的实例变量时,java 虚拟机(JVM)跳过该特定的变量。

该修饰符包含在定义变量的语句中,用来预处理类和变量的数据类型。

public transient int limit = 55; // 不会持久化
public int b; // 持久化
volatile 修饰符
volatile 修饰的成员变量在每次被线程访问时,都强制从共享内存中重新读取该成员变量的值。而且,当成员变量发生变化时,会强制线程将变化值回写到共享内存。这样在任何时刻,两个不同的线程总是看到某个成员变量的同一个值。一个 volatile 对象引用可能是 null。
public class MyRunnable implements Runnable
{
private volatile boolean active;
public void run() {
active = true;
while (active) { // 第一行
// 代码
}
}
public void stop() {
active = false; // 第二行
}
}
通常情况下,在一个线程调用 run() 方法(在 Runnable 开启的线程),在另一个线程调用 stop() 方法。 如果 第一行 中缓冲区的 active 值被使用,那么在 第二行 的 active 值为 false 时循环不会停止。

但是以上代码中我们使用了 volatile 修饰 active,所以该循环会停止。

5.instanceof 运算符
该运算符用于操作对象实例,检查该对象是否是一个特定类型(类类型或接口类型)。

instanceof运算符使用格式如下:
( Object reference variable ) instanceof (class/interface type)
如果运算符左侧变量所指的对象,是操作符右侧类或接口(class/interface)的一个对象,那么结果为真。

下面是一个例子:
String name = "James";
boolean result = name instanceof String; // 由于 name 是 String 类型,所以返回真
如果被比较的对象兼容于右侧类型,该运算符仍然返回true。

看下面的例子:
class Vehicle {}

public class Car extends Vehicle {
public static void main(String[] args){
Vehicle a = new Car();
boolean result = a instanceof Car;
System.out.println( result);
}
}
以上实例编译运行结果如下:true

6.数组定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
   public static void main(String[] args) {
// 数组大小
int size = 10;
// 定义数组
double[] myList = new double[size];
myList[0] = 5.6;
myList[1] = 4.5;
myList[2] = 3.3;
myList[3] = 13.2;
myList[4] = 4.0;
myList[5] = 34.33;
myList[6] = 34.0;
myList[7] = 45.45;
myList[8] = 99.993;
myList[9] = 11123;
// 计算所有元素的总和
double total = 0;
for (int i = 0; i < size; i++) {
total += myList[i];
}
System.out.println("总和为: " + total);
}
}

7.封装:是指一种将抽象性函式接口的实现细节部份包装、隐藏起来的方法。
良好的封装能够减少耦合。
类内部的结构可以自由修改。
可以对成员变量进行更精确的控制。
隐藏信息,实现细节。

8.继承:子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为。

extends :publicclassPenguin extends Animal{}

implements:public class C implements A,B {}

void eatTest() {
this.eat(); // this 调用自己的方法
super.eat(); // super 调用父类方法
}`

如果父类构造器没有参数,则在子类的构造器中不需要使用 super 关键字调用父类构造器,系统会自动调用父类的无参构造器。

9.抽象类总结规定
抽象类不能被实例化(初学者很容易犯的错),如果被实例化,就会报错,编译无法通过。只有抽象类的非抽象子类可以创建对象。
抽象类中不一定包含抽象方法,但是有抽象方法的类必定是抽象类。
抽象类中的抽象方法只是声明,不包含方法体,就是不给出方法的具体实现也就是方法的具体功能。
构造方法,类方法(用 static 修饰的方法)不能声明为抽象方法。
抽象类的子类必须给出抽象类中的抽象方法的具体实现,除非该子类也是抽象类。

10.在Java中,类的多继承是不合法,但接口允许多继承。

11.

1
2
3
4
5
6
7
8
9
10
class Main {
public static void main(String args[]) {
System.out.println(fun());
}

int fun()
{
return 20;
}
}

Main 类中 main() 是一个静态函数, fun() 是一个非静态函数, Java 静态函数中不能调用非静态函数的方法

12.

1
2
3
4
5
6
7
8
9
10
11
public class Main { 
public static void main(String args[]) {
String x = null;
giveMeAString(x);
System.out.println(x);
}
static void giveMeAString(String y)
{
y = "NOWCODER";
}
}

Java 中参数通过值传递,所以 x 传到函数中不会影响原来的值。

13.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class intWrap {
int x;
}
public class Main {
public static void main(String[] args) {
intWrap i = new intWrap();
i.x = 10;
intWrap j = new intWrap();
j.x = 20;
swap(i, j);
System.out.println("i.x = " + i.x + ", j.x = " + j.x);
}
public static void swap(intWrap i, intWrap j) {
int temp = i.x;
i.x = j.x;
j.x = temp;
}
}

在 Java 应用程序中永远不会传递对象,而只传递对象引用。因此是按引用传递对象。

14.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Main {
public static void main(String args[]) {
System.out.println(fun());
}
static int fun(int x = 0)
{
return x;
}
}
//
编程错误
Java 函数不允许参数设置默认值。

15.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Test implements Cloneable
{
int a;

Test cloning()
{
try
{
return (Test) super.clone();
}
catch(CloneNotSupportedException e)
{
System.out.println("CloneNotSupportedException is caught");
return this;
}
}
}

class Main
{

public static void main(String args[])
{
Test obj1 = new Test();
Test obj2;
obj1.a = 10;
obj2 = obj1.cloning();
obj2.a = 20;

System.out.println("obj1.a = " + obj1.a);
System.out.println("obj2.a = " + obj2.a);
}
}

clone( ) 方法调用时会生成多个对象的拷贝。 类只有在实现 Cloneable 接口才可以实现克隆。

16.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Main
{
public static void main(String[] args)
{
String str = "nowcoder";
str.toUpperCase();
str += "wwwnowcodercom";
String string = str.substring(2,13);
string = string + str.charAt(4);;
System.out.println(string);
}
}
//str.toUpperCase() 将字符串小写字母转换为大写字母,但是它不会改变原始的字符串。 str.substring(x, y) 返回 ‘x'(包含) 到 ‘y'(不包含) 位置的字符串。 str.charAt(x) 返回 x 位置的字符。