Java learning Summary (1) --> JAVA environment variable configuration, DOS window with package compilation, and two new features of jdk1.5 (variable parameters and enhanced for loops)

Source: Internet
Author: User

1. Program compilation and running

Because the machine only recognizes the 0 and 1 command sequences and cannot directly read the source code, because the source code is a string sequence composed of character texts, the compiler needs to compile the source code into code commands that can be recognized by the machine.

In Java, the Java source program can be compiled and run in both windows and other operating systems. The difference is that the compiler (JVM Virtual Machine) is different. Sun has developed different virtual machines for different operating systems. The same source code is compiled in different types of JVM to obtain the same bytecode. The generation of bytecode is only related to JVM. The so-called JDK version upgrade means that JVM is being upgraded.

2. configuration of Java environment variables (3 files) --> previously, only configuration is available, and the specific functions are unclear.

Classpath = .;

The purpose of classpath configuration is to find the source program to be compiled (that is, the class) and the source file to be compiled no matter which directory the source program is located.

Java_home = E: \ Java \ jdk1.7.0)

Some netizens said it was dispensable to facilitate JDK switching of different versions, but I didn't need java_home during installation. I found the JDK directory directly in path and there was a configuration problem, the DOS window prompts that javac is not an internal command. After java_home is added, the compilation is normal. (It is not clear where the problem occurs)

Path = % java_home % \ bin;

The purpose of configuring path is to find the JDK installation location, and then find the javac and Java tools for compiling and running the source program.

3. Some gains of running and compiling in the DOS window (the focus is on running the classes with packages)

The compiling method is the same with or without a package:

Javac-D path *. Java (it indicates compiling all Java source programs in the current directory to the path directory. The path can be a vertex or a directory under a drive letter)

Such as compiling and running (package com. sunzone. Office hello. Java source program)

Compile: javac-D. Hello. Java

Run: Java com. sunzone. Office. Hello or Java COM/Sun/zone/office/Hello

5. The first new feature of jdk1.5: variable parameters

Variable parameters mean that the number of parameters received by a method is not fixed.

Variable parameters have the following features:

(1) variable parameters can only appear at the end of the parameter list.

(2 )... It is located between the variable type and the variable name, and can be left with or without spaces.

(3) When a variable parameter method is called, the compiler will implicitly create an array for the variable parameter and access the parameter in the method body as an array.

Eg:

Public static int add (int x, int... ARGs) {// sum

Int sum = X;

For (INT I = 0; I <args. lengh; I ++ ){

Sum + = ARGs [I];

}

Return sum;

}

6. jdk1.5's second new feature: enhanced for Loop

For (type variable name: Set variable name ){.....}

Note:

Iteration variables must be defined in ()

Collection variables can be arrays or collection classes that implement the iterable interface.

Eg: use an enhanced for loop to modify the summation method.

Public static int add (int x, int... ARGs) {// sum

Int sum = X;

For (INT Arg: ARGs) {// pay special attention to the nature of Arg. Arg is actually an element in the ARGs array.

Sum + = ARG;

}

Return sum;

}

7. Differences between overload and override

Overriding and overloading are different manifestations of Java polymorphism.

Overwriting Overloading is a manifestation of the polymorphism between the parent class and the subclass. If the method defined in the subclass has the same name and parameter as the parent class, this method is overwritten. Features: the return value type, Method Name, number of parameters, and parameter type are identical. When the subclass object uses this method, the definition in the subclass is called. The definition in the parent class is blocked. You can only rewrite non-private methods of the parent class.

Overload Overloading is a manifestation of polymorphism in a class. If multiple methods with the same name are defined in a class, they are called method overloading by using different numbers of parameters or different parameter types. Overloading

You can change the type of the returned value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.