COMP0004-Notes(1): Java

8 篇文章 0 订阅

Java the language

Java SE & Java JDK

Java SE is the language, SDK is the language + compilers & other tools (example?)

JVM

Java Virtual Machine, run-time, memory allocation, garbage collection(on a separte thread), etc.

Classes

The whole point of Java revolves around the concept of classes. It is dozens/hundreds/thousands classes working & communicating with one another to make a program running. (See another note on Principles of Java Programming)

Objects & References

Classes are blueprints, an abstraction on how a particular item is organised (e.g. the idea of a person, having two eyes and one nose, being able to breathe and able to think etc.), Objects are the concrete instances created from the blueprint and one instance only (e.g. Socrates is an object, an instance, an example of the blueprint Person we just described earlier).

  • Objects created at runtime
  • one-to-many relationship

Consider the following Java example:

Person Socrates = new Person();, 

The term reference is used in the case of Java, in a similar sense to the term pointers in C. To interpret the socrates example above, we say that:

“Variable Socrates is of type Person, it stores a reference to an object of class Person.”

Note the term “a reference to”. Compare this with primitive types, e.g. int n = 1;, the int variable n here directly stores the int literal: 1. With classes, however, the objects created new Person() is NOT directly stored in a random piece of space in the linear memory which can directly be accessed via the “pointer” Socrates. Instead…

Classes on Heap

The exact objects, when they are created (at runtime), are stored in memory and automatically orgranised by the JVM in a heap. This implies another difference between Java and C:

  • In C, there is nothing between YOU, the programmer, and the computer memory. You and you only are the one responsible for managing the memory. There is no one to do that for you. A pointer directly points to the location where everything is stored and you retrieve anything at your own will.
  • In Java, however, there is the Java Virtual Machine that completely takes over the responsibility, and the programmer in this case will have absolutely NO direct access to the memory. YOU the programmer can only ask for accessing an object, and during the compile of the program, the JVM retrieves the object for you. In this case, the variable (in this case, Socrates) plays the part as a guide, because it stores the reference to the object, it knows how to find the object in the heap when you need it.
  • reference is not directly memory address, think of it as an entry in a table (object-reference table)
    Variable store references
    Look at this diagram. Socrates only stores the “arrow” to the object, not the object itself.

Instance Variables

“Instance variables” is just a fancy name for attributes, what every object of this class “looks like”. Attributes of a Person can be that having two eyes and one nose.

Methods

Methods are the functions that a class has. In other words, what behaviour every object of this class can do.

Data types

Primitive & Object Data Types

Primitive Types

Stored in binary format, they are not objects. However, autoboxing allows primitive types being boxed into corresponding class types:
int -> Integer
char -> Character
double -> Double
etc.

Class Types

  • == on Primitive: compares the raw values
  • == on Class: compares the references, see if the two variables hold the reference to the same object
  • use .equals()!

Type Inference & Casting

  • Compiler inferences the type of variable by looking at the right-hand-side
  • In this way compiler does more work
var x = 100;
  • is an example of the use of type inferencing

Boxing(Auto-) & Unboxing

Switch statement

  • Fall-through error
  • the use of ->
  • compound case + yield

Arrays

  • runtime bounds checking
  • Also the array only stores reference to objects rather than the specific objects!

Constructors

  • contructor method must have the same name as the class

Layout of a Java class:

  1. Instance Variables
  2. Constructor
  3. Instance Methods

Graham Roberts, COMP0004, UCL, 2022

Static methods

  • static methods doesn’t need to be called on an object, like Math.abs()… you don’t need to create a Math object to use abs()
  • Don’t overuse it, currently use it for main is enough

Overloading

One or more methods or constructors can have the same name, so long as they have different arguments!

The compiler will determine which method to call based on the parameters being passed.

What is ‘this’ keyword?

this is a special variable that is automatically declared in an object. It refers to the object in which the variable is declared itself. Python’s self keyword serves a similar (but less powerful?) role. With this, you can directly refer to the current object. As a result, the type of this is the type of the current object.

What is the use of ‘this’?

1. Pass Myself to Another Method!

class A
{
	private B my_b = ...;
	public void a_method()
	{
		my_b.b_method(this);
	}
}

Here, my_b is a reference to another object of type B. We pass ourself to one of my_b's method using the this keyword.

2. Change My State

class A
{
	private int x;
	public void f(int x)
	{
		this.x = x;
	}
}

Here, we change the value of the attribute x of this object by calling this.

3. An Overloaded Constructor

class A
{
	public A(int x, int y, int z)
	{
		// some initialising code like below, but can be more complex
		this.x = x;
		this.y = y;
		this.z = z;
	}

	// An overloaded constructor supplying some default values
	public A()
	{
		this(0,0,0);
	}
}

Although you can definitely rewrite the second constructor without this, by copying over a exactly identical block of code from above, but only replacing the x, y and z values with the default values you want to have. In that way you would end up not only having two completely identical block of code, which already violates the principle of simplicity (non-duplication), but also making it look like you have inserted some “magic numbers” in the initialisation, which can be confusing especially when the code gets complicated.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值