A有一个Java代码:
public class Hello
{
public void print()
{
System.out.println("Hi");
}
}
Run Code Online (Sandbox Code Playgroud)
我编译了它并创建了一个Hello.class.我将它添加到Jar文件hello.jar中:
$jar -cvf hello.jar Hello.class
Run Code Online (Sandbox Code Playgroud)
我又写了一个程序:
class Test1
{
public static void main(String[] args)
{
new Hello().print();
System.out.println(Hello.class.getClassLoader());
}
}
Run Code Online (Sandbox Code Playgroud)
并从当前目录中删除了Hello.class.
然后我在扩展类路径中复制了hello.jar.我的程序工作正常:
$sudo cp hello.jar /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext
$ java Test1
Hi
sun.misc.Launcher$ExtClassLoader@28d93b30
$
Run Code Online (Sandbox Code Playgroud)
如果我从扩展类路径中删除hello.jar并将其复制到包含rt.jar的boot strap类路径(usr/lib/jvm/java-8-openjdk-amd64/jre/lib /)中,那么我的程序不是工作.
$ java Test1
Exception in thread "main" java.lang.NoClassDefFoundError: Hello
at Test1.main(Test1.java:5)
Caused by: java.lang.ClassNotFoundException: Hello
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
Run Code Online (Sandbox Code Playgroud)
根据我的知识,bootstrap或扩展类加载器中的所有jar文件都可以在程序中使用.为什么Java会抛出异常,如果它是正确的?如果我不对,请指导我.
我知道这是一个枚举
enum Year
{
First, Second, Third, Fourth;
}
Run Code Online (Sandbox Code Playgroud)
被转换成
final class Year extends Enum<Year>
{
public static final Year First = new Year();
public static final Year Second = new Year();
public static final Year Third = new Year();
public static final Year Fourth = new Year();
}
Run Code Online (Sandbox Code Playgroud)
当我尝试实例化枚举(不是类)时,我得到编译时错误:
error: enum types may not be instantiated
Year y = new Year();
Run Code Online (Sandbox Code Playgroud)
据我所知,私有构造函数使类不可实例化.我认为编译器提供了一个私有构造函数.但是当我看到我们可以使用默认修饰符为枚举定义构造函数时仍然无法创建枚举类型的对象时,我感到很困惑.
enum Year
{
First, Second, Third, Fourth;
Year()
{
}
}
class Example
{
public static void …Run Code Online (Sandbox Code Playgroud) LinkedHashMap.java使用了一个字段:
final boolean accessOrder;
Run Code Online (Sandbox Code Playgroud)
LinkedHashMap的构造函数是:
public LinkedHashMap(int initialCapacity,
float loadFactor,
boolean accessOrder) {
super(initialCapacity, loadFactor);
this.accessOrder = accessOrder;
}
Run Code Online (Sandbox Code Playgroud)
我想知道accessOrder字段的用途.请举例说明accessOrder是'true和'false'.有没有其他方法来更新已创建对象的accessOrder字段?
众所周知,谷歌有不止一个 IP 地址。如果我们使用https://toolbox.googleapps.com/apps/dig/#A/google.com@8.8.8.8网站,则会得到多个 google 的 IP 地址。
如果我运行以下命令,则场景不同:
gyan@localhost:~/codes/java/net$ dig google.com
; <<>> DiG 9.10.3-P4-Ubuntu <<>> google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 11777
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4000
;; QUESTION SECTION:
;google.com. IN A
;; ANSWER SECTION:
google.com. 269 IN A 216.58.220.46
;; Query time: 0 msec
;; SERVER: 10.100.171.1#53(10.100.171.1)
;; …Run Code Online (Sandbox Code Playgroud) 我使用 Java-8 编译器编译了以下代码:
package pack;
import sun.util.calendar.CalendarUtils;
public class A {
public static void main(String[] args) {
System.out.println(CalendarUtils.isGregorianLeapYear(2018));
}
}
Run Code Online (Sandbox Code Playgroud)
我使用 Java-8 编译器将上述代码编译为:
gyan@gyan-pc:~/codes/java$ ~/Documents/softwares/Linux/jdk1.8.0_131/bin/javac -d . a.java
a.java:2: warning: CalendarUtils is internal proprietary API and may be removed in a future release
import sun.util.calendar.CalendarUtils;
^
a.java:9: warning: CalendarUtils is internal proprietary API and may be removed in a future release
System.out.println(CalendarUtils.isGregorianLeapYear(2018));
^
2 warnings
Run Code Online (Sandbox Code Playgroud)
我的默认 Java 解释器的版本:
gyan@gyan-pc:~/codes/java$ java -version
java version "9.0.1"
Java(TM) SE Runtime Environment (build 9.0.1+11) …Run Code Online (Sandbox Code Playgroud) LinkedHashMap延伸HashMap.代码中put()不存在方法LinkedHashMap.java.所以我认为如果我可以put()在下面的程序中调用方法,那么它必须是从中继承的put()方法HashMap.
import java.util.*;
class First
{
public static void main(String[] args)
{
LinkedHashMap<Key, String> h=new LinkedHashMap<>(7);
h.put(new Key(3), "Hi");
h.put(new Key(1), "Hello");
h.put(new Key(9), "hru");
System.out.println(h);
}
}
Run Code Online (Sandbox Code Playgroud)
Key.java是:
class Key
{
int i = 0;
Key(int i)
{
this.i=i;
}
public int hashCode()
{
return i;
}
public String toString()
{
return i+"";
}
}
Run Code Online (Sandbox Code Playgroud)
它必须维护'after'和'before'引用以保留插入顺序:http://a.disquscdn.com/uploads/mediaembed/images/3751/7481/original.jpg
但是put()方法in HashMap不知道这些变量.那么如何调用put()维护这些变量呢? …
请参阅以下代码:
#include<stdio.h>
main(){
int pid, fds[2], pid1;
char buf[200];
pipe(fds);
pid = fork();
if(pid==0)
{
close(fds[0]);
scanf("%s", &buf);
write(fds[1], buf, sizeof(buf)+1);
}
else
{
pid1 = fork();
if(pid1==0)
{
close(fds[1]);
read(fds[0], buf, sizeof(buf)+1);
printf("%s\n", buf);
}
else
{
Line1: wait();
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我不评论Line1,它工作正常.请看下面:
hduser@pc4:~/codes/c/os$ ./a.out
hello //*Entry from keyboard*
hello //Output
hduser@pc4:~/codes/c/os$
Run Code Online (Sandbox Code Playgroud)
但是,如果我注释掉Line1,则两个子进程无法通信:
hduser@pc4:~/codes/c/os$ ./a.out
hduser@pc4:~/codes/c/os$
hi //*Entry from keyboard*
hi: command not found
hduser@pc4:~/codes/c/os$
Run Code Online (Sandbox Code Playgroud)
我无法理解wait()的重要性.
我们将用户定义的类Employee的反射用作:
Employee e = new Employee();
Class c = e.getClass();
Run Code Online (Sandbox Code Playgroud)
根据我的知识,第一个JVM加载类Employee的字节码,然后它还为每个加载的类(此处为Employee类)创建一个Class.class对象.在Class.class的对象中,JVM存储有关最近加载的类的元信息.
类的元信息是"方法名称","字段名称"等.这些类型的类,如"Method","Field"等,在java.lang.reflect包中定义.
我看到了Class.java的代码.我在Class.class中找到了返回这些类型的对象或对象数组的方法,如"Method","Field"等.但Class.class中没有一个字段,其类型为"Method","Field"等.
如果我的上述陈述有误,请认真对待.如果上述陈述没有错,那么我有以下疑问:1).在Class.class的哪个字段中存储有关类的各种信息?2).Employee的JVM对象的内存区域和Class.class的对象存储在哪里?3).Employee的JVM字节码的存储区和Class.class的字节码存储在哪里?
一个字节不能容纳来自世界上各种语言的字符的 unicode。所以使用字节数组我们不能有不同语言的字符串。为什么这些类使用字节数组而不是字符数组?
更新:
class First
{
public static void main(String[] args)
{
System.out.println();
String s = "\u0935\u0902\u0926\u0947 \u092e\u093e\u0924\u0930\u092e\u094d";
String s1 = "???? ??????";
System.out.println(sb);
System.out.println(sb1);
}
}
Run Code Online (Sandbox Code Playgroud)
我认为上面的字符串每个字符需要两个字节。它们如何可以容纳在一个字节中?
我有两个 Java 文件:
检查.java
import java.util.concurrent.Semaphore;
class Check
{
public static void main(String[] args)
{
Semaphore s = new Semaphore(1);
MyThread t1 = new MyThread("t1",s);
MyThread t2 = new MyThread("t2",s);
t1.start();
t2.start();
t2.interrupt();
}
}
Run Code Online (Sandbox Code Playgroud)
MyThread.java
import java.util.concurrent.Semaphore;
class MyThread extends Thread
{
Semaphore s;
MyThread(String name, Semaphore s)
{
super(name);
this.s=s;
}
public void run()
{
try
{
s.acquireUninterruptibly();
for(int i=0; i<5; i++)
{
Thread.sleep(500);
System.out.println(Thread.currentThread().getName()+"-"+i);
}
s.release();
}
catch(InterruptedException e){}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我注释掉语句“t2.interrupt()”,那么两个线程都可以正常执行。但如果我不评论该语句,那么线程 t2 根本不会执行。根据我对 acquireUninterruptically() 方法的理解,线程 t2 …
java ×8
collections ×2
java-9 ×2
jvm ×2
c ×1
class ×1
classloader ×1
constructor ×1
dig ×1
dns ×1
enums ×1
ipc ×1
jar ×1
java-module ×1
linux ×1
networking ×1
nslookup ×1
pipe ×1
reflection ×1
semaphore ×1
string ×1
stringbuffer ×1