Thứ Năm, 26 tháng 3, 2015

Comprehensive JAVA

This is a comprehensive introduction into JAVA. If you want to learn JAVA with me you have to work with the 3 following rules:

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
Update Jan.31/2015 for manual WINDOWS settings 
[​IMG]
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 (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;= (int) l;  // casting long to int: 64 bits to 32 bits= (bytec// casting char to byte: 16 bits to 8 bitsa// no casting because 64 bits is larger than 32 bits of an intb// 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 = (FileInputStreaminp// casting because inp is forefather of FileInputStream
(more HERE)
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... athrows 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$
Primitive Assignment must be adjusted accordingly to its type. Example:
PHP:
float f 123.0f;
The suffix "f" after 0 says "this constant is a float". Byte assignment is 0Xnn (X or x for Hex and nn is 2 hexadecimal digits). Note: As long as it's an ASCII code it's posible to do this:
PHP:
byte b0 'B'// ASCII Bbyte b1 0X42// ASCII of B
NO unsigned primitives. Primitives of JAVA are always signed. A byte is, for example, ranged beween -127 and +127.
- 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 ActionListenerMouseListener {
        ...
}
You may note that Exp2 includes 2 additional lines: "import java.io.*;" and "import javax.swing.*;". The reason is that JAVA is a semi-clone of C and C is fast because of its minimalism. Anything that is needed extra must be included. The keyword "#include". Also, JAVA is also a miminalist, too. In JAVA term "#include" is replaced by "import". Exp1 uses the Java IO package and Exp2 the event-package and the extension javax package. Some people who run IDE as their development editor get usually the imports of every object:
PHP:
import java.awt.event.MouseListener;import java.awt.event.ActionListener;import javax.swing.JFrame;
...
It looks quite professional because IDE generated them. If you have to write, you'll appreciate the short cut "import java.xyz.*;" And don't believe that "only that that's explicitly inported is imported. javac is smart enough to import during the compilation only objects it needed. Proof?
correct the Exp1 and run
Code:
javac -verbose -d ./classes -g:none Exp1.java
then you'll see which objects are imported.

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?




Dịch vụ kế toán thuế | Dịch vụ báo cáo thuế | Dịch vụ dọn dẹp sổ sách | Dịch vụ quyết toán thuế | dịch vụ hoàn thuế GTGT | dịch vụ báo cáo tài chính | Khóa học kế toán cho giám đốc

Không có nhận xét nào:

Đăng nhận xét

Advertise