Home>

Although the Java language is a typical object-oriented programming language,But eight of the basic data types do not support object-oriented programming,Basic types of data don't have the characteristics of "objects"-they don't carry properties and they have no methods to call. They are only used to meet the deep-rooted habits of humans,And it is indeed simple and effective for routine data processing.

This approach with non-object-oriented technology can sometimes be inconvenient,For example, reference type data inherits the characteristics of the object class.To convert to the string type (which is often required), simply call tostring () defined in the object class, and converting the basic data type to the string type is much more troublesome.To solve such problems, java has designed corresponding classes for each basic data type.It is called wrapper classes, and there are also textbooks called overlay classes or data type classes.

Each wrapper object can encapsulate a corresponding basic type of data,It also provides some other useful methods.Once a wrapper object is created,Its content (the encapsulated primitive type data value) cannot be changed.

Basic types and corresponding packaging classes can be replaced with each other:

The conversion from the basic type to the corresponding packaging class is called boxing.For example, wrapping an int into an object of type integer;

The conversion of the packaging class to the corresponding basic type is called unboxing.For example, objects of the integer class are reduced to int.

Packaging applications

The use of the eight packaging classes is similar,The following are common application scenarios.

1) Convert between int and integer

You can box an int through the constructor of the integer class, and unbox the integer through the intvalue method of the integer class. E.g:

public class demo {
  public static void main (string [] args) {
    int m=500;
    integer obj=new integer (m);//manual boxing
    int n=obj.intvalue ();//manual unboxing
    system.out.println ("n =" + n);
    integer obj1=new integer (500);
    system.out.println ("obj is equivalent to obj1?" + obj.equals (obj1));
  }
}

operation result:

n=500
obj is equivalent to obj1?true

2) convert string to integer

The integer class has a static paseint () method that converts a string to an integer,The syntax is:

parseint (string s, int radix);

s is the string to be converted,radix is ​​base, optional, default is decimal.

The following code will tell you what kind of strings can be converted to integers:

public class demo {
  public static void main (string [] args) {
    string str []={"123", "123abc", "abc123", "abcxyz"};
    for (string str1:str) {
      try {
        int m=integer.parseint (str1, 10);
        system.out.println (str1 + "can be converted to an integer" + m);
      } catch (exception e) {
        system.out.println (str1 + "Cannot convert to integer");
      }
    }
  }
}

operation result:

123 can be converted to an integer 123
123abc cannot be converted to an integer
abc123 cannot be converted to an integer
abcxyz cannot be converted to an integer

3) convert integer to string

The integer class has a static tostring () method that converts an integer to a string.E.g:

public class demo {
  public static void main (string [] args) {
    int m=500;
    string s=integer.tostring (m);
    system.out.println ("s =" + s);
  }
}

operation result:

s=500

Automatic unboxing and boxing

The above examples all need to instantiate a wrapper class manually,This is called manual unboxing.Prior to java 1.5 (5.0) you had to unbox and box manually.

Automatically unboxing and boxing after java 1.5,That is, when converting the basic data type and the corresponding wrapper class,The system will do it automatically,This will greatly facilitate the programmer's code writing.E.g:

public class demo {
  public static void main (string [] args) {
    int m=500;
    integer obj=m;//autobox
    int n=obj;//automatic unboxing
    system.out.println ("n =" + n);
    integer obj1=500;
    system.out.println ("obj is equivalent to obj1?" + obj.equals (obj1));
  }
}

operation result:

n=500
obj is equivalent to obj1?true

Automatic unboxing and packing is a commonly used function.The reader needs to master it.

Not only can packages contain classes,It can also contain interfaces and other packages.

Directories use "\" to indicate hierarchical relationships,For example, e:\ java \ workspace \ demo \ bin \ p1 \ p2 \ test.java.

Packages use "." To indicate hierarchical relationships,For example, p1.p2.test represents the directory \ p1 \ p2 \ test.class.

How to implement the package

A package can be declared with the package keyword,E.g:

 package p1.p2;

The package statement must be placed before all statements,E.g:

package p1.p2;
public class test {
  public test () {
    system.out.println ("I am the constructor of the test class");
  }
}

Shows that the test class is in the p1.p2 package.

Package call

In java, there are two ways to call classes in other packages.

1) Add the full package name in front of each class name

Program example:

public class demo {
  public static void main (string [] args) {
    java.util.date today=new java.util.date ();
    system.out.println (today);
  }
}

operation result:

wed dec 03 11:20:13 cst 2014

2) Introduce the classes in the package through the import statement

Program example:

import java.util.date;
//You can also import all classes in the java.util package
//import java.util. *;
public class demo {
  public static void main (string [] args) {
    date today=new date ();
    system.out.println (today);
  }
}

The result is the same as above.

In actual programming,It is not necessary to write the classes to be introduced in such detail,You can directly import all the classes in a specific package,For example, import java.util. * ;.

Class path

java when importing classes,You must know the absolute path of the class.

First create demo.java in the e:\ java \ workspace \ demo \ src \ p0 \ directory (e:\ java \ workspace \ demo \ src \ is the root directory of the project source files), enter the following code:

package p0;
import p1.p2.test;
public class demo {
  public static void main (string [] args) {
    test obj=new test ();
  }
}

Create test.java in the e:\ java \ workspace \ demo \ src \ p1 \ p2 directory and enter the following code:

package p1.p2;
public class test {
  public test () {
    system.out.println ("I am the constructor of the test class");
  }
}

Suppose we set the classpath environment variable to.;D:\ program files \ jdk1.7.0_71 \ lib and the source file demo.java has the import p1.p2.test;statement at the beginning, then the compiler will first check e:\ java \ If the test.java or test.class file exists in the workspace \ demo \ src \ p0 \ p1 \ p2 \ directory, if it does not exist,Will continue to search the d:\ program files \ jdk1.7.0_71 \ lib \ p1 \ p2 \ directory, if neither directory exists, an error will be reported.Apparently, test.java is located in the e:\ java \ workspace \ demo \ src \ p1 \ p2 \ directory and the compiler cannot findI get an error, what should I do?

The classpath can be specified with the classpath option of the javac command.

Open cmd and go to the directory where the demo.java file is located.Execute the javac command and set the classpath to e:\ java \ workspace \ demo \ src, as shown in the following figure:

When running a java program, you also need to know the absolute path of the class.In addition to the path specified by the classpath environment variable,You can also increase the path through the classpath option of the java command,As shown below:

Note the difference between the java command and the javac command,To execute the javac command, you need to enter the current directory.To execute the java command, you need to enter the parent directory of the current directory.And the package name must be preceded by the package name.

Can be understood this way,javac is a platform command,It operates on specific platform files,To indicate the path of the file being compiled.And java is a virtual machine command,It operates on classes,That is, the description of the class should be in the form of point description.And ca n’t add extensions,Also note the case of the class name.

These commands are more complicated,Actual development requires eclipse. It is very convenient to manage packages and compile and run programs under eclipse.eclipse actually executes these commands as well.

Access to packages

A class, method, or member variable declared as public,Can be used by any class in any package,And a class, method, or member variable declared as private,Can only be used by this class.

Classes, methods, and member variables without any modifiers,Only accessible by all classes in this package,It cannot be accessed by any class outside the package.

  • Previous Explanation of Java's access modifiers and variable scope
  • Next Based on jQuery imitation Taobao product picture magnifier code sharing