10 Java Tips – Series I

Java Tips

10 Java Tips – Devi sapere,,en,Gestione della memoria in Java,,en,L'interfaccia è stata introdotta per servire allo scopo ed è stata derivata dall'obiettivo C,,en,Le ragioni per omettere più ereditarietà dal linguaggio Java derivano principalmente dal,,en,Come un linguaggio semplice,,en,I creatori di Java volevano un linguaggio che la maggior parte degli sviluppatori potesse cogliere senza un addestramento approfondito,,en,senza trascinare la complessità inutile del C ,,en,Nei designer,,en,opinione,,en,l'ereditarietà multipla causa più problemi e confusione di quanti ne risolva,,en,Quindi hanno tagliato più eredità dalla lingua,,en,proprio come hanno ridotto il sovraccarico dell'operatore,,en,I designer,,en,una vasta esperienza in C ha insegnato loro che le eredità multiple non valevano proprio il mal di testa,,en,I progettisti di Java hanno scelto di consentire l'ereditarietà di più interfacce attraverso l'uso di interfacce,,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

Image 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, un'idea presa in prestito dai protocolli dell'Obiettivo C.,,en,L'ereditarietà dell'interfaccia multipla consente ad un oggetto di ereditare molte diverse firme di metodo con l'avvertenza che l'oggetto ereditario deve implementare quei metodi ereditati,,en,Quali sono gli undici obiettivi di design di java,,en,Di seguito sono riportati gli undici obiettivi di progettazione del linguaggio di programmazione java,,en,I progettisti Java hanno raggiunto questo obiettivo tenendo presenti questi obiettivi,,en,Orientato agli oggetti,,en,Interpretato,,en,distribuito,,en,robusto,,en,multithread,,en,Sicuro,,en,Dinamico,,en,Architettura neutrale,,en,Perché java è chiamato distribuito,,en,Java ha una vasta libreria di routine per gestire protocolli TCP / IP come HTTP e FTP,,en,Quindi le applicazioni Java possono aprire e accedere agli oggetti attraverso Internet tramite URL,,en,La programmazione di rete usando java è forte e facile da usare,,en,Quindi java è chiamato distribuito,,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
  • Portable
  • 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.

La natura distribuita di Java brilla davvero quando combinata con le sue capacità di caricamento dinamico della classe,,en,queste funzionalità consentono a un interprete Java di scaricare ed eseguire codice da Internet,,en,Questo è ciò che accade quando un browser Web scarica ed esegue un'applet Java,,en,Gli scenari possono essere più complicati di così,,en,Immagina un word processor multi-media scritto in Java,,en,Quando viene chiesto a questo programma di visualizzare alcuni tipi di dati che non ha mai incontrato prima,,en,potrebbe scaricare dinamicamente una classe dalla rete in grado di analizzare i dati,,en,e quindi scarica dinamicamente un'altra classe,,en,probabilmente un Java,,en,in grado di visualizzare i dati all'interno di un documento composto,,en,Un programma come questo utilizza risorse distribuite sulla rete per crescere e adattarsi dinamicamente alle esigenze del proprio utente,,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, per esempio. 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 “fagiolo”) 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.

Perché java si chiama architettura neutrale,,en,Il compilatore Java genera un'istruzione bytecode che è indipendente dall'architettura del computer,,en,Il bytecode è progettato per essere facilmente interpretabile su qualsiasi macchina e facilmente tradotto in codice macchina nativo al volo,,en,Il seguente diagramma mostra i dettagli su come funziona,,en,Architettura Java neutrale,,en,Perché è necessario il browser abilitato per Java per eseguire un'applet,,en,Applet è un programma java che funziona su una pagina web,,en,Quindi il browser dovrebbe avere la capacità di interpretare il bytecode e farlo funzionare,,en,Ecco perché è richiesto il browser abilitato per Java per eseguire un'applet incorporata in una pagina web,,en,In realtà il browser contiene la JVM integrata che aiuta a interpretare il codice byte e a mostrare l'output,,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

Image 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.

Cosa intendi con 'Java è un linguaggio fortemente tipizzato',,en,Java è fortemente tipizzato significa che ogni variabile in java deve avere un tipo dichiarato,,en,Ci sono otto tipi primitivi in ​​Java,,en,Quattro di loro sono tipi interi e due sono tipi di numeri in virgola mobile,,en,Uno è il tipo di carattere char,,en,usato per i caratteri nella codifica Unicode e uno è un tipo booleano per i valori di verità,,en,Come I / O è gestito in java,,en,In Java I / O rappresenta i flussi di input e output,,en,Gli stream vengono utilizzati per leggere o scrivere su dispositivi come file o rete o console,,en,Il pacchetto Java.io fornisce classi I / O per manipolare i flussi,,en,Questo pacchetto supporta due tipi di flussi,,en,flussi binari che gestiscono dati binari e flussi di caratteri che gestiscono i dati dei caratteri,,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 e OutputStream sono interfacce di alto livello per la manipolazione di flussi binari,,en,Reader e Writer sono interfacce di alto livello per la manipolazione dei flussi di caratteri,,en,La figura seguente mostra la relazione tra le diverse classi di IO affrontate in questa sezione,,en,In che modo lo stream bufferizzato migliora le prestazioni in java,,en,Il comportamento predefinito di un flusso è leggere o scrivere un byte alla volta,,en,Ciò causa una scarsa prestazione di I / O perché richiede molto tempo per leggere / scrivere byte per byte quando si gestiscono grandi quantità di dati,,en,L'I / O Java fornisce gli stream bufferizzati per sovrascrivere questi comportamenti di byte per byte predefiniti,,en,Devi usare i flussi bufferizzati,,en,BufferedInputStream e BufferedOutputStream,,en,bufferizzare i dati e quindi leggere / scrivere che offre buone prestazioni,,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

Image 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. È necessario comprendere il comportamento predefinito del metodo e agire in tal senso,,en,La seguente figura mostra come gli stream bufferizzati deviano il flusso di dati,,en,Flussi bufferizzati,,en,Come si misurano le prestazioni del comportamento predefinito e il buffering per la lettura dei file,,en,Il seguente frammento di codice prenderà file simili per leggere e scrivere utilizzando la lettura predefinita e la lettura bufferizzata,,en,E poi mostrerà il tempo impiegato da entrambi,,en,Per testare il codice, mantenere i file nel file system locale come mostrato di seguito,,en,Buffering per la lettura dei file,,en,pacchetto com.performance.io,,en,classe pubblica IOTest,,en,IOTest io = new IOTest,,it,long startTime = System.currentTimeMillis,,en,io.readWrite,,en,temp / test-origin.html,,en,temp / test-destination.html,,en,long endTime = System.currentTimeMillis,,en,Tempo impiegato per leggere e scrivere utilizzando il comportamento predefinito,,en,fine del tempo,,en,ora di inizio,,en,milli secondi,,en.

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

Buffered streams

Image 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;

import java.io.*;

public class IOTest {

public static void main(Stringa[] args){

IOTest io = new IOTest();

provare{

long startTime = System.currentTimeMillis();

io.readWrite(“c:/temp/test-origin.html”,”c:/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,Tempo impiegato per leggere e scrivere utilizzando i flussi bufferizzati,,en,endTime1,,en,startTime1,,sv,pubblico static void readWrite,,en,String fileFrom,,fy,String file To,,bg,InputStream in = null,,en,OutputStream out = null,,en,in = nuovo FileInputStream,,en,fileFrom,,fy,out = nuovo FileOutputStream,,en,fileto,,el,int bytedata = in.read,,en,dati byte ==,,sv,byte di dati,,sv,finalmente,,en,in.close,,en,pubblico static void readWriteBuffer,,en,InputStream inBuffer = null,,en,OutputStream outBuffer = null,,en,InputStream in = new FileInputStream,,en,inBuffer = new BufferedInputStream,,en,OutputStream out = nuovo 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(“c:/temp/test-origin.html”,”c:/temp/test-destination.html”);

long endTime1 = System.currentTimeMillis();

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

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

}prendere(IOException e){ e.printStackTrace();}

}

public static void readWrite(String fileFrom, String fileTo) throws IOException{

InputStream in = null;

OutputStream out = null;

provare{

in = new FileInputStream(fileFrom);

out = new FileOutputStream(fileTo);

mentre(vero){

int bytedata = in.read();

se(bytedata == -1)

break;

out.write(bytedata);

}

}

prendere(Exception e)

{

e.printStackTrace();

}

finally{

se(in != null)

in.close();

se(fuori !=null)

out.close();

}

}

public static void readWriteBuffer(String fileFrom, String fileTo) throws IOException{

InputStream inBuffer = null;

OutputStream outBuffer = null;

provare{

InputStream in = new FileInputStream(fileFrom);

inBuffer = new BufferedInputStream(in);

OutputStream out = new FileOutputStream(fileTo);

outBuffer = new BufferedOutputStream(fuori);

mentre(vero){

int bytedata = inBuffer.read();

se(bytedata == -1)

break;

out.write(bytedata);

}

}

prendere(Exception e)

{

e.printStackTrace();

}

finally{

se(inBuffer != null)

inBuffer.close();

se(outBuffer !=null)

outBuffer.close();

}

}

)







============================================= ============================================== Acquista i migliori libri di tecnologia su Amazon,en,ELETTRICI CT COSTRALETTRICO,en
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share