Java

Java
ExisfarJava
Configuration
在JDK1.5之后的版本,配置Java环境变量的时候我们不再需要配置classpath,只需要配置Java_Home以及path即可!
原配置代码
.;%Java_Home%\bin;%Java_Home%\lib\dt.jar;%Java_Home%\lib\tools.jar
原代码详解
Java_Home代表了我们jdk的路径
dt.jar是关于运行环境的类库,主要是用于swing的包,如果不使用可以不配置。
tools.jar是工具类库,它在编译和运行一个类时被使用
当我们配置classpath后,系统会根据我们所配置的classpath加载类
例如:在我们使用javac命令编译程序时,系统加载tools.jar其实就封装了下面这样一条命令
javac XXX.java |
当然tools的功能可不止这一点,但是确实它为我们提供了很多便利。
我们不再需要配置classpath了!
在JDK1.5以后,classpath并不是必须配置了,在JDK1.5之前,是没有办法在当前目录下加载类的(找不到 JDK目录下lib文件夹中的.jar文件),所以我们需要通过配置classpath,但JDK1.5之后,JRE能自动搜索目录下类文件,并且加载dt.jar和tool.jar的类。
官方档解释(JDK Tools and Utilities)
The class path tells the JDK tools and applications where to find third-party and user-defined classes that are not extensions or part of the Java platform. See The Extension Mechanism at
类路径告诉JDK工具和应用程序在哪里可以找到第三方和用户定义的类,这些类既不是Java平台的扩展,也不是Java平台的一部分。参见扩展机制
If you upgrade from an earlier release of the JDK, then your startup settings might include CLASSPATH settings that are no longer needed. You should remove any settings that are not application-specific, such as classes.zip. Some third-party applications that use the Java Virtual Machine (JVM) can modify your CLASSPATH environment variable to include the libraries they use. Such settings can remain.
如果您从JDK的早期版本升级,那么您的启动设置可能包括不再需要的类路径设置。您应该删除任何与应用程序无关的设置,比如classes.zip。一些使用Java虚拟机(JVM)的第三方应用程序可以修改类路径环境变量,以包含它们使用的库。这样的设置可以保留。
You can change the class path by using the -classpath or -cp option of some Java commands when you call the JVM or other JDK tools or by using the CLASSPATH environment variable. See JDK Commands Class Path Options. Using the -classpath option is preferred over setting the CLASSPATH environment variable because you can set it individually for each application without affecting other applications and without other applications modifying its value. See CLASSPATH Environment Variable.
在调用JVM或其他JDK工具时,可以使用一些Java命令的-classpath或-cp选项,或者使用CLASSPATH环境变量,来更改类路径。参见JDK命令类路径选项。使用-classpath选项优于设置CLASSPATH环境变量,因为您可以为每个应用程序单独设置它,而不影响其他应用程序,也不需要其他应用程序修改它的值。参见CLASSPATH环境变量。
通过官方的文档说明我们可以看到,rt.jar和tool.jar这两种属于java平台自身的包就不需要添加到classpath中,只有一些第三方类或者自定义类需要,也并不推荐使用配置CLASSPATH的方法,更推荐使用-classpath选项
Dynamic Allocation
In Java, the syntax int a[size]
is used to declare an array, but it behaves differently compared to C/C++. Let’s break it down:
Array Declaration in Java
In Java, int a[size]
is not valid syntax by itself. Instead, you need to declare the array and then allocate memory for it separately. Here’s how it works:
-
Declaration:
int[] a; // Declares an array reference (no memory allocated yet)
-
Allocation:
a = new int[size]; // Allocates memory for the array
Here:
size
is a variable that determines the size of the array at runtime.new int[size]
dynamically allocates memory for the array on the heap.
Example
int size = 5; |
Here:
a
is a reference to an array of integers.- The memory for the array is allocated dynamically on the heap using the
new
keyword. - The size of the array (
size
) can be determined at runtime.
Why This is Dynamic Allocation
In Java:
- Arrays are always allocated on the heap.
- The size of the array can be determined at runtime.
- The memory is managed by the Java Virtual Machine (JVM) through garbage collection, so you don’t need to manually free the memory.
This is fundamentally different from C/C++, where int a[size]
can sometimes be stack-allocated (static allocation) depending on the context.
Key Points
-
Dynamic Allocation:
- In Java, arrays are always dynamically allocated on the heap using the
new
keyword. - The size of the array can be determined at runtime.
- In Java, arrays are always dynamically allocated on the heap using the
-
No Stack Allocation for Arrays:
- Unlike C/C++, Java does not allow stack allocation for arrays. All arrays are heap-allocated.
-
Automatic Memory Management:
- Java handles memory management automatically through garbage collection. You don’t need to manually free the memory allocated for arrays.
Comparison with C/C++
Feature | Java (int[] a = new int[size] ) |
C/C++ (int a[size] ) |
---|---|---|
Memory Location | Heap | Stack (if size is known at compile time) or Heap (if using malloc /new ) |
Allocation Type | Always dynamic | Can be static (stack) or dynamic (heap) |
Memory Management | Managed by JVM (garbage collection) | Manual (programmer must free memory) |
Syntax | int[] a = new int[size]; |
int a[size]; (stack) or int* a = new int[size]; (heap) |
Summary
In Java, int[] a = new int[size]
is dynamic allocation because:
- The array is allocated on the heap.
- The size can be determined at runtime.
- Memory is managed by the JVM.
This is different from C/C++, where int a[size]
can be either static (stack) or dynamic (heap) depending on the context. In Java, arrays are always dynamically allocated.
Method Overriding
In Java, the behavior is similar to C++ when it comes to method overriding and polymorphism, but there are some key differences in how member variables are handled. Let’s analyze the equivalent Java code and its output.
Java Code
class Animal { |
Key Differences Between Java and C++
-
Method Overriding:
- In Java, all non-static methods are implicitly virtual (unless marked
final
orprivate
). This means that the method to be called is determined at runtime based on the actual object type (dynamic binding). - In C++, methods are only virtual if explicitly declared with the
virtual
keyword.
- In Java, all non-static methods are implicitly virtual (unless marked
-
Member Variable Hiding:
- In Java, if a derived class defines a member variable with the same name as a base class member variable, the derived class variable hides the base class variable.
- However, the variable accessed depends on the reference type, not the object type. This is similar to C++.
Why Dog Eat
is Printed
-
dog.eat()
:- The
eat()
method inAnimal
is overridden inDog
. - Since
dog
is a reference of typeAnimal
but points to aDog
object, the overriddenDog::eat()
method is called at runtime. This is because Java uses dynamic method dispatch for non-static methods. - Therefore,
"Dog Eat."
is printed.
- The
-
dog.a
:- The variable
a
is not overridden in Java; it is hidden. - The reference type (
Animal
) determines which variable is accessed. Sincedog
is of typeAnimal
,Animal::a
is accessed, and1
is printed.
- The variable
Output
1 |
Summary of Behavior in Java
dog.eat()
: Calls the overriddenDog::eat()
method because Java uses dynamic method dispatch for non-static methods.dog.a
: AccessesAnimal::a
because the reference type (Animal
) determines which variable is accessed.
Key Takeaways
- In Java, methods are dynamically dispatched based on the actual object type (runtime polymorphism).
- Member variables are resolved based on the reference type, not the object type.
How to Access Dog::a
If you want to access the derived class member variable (Dog::a
), you need to use a reference of type Dog
. For example:
Dog dogRef = d; |
This behavior is consistent with C++ and Java’s handling of member variables and method overriding.