1) Write codes only with an editor (I don't care what it is: Notepad, jedit or any IDE)
2) Windows CMD or LINUX terminal to test the codes
3) Setup a JAVA working environment as following:
Code:
WINDOWS: a) Get System Control Panel and set the variables: - PATH: %PATH%;.;(path of jdk-bin directory) - CLASSPATH: %CLASSPATH%;.\classes;(path of jdk-lib directory) b) create a new directory congdongjava and subdirectory classes. Example: - d:\congdongjava - d:\congdongjava\classes LINUX: a) open the .profile and add the variables: - PATH: $PATH:. - CLASSPATH: $CLASSPATH:./classes - make sure that JDK or OpenJDK is installed properly b) create in your home directory a subdirectory "congdongjava", get into it and create the subdirectory classes
![[IMG]](http://s25.postimg.org/59yerrvjj/set_On_Window.jpg)
The PATH and CLASSPATH are the most basic things JAVA requires and every newbie must know and understands how they work.
- PATH, as it name says, is the track, the route that leads to the actual component, the actual object you work with. PATH is an OS-Variable
- CLASSPATH, as its name says, is the track, the street that leads to the actual JAVA class you work with. CLASSPATH is a JAVA variable.
Besides, every newbie must be familiar with:
- java: The invocation of Java Virtual Machine (JVM). On Windows the javaw when JVM should work in background and on all Linux & (ampersand) is used to tell JVM how it behaves (e.g. java helloworld &)
- javac: This is the JAVA compiler. javac translates a JAVA plain codes (source) into binary values (byte-codes) so that JVM could understand.
- jar: This is the so-called Java ARchiver. jar is used to compress the java-related components to save not only space (similar to ZIP or RAR), but also ready for delivery (RAR stands for Roshal ARchiver where Roshal is the name of the developer).
-----------------------------------------------------------------------------------------------------------------
JAVA
Object Oriented Programming is the art to describe an issue, a problem, an event as an object and to provide a solution as an object with a conventional 3GL programming language. In this case, JAVA runs a "deviation" of C (developed by AT&T Bell). Therefore JAVA shares all C keywords, but differs slightly from C in some functionalities:
- Type-strong: Any conversion between 2 objects must be usually cast (C: no = type-weak). Example:
PHP:
int a;long l = 100;char c = 'C';byte b;a = (int) l; // casting long to int: 64 bits to 32 bitsb = (byte) c; // casting char to byte: 16 bits to 8 bitsl = a; // no casting because 64 bits is larger than 32 bits of an intc = b; // no casting because 16 bits is bigger than 8 bits of a byteInputStream inp;FileInputStream fis = new FileInputStream("text.txt");inp = fis; // NO casting because FileInputStream is an offspring of InputStreamfis = (FileInputStream) inp; // casting because inp is forefather of FileInputStream
If you don't know the "casting" rule javac will break off and complains like this:
PHP:
import java.io.*;
public class Exp1 {
public static void main(String... a) throws Exception {
InputStream inp;
FileInputStream fis = new FileInputStream("Exp1.java");
inp = fis;
fis = inp; // <<--missiing casting<<--ERROR
}
}
Code:
erika@erika:/media/Data/test$ javac -g:none -d ./classes test.java test.java:7: error: incompatible types fis = inp; ^ required: FileInputStream found: InputStream 1 error erika@erika:/media/Data/test$
PHP:
float f = 123.0f;
PHP:
byte b0 = 'B'; // ASCII Bbyte b1 = 0X42; // ASCII of B
- NO multiple inheritance of different objects. If an inheritance of more than 2 different objects is needed the keywords "inplements" is used. Example:
PHP:
import java.awt.event.*;import javax.swing.*;
public class Exp2 extends JFrame implements ActionListener, MouseListener {
...
}
PHP:
import java.awt.event.MouseListener;import java.awt.event.ActionListener;import javax.swing.JFrame;
...
correct the Exp1 and run
Code:
javac -verbose -d ./classes -g:none Exp1.java
Excercise:
1) What happens with double d = 0.123f?
2) Why a double must be cast to a float?
3) If B is an offspring of A what happens if B is assigned to A?
Không có nhận xét nào:
Đăng nhận xét