Background#
I'm about to enter my third year of college. Starting from our batch, the college offers specialized training in the third year. I chose software development based on my personal interest, so Java became my compulsory course...
After the end of the summer semester on July 24th, following the basic principle of "it's better to find something to do than to do nothing", I started previewing Java (actually just slacking off). It has been five days now, and I'm ready to write a small summary.
Choice of Tutorial#
I consulted the members of the "NEU LUG" group for advice on this issue, and the suggestion I received was to watch online courses. However, considering my own situation, I ultimately chose a learning method that suits me - reading documentation. Here, I chose the Liaoxuefeng tutorial. As for the reason, it's mainly because I used his tutorials for Git
/JavaScript
/Python
, so naturally I chose his tutorial for Java
as well. 🤣
After studying for a week, I feel that the tutorial covers various aspects in detail and delves deep into the new features of Java 14
. There are practice exercises after each section to help consolidate knowledge, and if there are any unclear points, you can ask questions in the comments. Overall, it's a great tutorial.
Current Progress#
Currently, I have studied the first six chapters and part of the seventh chapter of the tutorial. These chapters are:
- Java Quick Start
- Object-Oriented Programming
- Exception Handling
- Reflection
- Annotations
- Generics
- Collections
Learning Experience#
-
Java Quick Start
This chapter explains the variable types and basic syntax of Java. Overall, it is very similar to
C language
and relatively easy to understand, but there are still some things to pay attention to. For example:-
Switch expression introduced in Java 12
String fruit = "apple"; // opt = 1 int opt = switch(fruit){ case "apple"->1; case "pear" ->2; }
-
Java data types
It should be noted that, except for integer, floating-point, character, and boolean types, all other types in Java are reference types (objects).
Java internally uses UTF-16 to store characters, so each Java character occupies two bytes.
To facilitate operations, each primitive type has its corresponding wrapper class (e.g., the wrapper class for
int
isInteger
).String is immutable. When assigning a new value to a String, a new space is actually allocated and the String pointer is redirected to that space.
-
var keyword
This keyword is used to let the compiler automatically infer the type, similar to
auto
in C++.var s = "test"; // s is String
-
Array traversal
Java provides a way to traverse arrays similar to Python's
for ... in ...
:String[] arr = new String[] { "1", "2", "3" }; for (var item : arr) { System.out.println(item); }
-
-
Object-Oriented Programming
Object-oriented programming is a well-known concept. If you have a learning foundation in C++, it should not be difficult to understand. As for the core classes in Java, I feel that they are more like things to look up when needed, and there is no need to master them proficiently.
-
Instance fields/methods and static fields/methods
In my personal understanding, instance fields/methods are applied to objects (instances), while static fields/methods are applied to the class itself.
For example, in a student class, the individual's name, age, etc. should be instance fields, while the number of students, which is a "class property," should be a static field.
In addition, static methods are also used to represent functionality that can exist independently of an instance, such as:
// The static method valueOf in the Number class is used to construct a Number instance, independent of any specific Number Number.valueOf(); // The static method sort in the Arrays class is used to sort an array, independent of any specific Arrays Arrays.sort();
-
final keyword
The final keyword has three main purposes:
- Used to modify variables, indicating that the variable cannot be changed
- Used to modify methods, indicating that the method cannot be overridden
- Used to modify classes, indicating that the class cannot be inherited
-
Interfaces
Interfaces are a further abstraction of abstract classes, allowing only public methods and static and final fields.
-
Last four sections of the basics of object-oriented programming
The last four sections of the basics of object-oriented programming: packages, scope, classpath and jar, and modules explain the organization of Java code, which I personally feel is very important. It may be a bit confusing at first, so you can try reading it a few more times (although I'm still confused about it).
-
-
Exception Handling
Java's exception handling uses the
try...catch...
mechanism, which allows errors to be thrown up layer by layer. This section is relatively simple, so there isn't much to say about it. -
Reflection
Reflection is a mechanism for a program to obtain object information at runtime and call its methods without knowing anything about the instance. For example, calling a function by its name as a string.
In this chapter, I found the dynamic proxy section difficult to understand (mainly because I'm not sure what it's used for). One user in the comments gave a very simple example, which was very enlightening for me.
-
Annotations
In my opinion, annotations are a very useful feature. The tutorial gives a practical example, which is to use self-defined annotations for batch parameter checking. This comment explains the purpose of annotations very well.
-
Generics
Generics are another difficult hurdle, with the main difficulties concentrated in the last three sections: type erasure, extends wildcard, and super wildcard.
-
Type erasure
Java's implementation of generics uses type erasure, which means that all handling of generics is done by the compiler, and the virtual machine knows nothing about generics. This leads to many problems.
- Generics cannot be used with primitive types because the actual type of generics is Object.
- It is not possible to obtain the Class of the generics.
- It is not possible to determine the type of the generics (instanceof).
- It is not possible to directly instantiate a type T, and an additional Class is needed.
-
Extends and super wildcards
These two sections mainly explain the differences in properties caused by the use of extends/super wildcards in methods targeting generics. In general, extends is for reading and super is for writing.
To understand this property, you need to understand the difference between upcasting and downcasting:
Upcasting refers to using a parent class reference to refer to a child class, while downcasting refers to using a child class reference to refer to a parent class. The former is safe, while the latter is unsafe.
class Person{} class Student extends Person{} Person person = new Student(); //√ Student student = new Person(); //×
Therefore,
<? extends Integer>
indicates that the generics inside must beInteger
or its subclass. In this case, assigningInteger
or its subclass from the generics to the outerInteger
is upcasting, which is safe. However, assigning the outerInteger
to the inner subclass is downcasting, so an error occurs. The resulting property is "readable but not writable". The opposite is true for<? super Integer>
.
-
-
Collections
I have only studied about half of the seventh chapter so far, and I find it relatively simple. This chapter mainly covers basic data structures, such as arrays, linked lists, hash tables, etc., and their usage. Since it does not involve design and implementation, this chapter is quite relaxed.
To be continued... (Random updates) 😝