10 Java Tips – Series I

Java Tips

10 Java Tips – 你必須知道,,en,Java中的內存管理,,en,引入接口是為了達到目的,它來自Objective C,,en,從Java語言中省略多重繼承的原因主要來源於,,en,作為一種簡單的語言,,en,Java的創建者需要一種大多數開發人員無需經過大量培訓即可掌握的語言,,en,沒有承擔C 不必要的複雜性,,en,在設計師,,en,意見,,en,多重繼承會導致比解決更多的問題和困惑,,en,因此,他們從語言中刪除了多個遺產,,en,正如他們削減操作員超載一樣,,en,設計師,,en,豐富的C 經驗告訴他們,多重遺傳並不值得頭疼,,en,Java的設計者選擇通過使用接口來允許多個接口繼承,,en

How memory is managed in java?

In Java manual memory allocation and de-allocation has been eliminated. Memory in Java is automatically garbage collected so you never have to worry about memory corruption. Java memory is managed by the memory model. The Java Memory model explains synchronization techniques to make sure data corruption does not take place. Synchronization can also help to avoid deadlocks between threads and run programs smoothly. The following diagram gives an overview on java memory management.

Memory management in Java

圖像 1: Memory management in Java

Are multiple inheritances possible in java?

No, multiple inheritance is not possible in java. Interface has been introduced to serve the purpose and it has been derived from Objective C.

The reasons for omitting multiple inheritances from the Java language mostly stem from the “simple, object oriented and familiar” goal. As a simple language, Java’s creators wanted a language that most developers could grasp without extensive training. To that end, they worked to make the language as similar to C as possible (familiar) without carrying over C ’s unnecessary complexity (simple).

In the designers’ opinion, multiple inheritance causes more problems and confusion than it solves. So they cut multiple inheritances from the language (just as they cut operator overloading). The designers’ extensive C experience taught them that multiple inheritances just weren’t worth the headache.

Instead, Java’s designers chose to allow multiple interface inheritance through the use of interfaces, 從Objective C協議中藉鑑的想法,,en,多接口繼承允許對象繼承許多不同的方法簽名,但需要注意的是繼承對象必須實現那些繼承的方法,,en,java的十一個設計目標是什麼?,,en,以下是java編程語言的11個設計目標,,en,Java設計者通過牢記這些目標完成了任務,,en,面向對象,,en,解讀,,en,分散式,,en,強大的,,en,多線程,,en,安全,,en,動態,,en,建築中立,,en,為什麼java被稱為分佈式,,en,Java有一個廣泛的例程庫,用於處理HTTP和FTP等TCP / IP協議,,en,因此,Java應用程序可以通過URL打開和訪問Internet上的對象,,en,使用java的網絡編程功能強大且易於使用,,en,所以java被稱為分佈式,,en. Multiple interface inheritance allows an object to inherit many different method signatures with the caveat that the inheriting object must implement those inherited methods.







What are the eleven design goals of java?

Following are the eleven design goals of java programming language. Java designers have accomplished the task by keeping these goals in mind.

  • Simple
  • 手提
  • Object Oriented
  • Interpreted
  • Distributed
  • High Performance
  • Robust
  • Multithreaded
  • Secure
  • Dynamic
  • Architecture Neutral

Why java is called distributed?

Java has an extensive library of routines for handling TCP/IP protocols like HTTP and FTP. So Java applications can open and access objects across the internet via URLs. The network programming using java is strong and easy to use. So java is called distributed.

Java的分佈式特性與其動態類加載功能相結合,確實令人眼前一亮,,en,這些功能使Java解釋器可以從Internet下載和運行代碼,,en,這是Web瀏覽器下載並運行Java applet時發生的情況,,en,場景可能比這更複雜,,en,想像一下用Java編寫的多媒體文字處理器,,en,當要求該程序顯示以前從未遇到過的某種類型的數據時,,en,它可以從網絡中動態下載一個可以解析數據的類,,en,然後動態下載另一個類,,en,可能是一個Java,,en,可以顯示複合文檔中的數據,,en,像這樣的程序使用網絡上的分佈式資源來動態增長並適應其用戶的需求,,en. Together, these features make it possible for a Java interpreter to download and run code from across the Internet. This is what happens when a Web browser downloads and runs a Java applet, 例如. Scenarios can be more complicated than this, however. Imagine a multi-media word processor written in Java. When this program is asked to display some type of data that it has never encountered before, it might dynamically download a class from the network that can parse the data, and then dynamically download another class (probably a Java “bean”) that can display the data within a compound document. A program like this uses distributed resources on the network to dynamically grow and adapt to the needs of its user.

為什麼java被稱為架構中立,,en,Java編譯器生成一個獨立於計算機體系結構的字節碼指令,,en,字節碼設計為易於在任何機器上解釋,並且可以在運行中輕鬆轉換為本機機器代碼,,en,下圖顯示了其工作原理的詳細信息,,en,Java架構中立,,en,為什麼運行applet需要支持java的瀏覽器,,en,Applet是一個在網頁上運行的java程序,,en,因此瀏覽器應該具有解釋字節碼並使其工作的能力,,en,這就是為什麼需要支持java的瀏覽器來運行嵌入在網頁中的applet,,en,實際上瀏覽器包含內置的JVM,它有助於解釋字節代碼並顯示輸出,,en?

The Java compiler generates a bytecode instruction which is independent of computer architecture. The bytecode is designed to be both easy to interpret on any machine and easily translated into native machine code on the fly. The following diagram shows the details how it works.

Java architecture neutral

圖像 2: Java architecture neutral

Why java enabled browser is needed to run an applet?

Applet is a java program that works on a web page. So the browser should have the capability to interpret the bytecode and make it work. That is why java enabled browser is required to run an applet embedded in a web page. Actually the browser contains the JVM inbuilt which helps to interpret the byte code and show the output.

你是什​​麼意思'Java是一種強類型語言',,en,Java強類型意味著java中的每個變量都必須具有聲明的類型,,en,Java中有八種原始類型,,en,其中四個是整數類型,兩個是浮點數類型,,en,一個是字符類型char,,en,用於Unicode編碼中的字符,一個是真值的布爾類型,,en,如何在java中管理I / O.,,en,在java中,I / O表示輸入和輸出流,,en,流用於讀取或寫入設備,如文件或網絡或控制台,,en,Java.io包提供了操作流的I / O類,,en,該包支持兩種類型的流,,en,處理二進制數據的二進制流和處理字符數據的字符流,,en?

Java is strongly typed means that every variable in java must have a declared type. There are eight primitive types in Java. Four of them are integer types and two are floating-point number types. One is the character type char, used for characters in the Unicode encoding and one is a Boolean type for truth values.







How I/O is managed in java?

In java I/O represents Input and Output streams. Streams are used to read from or write to devices such as file or network or console. Java.io package provides I/O classes to manipulate streams. This package supports two types of streams – binary streams which handle binary data and character streams which handle character data. InputStream和OutputStream是用於操作二進制流的高級接口,,en,Reader和Writer是用於處理字符流的高級接口,,en,下圖顯示了本節中介紹的不同IO類的關係,,en,不同的I / O類,,en,緩衝流如何提高java中的性能,,en,流的默認行為是一次讀取或寫入一個字節,,en,這會導致較差的I / O性能,因為在處理大量數據時,需要花費大量時間逐字節讀/寫,,en,Java I / O提供緩衝流來覆蓋這些逐字節的默認行為,,en,您需要使用緩衝流,,en,BufferedInputStream和BufferedOutputStream,,en,緩衝數據然後讀/寫,這提供了良好的性能,,en. Reader and Writer are high level interfaces for manipulating character streams.

The following figure shows the relationship of different IO classes addressed in this section

Different I/O classes

圖像 3: Different I/O classes

How does buffered stream improve performance in java?

The default behavior of a stream is to read or write one byte at a time. This causes poor I/O performance because it takes lot of time to read/write byte by byte when dealing with large amounts of data. Java I/O provides Buffered streams to override these byte by byte default behaviors. You need to use Buffered streams (BufferedInputStream and BufferedOutputStream) to buffer the data and then read/write which gives good performance. 您需要了解方法的默認行為並採取相應措施,,en,下圖顯示了緩衝流如何轉移數據流,,en,緩衝流,,en,如何衡量默認行為的性能和文件讀取的緩衝,,en,以下代碼片段將使用默認讀取和緩衝讀取來讀取和寫入類似的文件,,en,然後它將顯示兩者所花費的時間,,en,為了測試代碼,請將文件保存在本地文件系統中,如下所示,,en,緩衝文件讀取,,en,包com.performance.io,,en,公共課IOTest,,en,IOTest io =新的IOTest,,it,long startTime = System.currentTimeMillis,,en,io.readWrite,,en,溫度/測試origin.html,,en,溫度/測試destination.html,,en,long endTime = System.currentTimeMillis,,en,使用默認行為進行讀寫所需的時間,,en,時間結束,,en,開始時間,,en,毫秒,,en.

The following figure shows how buffered streams divert the data flow.

Buffered streams

圖像 4: Buffered streams

How do you measure performance of default behavior and buffering for file reading?

The following code snippet will take similar files to read and write using default reading and buffered reading. And then it will display the time taken by both of them. For testing the code keep the files in local file system as shown below.

Listing 1: Buffering for file reading

package com.performance.io;

進口java.io. *;

public class IOTest {

公共靜態無效的主要(串[] 參數){

IOTest io = new IOTest();

嘗試{

long startTime = System.currentTimeMillis();

io.readWrite(“Ç:/temp/test-origin.html”,”Ç:/temp/test-destination.html”);

long endTime = System.currentTimeMillis();

System.out.println(“Time taken for reading and writing using default behaviour : ”

+ (endTime – startTime) + ” milli seconds” );

long startTime1 = System.currentTimeMillis,,en,io.readWriteBuffer,,en,long endTime1 = System.currentTimeMillis,,en,使用緩衝流進行讀寫的時間,,en,endTime1,,en,startTime1,,sv,public static void readWrite,,en,字符串文件來自,,fy,字符串文件,,bg,InputStream in = null,,en,OutputStream out = null,,en,in = new FileInputStream,,en,fileFrom,,fy,out = new FileOutputStream,,en,fileTo,,el,int bytedata = in.read,,en,字節數據==,,sv,字節數據,,sv,最後,,en,附寄,,en,public static void readWriteBuffer,,en,InputStream inBuffer = null,,en,OutputStream outBuffer = null,,en,InputStream in = new FileInputStream,,en,inBuffer = new BufferedInputStream,,en,OutputStream out = new FileOutputStream,,en,outBuffer = new BufferedOutputStream,,en,int bytedata = inBuffer.read,,en,inBuffer,,en,inBuffer.close,,en,outBuffer,,en,outBuffer.close,,en,techalpine.com/10-java-tips-series-i,,en();

io.readWriteBuffer(“Ç:/temp/test-origin.html”,”Ç:/temp/test-destination.html”);

long endTime1 = System.currentTimeMillis();

System.out.println(“Time taken for reading and writing using buffered streams : ”

+ (endTime1 – startTime1) + ” milli seconds” );

}抓(IOException異常Ë){ e.printStackTrace();}

}

public static void readWrite(String fileFrom, String fileTo) 拋出IOException異常{

InputStream in = null;

OutputStream out = null;

嘗試{

in = new FileInputStream(fileFrom);

out = new FileOutputStream(fileTo);

while(真){

int bytedata = in.read();

如果(bytedata == -1)

break;

out.write(bytedata);

}

}

抓(例外五)

{

e.printStackTrace();

}

finally{

如果(in != NULL)

in.close();

如果(出 !=null)

out.close();

}

}

public static void readWriteBuffer(String fileFrom, String fileTo) 拋出IOException異常{

InputStream inBuffer = null;

OutputStream outBuffer = null;

嘗試{

InputStream in = new FileInputStream(fileFrom);

inBuffer = new BufferedInputStream(in);

OutputStream out = new FileOutputStream(fileTo);

outBuffer = new BufferedOutputStream(出);

while(真){

int bytedata = inBuffer.read();

如果(bytedata == -1)

break;

out.write(bytedata);

}

}

抓(例外五)

{

e.printStackTrace();

}

finally{

如果(inBuffer != NULL)

inBuffer.close();

如果(outBuffer !=null)

outBuffer.close();

}

}

)







============================================= ============================================== 在亞馬遜上購買最佳技術書籍,en,電工CT Chestnutelectric,en
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share