Chapter 2 Classes and Objects

1) 
Body mercury;

This declaration states that mercury is a variable that can hold a reference to an object of type Body. The declaration DOES NOT create an object.

2) this(...) could be used to call constructors:

class Body {
    public long idNum;
    public String name;
    public Body orbits;
    Body() {
        idNum = nextID++;
    }
  
    Body(String bodyName, Body orbitsAround) {
        this(); // explicit constructor invocation, call constructor Body()
        name = bodyName;
        orbits = orbitsAround;
    }
public static long nextID = 0;}

3)Initialization Block: a block of statements that appears within the class declaration, outside of any member, or constructor, declaration and that initializes the fields of the object.

class Body {
    public long idNum;
    public String name = "<unnamed>";
    public Body orbits = null;

    private static long nextID = 0;

    {
        idNum = nextID++;
    }

    public Body(String bodyName, Body orbitsAround) {
        name = bodyName;
        orbits = orbitsAround;
    }
}


The initialization block is executed as if it were placed at the beginning of every constructor in the class - with multiple blocks being executed in the order they appear in the class.

Code:

public class InitializationBlock {
	{
		System.out.println("Block 1");
	}
	
	{
		System.out.println("Block 2");
	}
	
	{
		System.out.println("Block 3");
	}
	
	InitializationBlock(){
		System.out.println("Default Constructor!");
	}
	
	InitializationBlock(String str) {
		System.out.println("Constructor with parameter!");
	}
	
	public static void main(String[] args) {
		InitializationBlock block1 = new InitializationBlock();
		InitializationBlock block2 = new InitializationBlock("JavaBeta");
	}
}

Output:
Block 1
Block 2
Block 3
Default Constructor!
Block 1
Block 2
Block 3
Constructor with parameter!
A static initialization block is much like a non-static initialization block except it is declaredstatic, can only refer to static members of the class, and cannot throw any checked exceptions.


class Primes {
    static int[] knownPrimes = new int[4];

    static {
        knownPrimes[0] = 2;
        for (int i = 1; i < knownPrimes.length; i++)
            knownPrimes[i] = nextPrime();
    }
    // declaration of nextPrime ...
}
4) Methods with Variable Numbers of Arguments
The last parameter in a method (or constructor) parameter list can be declared as a sequence of a given type. To indicate that a parameter is a sequence you write an ellipse (...) after the parameter type, but before its name.
public static void print(String... messages) {
    // ...
}

Sequence parameters allow a method to be invoked with a variable number of arguments (including zero) that form the sequence.
System.out.printf is an example of method with variable numbers of arguments
5) All objects have a toString method whether their class explicitly defines one or not - this is because all classes extend the class Object and it defines the toString method.
6)All parameters to methods are passed "by value." In other words, values of parameter variables in a method are copies of the values the invoker specified as arguments. If you pass adouble to a method, its parameter is a copy of whatever value was being passed as an argument, and the method can change its parameter's value without affecting values in the code that invoked the method. 
You should note that when the parameter is an object reference, it is the objectreferencenot the object itselfthat is passed "by value." Thus, you can change which object a parameter refers to inside the method without affecting the reference that was passed. But if you change any fields of the object or invoke methods that change the object's state, the object is changed for every part of the program that holds a reference to it.
Code:

class Person{
	String name;
	int age;
	Person(String name, int age){this.name = name; this.age = age;}
	void print(){System.out.println("(" + name + "," + age + ")");}
}
public class PassByValue {
	static void change(Person person){
		person.name = "JavaBeta";
		person.age = 27;		
	}
	
	static void modify(Person person){
		person = new Person("JavaBeta", 27);		
	}
	public static void main(String[] string) {
		Person person = new Person("JavaAlpha", 27);
		person.print();
//		change(person); //change the fields of person
		modify(person); //pass-by-value, so the object refernce is unchanged!
		person.print();
	}
}

Output:
(JavaAlpha,27)
(JavaBeta,27)


The Java programming language DOES NOT pass objects by reference; All parameters to methods are passed "by value".


7)You can declare method parameters to be final, meaning that the value of the parameter will not change while the method is executing.


8)If two signatures differ only because one declares a sequence and the other an array, then a compile-time error occurs.
Fixed-argument method will always be selected over a varargs method.


public class OverloadingWithVarargs {
	static void print(String str){
		System.out.println("Print 1: " + str);
	}
	static void print(String str, String...strings){
		System.out.println("Print 2: " + str);
		for(int i = 0;i < strings.length; ++i) System.out.println(strings[i]);
	}
	static void print(String...strings){
		System.out.println("Print 3: ");
		for(int i = 0;i < strings.length; ++i) System.out.println(strings[i]);
	}
	public static void main(String[] args){
		String str = "Java Beta"; // fixed parameter is prior to ones with varargs
//		print(str, "Java Beta"); // ambiguity: print 2 or print 3?
	}
}


9)static import statement: 
import static java.util.Math.exp
Then you can use exp instead of Math.exp in your program. A static import statement must appear at the start of a source file before any class or interface declaration.


static import on demand statement: 
import static static java.util.Math.*








内容概要:《2024年中国城市低空经济发展指数报告》由36氪研究院发布,指出低空经济作为新质生产力的代表,已成为中国经济新的增长点。报告从发展环境、资金投入、创新能力、基础支撑和发展成效五个维度构建了综合指数评价体系,评估了全国重点城市的低空经济发展状况。北京和深圳在总指数中名列前茅,分别以91.26和84.53的得分领先,展现出强大的资金投入、创新能力和基础支撑。低空经济主要涉及无人机、eVTOL(电动垂直起降飞行器)和直升机等产品,广泛应用于农业、物流、交通、应急救援等领域。政策支持、市场需求和技术进步共同推动了低空经济的快速发展,预计到2026年市场规模将突破万亿元。 适用人群:对低空经济发展感兴趣的政策制定者、投资者、企业和研究人员。 使用场景及目标:①了解低空经济的定义、分类和发展驱动力;②掌握低空经济的主要应用场景和市场规模预测;③评估各城市在低空经济发展中的表现和潜力;④为政策制定、投资决策和企业发展提供参考依据。 其他说明:报告强调了政策监管、产业生态建设和区域融合错位的重要性,提出了加强法律法规建设、人才储备和基础设施建设等建议。低空经济正加速向网络化、智能化、规模化和集聚化方向发展,各地应找准自身比较优势,实现差异化发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值