Java中的編譯器類
什麼是Java中的編譯器類?
原生程式碼是一種可以在Java虛擬機器中執行的程式碼形式。編譯器類提供支援,併為我們將Java程式碼轉換為原生程式碼提供空間。這是一個公共包,嵌入在Java環境中的java.lang.Compiler.command()包中。
Java中編譯器類的示例
這是一個Java中編譯器類的示例:
class of the NewClass$CompilerClass Name Value Is: null value Is the compilation successful here? : false value Is the compilation successful using str here? : false value
在Java中使用編譯器類的演算法
在這個可能的演算法中,我們將向您展示如何執行編譯器類,該類為從Java程式碼到原生程式碼提供支援和服務。透過使用此演算法,我們將構建一些Java語法來有效地解釋該過程。
步驟1 - 開始程序。
步驟2 - 匯入並宣告一些Java包以執行程序。
步驟3 - 宣告一個公共類。
步驟4 - 提及一個字串引數。
步驟5 - 提及並呼叫編譯器類函式類。
步驟6 - 使用enable()方法。
步驟7 - 為編譯器方法宣告一個類。
步驟8 - 使用print命令。
步驟9 - 檢查子類是否存在。
步驟10 - 檢查布林值。
步驟11 - 獲取列印值。
步驟13 - 使用desable停止方法。
步驟14 - 以編譯器類的形式宣告一個私有類。
步驟15 - 終止程序。
在Java中使用編譯器類的語法
protected void assertRun(String mainClass) {
try {
List<String> args = new ArrayList<>();
args.add(jvm);
args.add("-cp");
args.add(".:" + System.getProperty("java.class.path"));
args.add(mainClass);
log.debug("command: " + StringUtils.join(args.toArray(new String[]{}), "
"));
Compiler Class In Java
ProcessBuilder pb = new ProcessBuilder(args);
pb.redirectErrorStream(true);
pb.directory(getClassOutputDir());
Process p = pb.start();
jvmOutput = IOUtils.toString(p.getInputStream());
int exitCode = p.waitFor();
log.debug(jvm + " exit code: " + exitCode);
log.debug(jvm + " console output: " + jvmOutput);
assertEquals(jvmOutput, 0, exitCode);
} catch (IOException ex) {
throw new RuntimeException(ex);
} catch (InterruptedException ex) {
throw new Error(ex);
}
}
在上述可能的語法中,我們嘗試向您展示如何在特定過程中宣告和建立編譯器類方法。透過使用這種特殊的語法,我們將朝著與問題陳述相關的某些可能的方法前進,以有效的方式。
遵循的方法
方法1 - 演示編譯器類方法用法的Java程式
方法2 - 透過使用各種Long類方法和位計數來演示編譯器類的Java程式
方法1:編譯器類方法的用法
編譯器擴充套件物件方法的用法
在這種可能的方法中,我們將應用Java編譯器類方法,如果編譯成功,該方法將返回true值。但它也可能丟擲NullPointerException。
public final class Compiler extends Object public static boolean command(Object argument) public static boolean compileClass(Class c) public static void enable() public static void disable() public static boolean compileClasses(String string)
示例
//Java Program illustrating the use of Compiler class Methods.
import java.lang.*;
public class ARBRDD {
public static void main(String[] args){
CompilerClass geek = new CompilerClass();
Compiler.enable();
Class c = geek.getClass();
System.out.println(c);
Object g = Compiler.command("javac CompilerClass Has Been Declared");
System.out.println("Value Of The Process Is : " + g);
boolean check = Compiler.compileClass(c);
System.out.println( "Is the particular compilation successful here? : " + check);
String str = "CompilerClass";
boolean check1 = Compiler.compileClasses(str);
System.out.println( "Is the particular compilation successful using str here ? : " + check1);
Compiler.disable();
}
private static class CompilerClass {
public CompilerClass() {}
}
}
輸出
class ARBRDD$CompilerClass Value Of The Process Is : null Is the particular compilation successful here? : false Is the particular compilation successful using str here ? : false
方法2:Long類和位計數方法的用法
Long類方法的用法
在這種可能的方法中,我們將應用Long物件來表示與系統屬性關聯的值。如果該值在程序中不存在,則它可以返回null值。
public Long(long b) public Long(String s)throws NumberFormatException public String toString(long b) public byte byteValue() public static int bitCount(long i) public static int numberofLeadingZeroes(long i)
示例
//Java program to illustrate compiler class by using various Long class methods
public class Long_test{
public static void main(String args[]){
long b = 55;
String bb = "45";
Long x = new Long(b);
Long y = new Long(bb);
System.out.println("toString(b) = " + Long.toString(b));
System.out.println("toHexString(b) =" + Long.toHexString(b));
System.out.println("toOctalString(b) =" + Long.toOctalString(b));
System.out.println("toBinaryString(b) =" + Long.toBinaryString(b));
Long z = Long.valueOf(b);
System.out.println("valueOf(b) = " + z);
z = Long.valueOf(bb);
System.out.println("ValueOf(bb) = " + z);
z = Long.valueOf(bb, 6);
System.out.println("ValueOf(bb,6) = " + z);
long zz = Long.parseLong(bb);
System.out.println("parseLong(bb) = " + zz);
zz = Long.parseLong(bb, 6);
System.out.println("parseLong(bb,6) = " + zz);
long prop = Long.getLong("sun.arch.data.model");
System.out.println("getLong(sun.arch.data.model) = " + prop);
System.out.println("getLong(abcd) =" + Long.getLong("abcd"));
System.out.println("getLong(abcd,10) =" + Long.getLong("abcd", 10));
String decimal = "45";
String octal = "005";
String hex = "0x0f";
Long dec = Long.decode(decimal);
System.out.println("decode(45) = " + dec);
dec = Long.decode(octal);
System.out.println("decode(005) = " + dec);
dec = Long.decode(hex);
System.out.println("decode(0x0f) = " + dec);
long valrot = 2;
System.out.println("rotateLeft(0000 0000 0000 0010 , 2) =" + Long.rotateLeft(valrot, 2));
System.out.println("rotateRight(0000 0000 0000 0010,3) =" + Long.rotateRight(valrot, 3));
}
}
輸出
toString(b) = 55 toHexString(b) =37 toOctalString(b) =67 toBinaryString(b) =110111 valueOf(b) = 55 ValueOf(bb) = 45 ValueOf(bb,6) = 29 parseLong(bb) = 45 parseLong(bb,6) = 29 getLong(sun.arch.data.model) value = 64 getLong(abcd) = null getLong(abcd,10) =10 decode(45) = 45 decode(005) = 5 decode(0x0f) = 15 rotateLeft(0000 0000 0000 0010 , 2) =8 rotateRight(0000 0000 0000 0010,3) =4611686018427387904
位計數方法的用法
在這種可能的方法中,我們將為編譯器類應用highestOneBit()和LowestOneBit()來獲取單個一位的值。它還會返回一個最後一位為1的較高和較低的值。例如:
如果數字的值為0000 0000 0000 1111,則程序返回的值為0000 0000 0000 0001。
示例
//Java program to illustrate compiler class by using various Long class methods by using bit count
public class ARBRDD{
public static void main(String args[]){
long b = 55;
String bb = "45";
Long x = new Long(b);
Long y = new Long(bb);
System.out.println("bytevalue(x) = " + x.byteValue());
System.out.println("shortvalue(x) = " + x.shortValue());
System.out.println("intvalue(x) = " + x.intValue());
System.out.println("longvalue(x) = " + x.longValue());
System.out.println("doublevalue(x) = " + x.doubleValue());
System.out.println("floatvalue(x) = " + x.floatValue());
long value = 45;
System.out.println("Long.bitcount(value)=" + Long.bitCount(value));
System.out.println("Long.numberOfTrailingZeros(value)=" +
Long.numberOfTrailingZeros(value));
System.out.println("Long.numberOfLeadingZeros(value)=" +
Long.numberOfLeadingZeros(value));
System.out.println("Long.highestOneBit(value)=" +
Long.highestOneBit(value));
System.out.println("Long.lowestOneBit(value)=" +
Long.lowestOneBit(value));
System.out.println("Long.reverse(value)=" + Long.reverse(value));
System.out.println("Long.reverseBytes(value)=" +
Long.reverseBytes(value));
System.out.println("Long.signum(value)=" + Long.signum(value));
int hash = x.hashCode();
System.out.println("hashcode(x) = " + hash);
boolean eq = x.equals(y);
System.out.println("x.equals(y) = " + eq);
int e = Long.compare(x, y);
System.out.println("compare(x,y) = " + e);
int f = x.compareTo(y);
System.out.println("x.compareTo(y) = " + f);
}
}
輸出
ytevalue(x) = 55 shortvalue(x) = 55 intvalue(x) = 55 longvalue(x) = 55 doublevalue(x) = 55.0 floatvalue(x) = 55.0 Long.bitcount(value)=4 Long.numberOfTrailingZeros(value)=0 Long.numberOfLeadingZeros(value)=58 Long.highestOneBit(value)=32 Long.lowestOneBit(value)=1 Long.reverse(value)=-5476377146882523136 Long.reverseBytes(value)=3242591731706757120 Long.signum(value)=1 hashcode(x) = 55 x.equals(y) = false value compare(x,y) = 1 x.compareTo(y) = 1
結論
Java編譯器類是一個後端程式碼容器,它將位元組碼儲存在Java虛擬機器中。javac方法將原始碼轉換為已編譯的類方法,並使用特定的類副檔名重新命名原始檔。在今天的文章中,我們學習了Java環境中編譯器類的運作過程。透過上述語法和演算法,我們構建了一些Java程式碼來有效地解決問題陳述。
另請閱讀: Java面試問題及答案
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP