The first lesson from C ++ to Java

Source: Internet
Author: User

Sequence: the quickest way to learn computer languages is to map one language to another. Java and C ++ are the most common advanced face object languages. They are extremely similar in terms of syntax and programming and can be imitated, examples here include syntax comprehension and programming specifications. This article focuses on the Java language.

Java Development Environment Configuration

1. Install JDK

Version: jdk1.6.0 _ 27

Configure user variables:

A. Create java_home

C: \ Program Files (x86) \ Java \ jdk1.6.0 _ 27 (JDK installation path)

B. Create a path

% Java_home % \ bin; % java_home % \ JRE \ bin

C. Create a classpath

.; % Java_home % \ Lib \ tools. Jar

After the configuration is complete, run the Java command in cmd to check whether the configuration is successful.

2. Install eclipse

Version: eclipse-jee-juno-win32

Decompress the package to the installation path.

Encapsulation

Encapsulation is the most basic concept of a surface object. Its significance is to ensure that some data is not disturbed by the outside world. The same is true in C ++ -- class. It is important to precisely define the data access permissions (public, private, and protect ). But in Java, there is a concept that is higher than the class-package. The class solves the abstraction of Data Objects and operation methods, while the package encapsulates the business logic. Class is more important for Java, because the entry to the Java program is a class, and the main function exists in the class.

Inheritance and Polymorphism

These two concepts are basically the same as C ++.

Program differences

This is the biggest difference between the two languages: C ++ isCompilation typeLanguage, while Java isInterpreted typeLanguage.

The javac compiler compiles the. Java file into the example. Class file, which contains the program's bytecode, And the bytecode contains the instruction code to be executed by the Java interpreter. Therefore, class cannot be run and must be explained using the Java interpreter. For example, Java example.

Why must the file name of a Java program have the same name as its public class?

After the Java program is compiled, each separate class is put into its own. in the class file, running the Java interpreter actually specifies the name of the class to be run, and it will automatically search for the class containing this name. class file. Different names cannot be found.

In addition, the Java compiler can compile a class without the main function, but the interpreter must have the main function.

Data Type

Java is more rigorous than C ++ and has higher requirements on data types. For example, Java cannot assign float values to int. Incompatible types must be forcibly converted.

The boolean type of Java is Boolean, and the value is true or false.

Java cannot declare the same variable nested, which is allowed in C ++.

Int x = 1;

{

Int x = 2;

}

Java arrays are slightly different, and Java arrays need to be dynamically allocated.

Int month [];

Month = new int [12]; // 12 elements are allocated, and each element is initialized to 0.

Or

Int [] month = new int [12];

Java provides array boundary detection, which is not available in C ++. If it is out of the range, the following exception occurs:

Java. Lang. arrayindexoutofboundsexception

Java does not have the sizeof function, and the data types have the same length on all machines.

String

A string is a type in C ++, but in Java, it is not a type but an object. This is true in many web languages. For example, in Javascript, arrays are also an object!

The biggest difference

Java has no pointer concept! Why? Image firewall for small applications.

Eclipse coding quick Layout

Shortcut: Ctrl + Shift + F

Java label usage principles

(1) A general continue will return to the beginning (top) of the innermost loop and continue execution.

(2) The continue with tags will arrive at the tag location and re-enter the loop next to the tag.

(3) generally, the break is interrupted and jumps out of the current loop.

(4) a break with a label will interrupt and jump out of the cycle indicated by the label.

Next to this

Point to this class Object

Static

The static method does not use this method. In the "static method", "non-static method" 3 cannot be called, but in turn can be called. In addition, you can call static methods only through the class itself without creating any objects. This is actually the main reason for the existence of static methods. It is like a global function in C. Global functions are not allowed in Java, but you can access other static methods and static fields by setting static methods in the class.

Class static variables

Static variables of a class are the same member variables shared by all objects of the class.

Class static method

The static method uses the public memory space, that is, all objects can be directly referenced. You do not need to create an object before using this method.

Resource release

1. Objects may not be recycled.

2. Garbage collection is not equal to "analysis structure ".

3. Garbage collection is only related to memory.

Final

The final modifier in Java is similar to the const variable in C ++.

Function

Random Number Function

Import java. util .*;

Int I = Rand. nextint (100); // take the integer that falls within 100.

The output of math. Random () includes 0.0. in mathematical language, the output value range is [0, 1)

Comparison functions = and equals ()

The equals operation indicates whether the two variables are referenced to the same object, that is, whether the content in the heap is the same.
= Compare the addresses of two objects, while equals compares the content of two objects.

Class

Implements interface implementation

Implements is a key word used by a class to implement an interface. It is used to implement the abstract methods defined in the interface. For example, people is an interface with the say method.
Public interface people ()
{
Public say ();
}
However, the interface does not have a method body.
Only one specific class can be used to implement the method body.
For example, the Chinese class implements the people interface.
Public class Chinese implements extends El {
Public say ()
{System. Out. println ("Hello! ");}
}

Package-New Concept of Java

Package-Library Unit

Import packagename;

For example, the utility Library released by Java

Import java. util .*;

In this way, all classes under this package can be used.

Note that all naming rules for Java packages use lower-case letters, including intermediate characters.

The statement for declaring a package is as follows:

Package mypackage;

To use the class of the package, you must first import the package.

When creating your own package, you will find that the package statement must be the first line of uncommented program code in the file.

If both Package A and Package B have the class1 class name, a prefix is required.

A. class1 OBJ = new A. class1 ();

With the Package concept, we can customize our own library.

Access permission again

Public, protected, and private

Differences:

C ++ can continue the previous permission settings, and Java must add the permission Declaration in each defined place.

The usage of the three permissions is basically the same as that of C ++.

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.