如何从Java中的函数返回多个值?任何人都可以使用元组提供示例代码吗?我无法理解元组的概念.
public class Tuple{
public static void main(String []args){
System.out.println(f());
}
static Pair<String,Integer> f(){
return new Pair<String,Integer>("hi",3);
}
public class Pair<String,Integer> {
public final String a;
public final Integer b;
public Pair(String a, Integer b) {
this.a = a;
this.b = b;
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码有什么错误?
abstract class Two {
Two() {
System.out.println("Two()");
}
Two(String s) {
System.out.println("Two(String");
}
abstract int display();
}
class One {
public Two two(String s) {
return new Two() {
public int display() {
System.out.println("display()");
return 1;
}
};
}
}
class Ajay {
public static void main(String ...strings ){
One one=new One();
Two two=one.two("ajay");
System.out.println(two.display());
}
}
Run Code Online (Sandbox Code Playgroud)
我们无法实例化一个抽象类,那么为什么函数 二二(String s)能够创建一个抽象类的实例Two ????
在Java中,所有类在第一次使用类时动态加载到JVM中.
这是否意味着如果我在我的源文件中有类并且我没有对它进行任何引用,那么它的Class对象不会被创建(即.class文件未被创建)?
在下面的示例代码中,iam没有引用test3类,但仍然创建了它的类对象.
class test1 {
static {
System.out.println("static block of test1");
}
}
class test2{
static {
System.out.println("static block of test2");
}
}
class test3 {}
class MyExample1 {
public static void main(String ...strings ) {
new test1();
new test2();
}
}
Run Code Online (Sandbox Code Playgroud)
为什么要test3.class创建文件?
谷歌分析代码必须放在HEAD标签之前.如果我的jsp文件中没有HEAD标记,谁能告诉我在哪里放ga代码?
我的节目正在抛出一个NullPointerException.我怀疑Calling the instance method of a null object 是它抛出空指针异常的原因.
任何人都可以解释这实际意味着什么?
我正在使用Spring Tool Suite构建 Spring Boot 应用程序并使用thymeleaf作为模板引擎。
文件夹结构如下:
src/main/resources/static
src/main/resources/templates
Run Code Online (Sandbox Code Playgroud)
HTML文件保存在src/main/resources/templates
,javacript文件保存在src/main/resources/static
为了将 javascript 文件包含在我的 html 中,我添加了以下行:
<script type="text/javascript" th:src="@{../static/js/policyCreations.js}"></script>
Run Code Online (Sandbox Code Playgroud)
但我在 chrome 控制台中收到以下错误:
获取http://localhost:8082/static/js/policyCreations.js网::ERR_ABORTED 404
谁能帮我解决这个问题
我使用以下链接来了解环境变量和系统属性。
https://docs.oracle.com/javase/tutorial/essential/environment/env.html
该链接表示环境变量由操作系统设置并传递给应用程序。当我使用它获取环境变量时,System.getenv()它向我显示了许多我从未设置过的属性。所以它一定是操作系统(我使用的是 macOS)设置了这些属性。其中的一些属性System.getenv()是MAVEN_CMD_LINE_ARGS、JAVA_MAIN_CLASS_1420、JAVA_MAIN_CLASS_1430。
我的问题是为什么操作系统要在环境变量中设置 java 特定属性?理想情况下,这些应该由 JVM 设置(在System.properties())。
PS:从我在网上读到的内容来看,我了解到环境变量是由操作系统设置的,而 System.properties() 是由 JVM 设置的
另外,如果有人可以向我指出有关环境变量和 System.properties 的良好链接,那将非常有帮助。我对两者感到非常困惑。
class MyException extends Exception {
MyException() {}
MyException(String msg) { super(msg);}
}
public class NewException {
static void f() throws MyException {
System.out.println("throwing exception from f()");
throw new ClassCastException();
}
static void g() throws MyException {
System.out.println("throwing exception from g()");
throw new MyException("parametrized ");
}
public static void main(String ...strings ) {
try {
f();
}
catch(MyException e) {
e.printStackTrace(System.out);
}
try {
g();
}
catch(MyException e) {
e.printStackTrace(System.out);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在函数f()中,我指定将抛出"MyException"异常,实际上我抛出了一些与MyException无关的其他异常,但编译器仍然没有抱怨.为什么?
java ×6
abstract ×1
class ×1
javac ×1
return-type ×1
spring ×1
spring-boot ×1
spring-mvc ×1
thymeleaf ×1
tuples ×1