Java study notes

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

1 set Java runtime environment

1.Windows:

To set environment variables, JAVAHOME, CLASSPATH, after the success of the PATH., test whether the correct on the command line:

image

2.Linux:

Modify/etc/profile

JAVA_HOME=/opt/jdk1.6.0_06
CLASSPATH=$JAVA_HOME/lib:$CLASSPATH
PATH=$JAVA_HOME/bin:$PATH
EXPORT JAVA_HOME CLASSPATH PATH

2. eclipse

Eclipse is written using Java, so the need for the Java runtime environment. Windows, if JDK installation option to install "public JRE", then eclipse can automatically find the Java runtime environment, you need to configure the JDK environment in environment variables.

3 compiler

The following is a HelloWorld.java code:

package com.gr.java;
public class HelloWorld{
    public static void main(String[] args){
        System.out.println("Hello world");
    }
}

Running under CMD:

javac –d . HelloWorld.java,This follows in accordance with the package directory, com/gr/java/ directory, in.

image

 

If the direct input javac HelloWorld.java, it will generate a HelloWorld.class with HelloWorld.java in the same directory, we need the directory structure generated bytecode files into our own organization, as follows:

image

4 dozen jar package

The jar package.Class files can be compressed package for unified management, packetized command are as follows:

image

Will generate hello.jar in the current directory, as follows:

image

 

Add hello.jar to CLASSPATH,

Windows:

set CLASSPATH=%CLASSPATH%;C\USERS\GR\hello.jar;

Linux:

set CLASSPATH=$CLASSPATH:/home/gr/hello.jar

Thus,.Class documents can be packaged into a jar file, the Java com.gr.java.HelloWorld can be executed.

image

In the specified jar package for.Class file:

image

 

5 Java allows only one public class appears in a file.

The wrapper class 6 in Java and its container packing

The basic types of data is not an object, you want to use the class, must be converted to object types.

byte – Byte  short – Short  int – Integer  long - Long

float - Float  double – Double

char – Character

bool – Boolean

See the following code:

		Integer a = 100;
		Integer b = 100;
		Integer c = 200;
		Integer d = 200;
		log.info(a == b);
		log.info(a == 100);
		log.info(c == d);
		log.info(c == 200);

Using the = = Integer objects is object references are the same, but not its magnitude is equal to. > <= =, between the Integer container handling, judge the value.

1,3 is the comparison should be different object is false, 2,4 is more should value for true,

We view the results:

image

2,4 on the A, C devanning conversion comparison as the basic data type, should be true.

1,3 as the object of comparison, 1 true, 3 false, there were two different results.

In fact, the definition in a, B, C, D, Integer a = 100; equal to the following statement:

Integer a = Integer.valueOf(100);

We will have a look this function valueOf how to achieve the specific. The following is the code in JDK:

    public static Integer valueOf(int i) {
        if(i >= -128 && i <= IntegerCache.high)
            return IntegerCache.cache[i + 128];
        else
            return new Integer(i);
    }
    private static class IntegerCache {
        static final int high = 127;
        static final Integer cache[];

        static {
            final int low = -128;
            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k <cache.length; k++)
                cache[k] = new Integer(j++);
        }

        private IntegerCache() {}
    }

We can see from the code, JDK in order to speed up the pace, the integer data between -128 to 127 of the cache, thus, leads to the initialization time, a, B made a reference to the same object. So the comparison when the emergence of equal, if not in this range, there is no such a situation. This is further proof, = = compares reference object.

7 Java a%b, B allows a decimal, C language requirements must be an integer. B is negative, the result is positive. A is negative, the result is negative.

8 + + problem

int a = 1;
a = a++;
System.out.println(a);

The output for the 1, the first implementation of a++ expression was 1, after the a increment is 2, then a++ the result of the expression is assigned to a, so the a final results for 1

9. equals(),hashCode()

equals()Methods generally used to compare the contents of the object are equal, the general need to cover.

hashCode()Value should be stable, an object to create value should not change. Two objects equals compared to true, should have the same hashCode (value).

10 when the constructor does not write class, javac will provide a parameterless constructor declaration default constructor, if provided, javac will not provide a default constructor.

Parameters of 11 Java methods for the transfer of only one form, based on the value transfer, transfer the value of the variable.

1 basic types of replication is value

2 reference type is a reference value (address) of replication.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Lucinda at November 18, 2013 - 12:33 PM