Although the Java language is a typical object-oriented programming language, eight of these basic data types do not support object-oriented programming, and basic types of data do not have "object" attributes--no attributes, no methods to call. They are used only to cater to the ingrained habits of the human race, and indeed to make routine data processing simple and effective.
This use of object-oriented technology can sometimes be inconvenient, such as the reference type data inherit the attributes of the object class, to be converted to a String type (often required) as long as a simple call to the definition of ToString () in the object class, and the base data type is converted to The String type is much more cumbersome. To solve this problem, Java designed the corresponding class for each basic data type, called the wrapper class (wrapper Classes), also has the teaching material called the outer covering class or the data type class.
Objects of each wrapper class can encapsulate a corresponding base type of data and provide other useful methods. Once the wrapper class object is created, its contents (the encapsulated base type data value) are immutable.
Basic types and corresponding wrapper classes can be exchanged for each other:
Conversion from the base type to the corresponding wrapper class is called boxing, for example, an object that wraps an int into an Integer class;
The wrapper class translates to the corresponding base type, which is called a unboxing, such as simplifying the object of the Integer class to int.
application of packing class
The use of eight wrapper classes is similar, and the following is a common application scenario.
1) To achieve the conversion between int and Integer
int can be boxed by the construction method of the integer class, and the integer is disassembled by the Intvalue method of the integer class. For example:
public class Demo {public
static void Main (string[] args) {
int m =;
Integer obj = new integer (m); Manual boxing
int n = obj.intvalue ();//Manual unboxing
System.out.println ("n =" + N);
Integer obj1 = new Integer (.
) System.out.println ("obj equivalent to obj1?") "+ obj.equals (obj1));
}
Run Result:
n = +
obj equivalent to obj1? True
2 Converts a string to an integer
The integer class has a static Paseint () method that converts a string to an integer, and the syntax is:
parseint (String s, int radix);
S is the string to be converted, radix is the system, optional, and the default is decimal.
The following code will tell you what 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, ten);
System.out.println (str1 + "can be converted to integer" + m);
} catch (Exception e) {
System.out.println (str1 + "cannot be converted to an integer");}}
Run 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 Converts an integer to a string
The integer class has a static toString () method that converts an integer to a string. For example:
public class Demo {public
static void Main (string[] args) {
int m =;
String s = integer.tostring (m);
System.out.println ("s =" + s);
}
}
Run Result:
Automatic unpacking and packing
The above example needs to manually instantiate a wrapper class, called a manual unboxing box. Java 1.5 (5.0) must be manually unboxing boxes before.
Java 1.5 can automatically unboxing boxing, that is, the basic data types and corresponding packaging class conversion, the system will automatically, which will greatly facilitate the programmer's code writing. For example:
public class Demo {public
static void Main (string[] args) {
int m =;
Integer obj = m; Automatic boxing
int n = obj;//Automatic unboxing
System.out.println ("n =" + N);
Integer obj1 =;
System.out.println ("obj equivalent to obj1?") "+ obj.equals (obj1));
}
Run Result:
n = +
obj equivalent to obj1? True
Automatic unboxing of boxes is a commonly used function that readers need to focus on mastering.
A package can contain not only classes, but also interfaces and other packages.
The table of contents is represented by "\" for hierarchical relationships, such as E:\Java\workspace\Demo\bin\p1\p2\Test.java.
Package with "." To represent a hierarchical relationship, for example, the directory represented by P1.p2.Test is \p1\p2\test.class.
How to implement a package
You can declare a package by using the package keyword, for example:
You must place the package statement before all statements, for example:
Package p1.p2;
public class Test {public
test () {
System.out.println ("I am the constructor for the Test class");
}
Indicates that the Test class is in the P1.P2 package.
the call to the package
In Java, there are two different ways to call classes in other packages.
1 Add the complete package name before each class name
Program Examples:
public class Demo {public
static void Main (string[] args) {
java.util.Date today=new java.util.Date ();
System.out.println (today);
}
Run Result:
Wed Dec 11:20:13 CST 2014
2 Introducing the class in the package through the import statement
Program Examples:
Import java.util.Date;
You can also introduce all the classes
//import java.util.* in the Java.util package;
public class Demo {public
static void Main (string[] args) {
date today=new date ();
System.out.println (today);
}
The run results are the same as above.
In actual programming, it is not necessary to write the classes to be introduced so detailed that you can directly introduce all the classes in a particular package, such as import java.util.*;
the path of the class
Java when importing a class, you must know the absolute path to the class.
First create the Demo.java under the E:\Java\workspace\Demo\src\p0\ directory (E:\Java\workspace\Demo\src\ is the root of the project source file) and enter the following code:
Package p0;
Import P1.p2.Test;
public class demo{public
static void Main (string[] args) {
Test obj = new Test ();
}
Then create the 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 for the Test class");
}
Let's say we set the CLASSPATH environment variable to.;D: \ Program Files\jdk1.7.0_71\lib, source file Demo.java beginning with import p1.p2.Test; Statement, the compiler checks to see if a Test.java or Test.class file exists in the E:\Java\workspace\Demo\src\p0\p1\p2\ directory and, if not, continues to retrieve D:\Program files\ Jdk1.7.0_71\lib\p1\p2\ directory, two directories do not exist will be an error. Obviously, Test.java is located in the E:\Java\workspace\Demo\src\p1\p2\ directory, the compiler can not find, will be an error, how to do?
You can specify the classpath by using the Javac command's classpath option.
Open cmd, enter 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 illustration:
When running a Java program, you also need to know the absolute path to the class, in addition to the path specified by the CLASSPATH environment variable, you can add the path through the CLASSPATH option of the Java command, as shown in the following illustration:
Note the difference between the Java command and the Javac command, the execution of the Javac command needs to go into the current directory, and the Execute Java command needs to go to the parent directory of the current directory, and the class name should be preceded by a package name.
To understand this, Javac is a platform command that operates on specific platform files to indicate the path of the file being compiled. And Java is a virtual machine command, which operates on classes, that is, the description of the class to use a dotted description, and can not add extensions, but also note the case of the class name.
These commands are more complex, and actual development requires eclipse, which is very handy for managing packages and compiling programs in Eclipse. Eclipse is actually executing these commands.
Access Permissions for packages
A class, method, or member variable declared as public can be used by any class under any package, whereas a class, method, or member variable declared private can only be used by this class.
The
classes, methods, and member variables that do not have any modifiers are accessible only to all classes in this package, and cannot be accessed by any class other than the package.