Notes from C ++ to Java (I)

Source: Internet
Author: User
Document directory
  • Package access permission
  • Protected permission
  • Java object Creation
1. Compile and run Java

The Package concept in Java is equivalent to the namespace concept in C ++. However, Java maps the package name to the directory structure in the file system, that is, if you declare the name of a package as COM. tij. everythingisobj, you must create a directory structure such as COM/tij/everythingisobj in your file system. All Java source code of this package will be placed in this directory! The reason for this is to facilitate JVM loading and running Java code. You can load *. Java compiled *. Class Based on package and classpath.

For example, everyobject. Java:

Package com. tij. everythingisobj;

Public class everyobject {

Public static void main (string [] ARGs ){

}
}

 

Javac COM/tij/everythingisobj/everyobject. Java

First, use javac to compile the *. Java file and generate *. Class. You can package these *. classes into a jar package, but you must keep the complete path name.

 

Java com. tij. everythingisobj. everyobject

After generating *. class, you can use the Java command in the command line to tell JVM which class to load and run it. At this time, JVM will search for the class you want to run according to the path set in classpath. If your classpath isC :/;./Then, JVM searches for the everyobject class in the C:/COM/tij/everythingisobj and./COM/tij/everythingisobj directories, and loads its static main function to run. You can also add-CP to the Java command to specify classpath.

Java-CP./everyobj. Jar com. tij. everythingisobj. everyobject

If you want to run the *. jar file, you must include your *. jar file in the-CP option. Open *. jar and you will see the complete path name. The directory is located at the west of *. Class. It is similar to running on a file system.

 

2. Java package

All *. Java must belong to a certain package. A package can have n. java files, and each. Java file can have only one public class. The class name must be the same as that of. java. *. Java files of the same package are stored in the same directory of the file system. If the package name is not specified in *. Java, *. Java belongs to the default package in its directory. Generally, the default package is not used.

 

2. Java permission package access permission

Java has the following permissions: public, protected, package access, and private. A stranger is package access. When a class or class member does not have any permission qualifier, the permission of this class or class member is package access. That is to say, it can only be accessed in the same package, but cannot be accessed in different packages.

 

For example, a staticobject class is defined in package com. tij. everythingisobj. If a packageaccess () function has no permission description, it is the package access permission.

 

 

 

In the same package (COM. tij. everythingisobj), The staticobject object can access packageaccess

In different packages, sobj objects cannot access the packageaccess function, because this function is only visible in COM. tij. everythingisobj package.

 

 

 

Protected permission

The protected permission provides the package access permission. However, when inheritance is performed, subclasses of different packages need to access some methods in the parent class and these methods do not need to be exposed to users. Therefore, the introduction requires the protected permission, because package access can only be accessed in the same package.Because protected has the package access permission, all protected members in the same package can ask questions, which is different from C ++.You can add the protected permission to the packageaccess function in the previous example. The result is the same. It can be accessed in the same package, but not in different packages.

 

3. create and destroy Java objects

Java cannot create non-primitive objects on the local stack. Any object must be created on the stack through new and must be followed by a pair of parentheses after the object name, new object (), c ++ is not required. Java automatically maintains the reference counter of the created object. When the counter is 0, the object may be automatically recycled by GC, so JAVA does not have the delete operator. When an object is created, if you want to call another constructor In the constructor, you must useThis (argument ....)To call!

Before calling the constructor, all static objects in the class are initialized first. In addition to static objects, Java also has static code blocks represented by static {}. The code in braces will also be initialized first, and these static objects will only be initialized once. Both static and non-static objects can be initialized when they are declared, or even functions can be called for initialization.

In Java, the initialization sequence is static members-> non-static members-> Constructor

Print the code

In S ()
In F ()
Static_object ()

 

 

Java object destruction

Java objects are destroyed by GC. When GC finds that the memory is insufficient, some objects with reference count of 0 will be released. The finalize function of the object will be called before release! However, programmers cannot rely on finalize, because GC may not release any objects until the program ends.

 

Java Miscellaneous

Every class in Java can have the main function for convenient testing.

Java = and! = The operator is used to compare references. Therefore, even if the content of the two objects is the same, the two objects are not equal! If you want to compare whether the content is the same, you need to call equals. The default equals operation is reference rather than content, so you need to override this function to compare the content yourself!

Import static com. tij. * introduces static members of the class. From the perspective of C/C ++, it is equivalent to introducing some "Global" functions.

>>> Unsigned right shift. no matter whether the operation object is signed or not, the value of 0 is always added to the high position.

 

For (int I: array) is equivalent to foeach, enumerating all elements of array.

The Declaration of the array is int [] array or Int array []. The array has a built-in variable, array. length to specify the length of the array. When creating an array, You can initialize int [] array = new int [] {1, 2, 4 };
For Enum meat {pig, dog, cat}, you must add meat. Pig When referencing it. In addition to the switch statement, you can directly use pig.

 

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.