Although the Java language is a typical object-oriented programming language, the eight basic data types do not support object-oriented programming. Basic data does not have the characteristics of "object" - it does not carry properties and has no methods to call. They are used only to cater to the deeply rooted habits of human beings and can indeed perform routine data processing simply and effectively.
This practice of using non-object-oriented technology can sometimes cause inconvenience. For example, all reference type data inherits the characteristics of the Object class. To convert it to String type (often there is a need), just simply call the defined in the Object class. ToString() is enough, while converting the basic data type to String type is much more troublesome. To solve this problem, Java designs corresponding classes for each basic data type, called Wrapper Classes, and textbooks are also called overlay classes or data type classes.
Each wrapper object can encapsulate a corresponding basic type of data and provide some other useful methods. Once a wrapper object is created, its content (the encapsulated basic type data value) cannot be changed.
The basic types and corresponding packaging classes can be replaced with each other:
Conversion from basic types to corresponding packaging classes is called boxing, such as wrapping int into an object of the Integer class;
The conversion of the wrapper class to the corresponding basic type is called unboxing, for example, simplifying the objects of the Integer class into ints again.
Packaging applications
The use of eight packaging classes is relatively similar, and the following are common application scenarios.
1) Implement the mutual conversion between int and Integer
Integer can be boxed through the Integer class constructor and Integer is unboxed through the Integer class intValue method. For example:
public class Demo { public static void main(String[] args) { int m = 500; Integer obj = new Integer(m); // Manual boxing int n = obj.intValue(); // Manual boxing System . out.println("n = " + n); Integer obj1 = new Integer(500); System.out.println("obj is equivalent to obj1?" + obj.equals(obj1)); }}
Running results:
n = 500obj is equivalent to obj1? true
2) Convert string to integer
The Integer class has a static paseInt() method that converts strings into integers, with the syntax:
parseInt(String s, int radix);
s is the string to be converted, radix is in binary, optional, and defaults to decimal.
The following code will tell you what kind of string 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{ in t m = Integer.parseInt(str1, 10); System.out.println(str1 + "can be converted to integer" + m); }catch(Exception e){ System.out.println(str1 + "cannot be converted to integer") ; } } }}
Running results:
123 Can be converted to integer 123123abc Cannot be converted to integer abc123 Cannot be converted to integer abcxyz Cannot be converted to integer
3) Convert integers to strings
The Integer class has a static toString() method that converts integers into strings. For example:
public class Demo { public static void main(String[] args) { int m = 500; String s = Integer.toString(m); System.out.println("s = " + s); }}
Running results:
s = 500
Automatic unboxing and packing
All the above examples require manual instantiation of a wrapper class, called manual unboxing. Java 1.5(5.0) must be manually unboxed.
After Java 1.5, it can be automatically unboxed, that is, when converting basic data types and corresponding packaging classes, the system will automatically perform, which will greatly facilitate programmers' code writing. For example:
public class Demo { public static void main(String[] args) { int m = 500; Integer obj = m; // Automatic boxing int n = obj; // Automatic unboxing System.out.println("n = " + n); Integer obj1 = 500; System.out.println("obj is equivalent to obj1?" + obj.equals(obj1)); }}
Running results:
n = 500obj is equivalent to obj1? true
Automatic unboxing and packing is a commonly used function, and readers need to focus on it.
Packages can not only contain classes, but also interfaces and other packages.
Directory uses "/" to represent hierarchical relationships, such as E:/Java/workspace/Demo/bin/p1/p2/Test.java.
The package uses "." to represent hierarchical relationships. For example, the directory represented by p1.p2.Test is /p1/p2/Test.class.
How to implement packages
A package can be declared through the package keyword, for example:
package p1.p2;
The package statement must be placed before all statements, for example:
package p1.p2;public class Test { public Test(){ System.out.println("I am the constructor of the Test class"); }}
Indicates that the Test class is located in the p1.p2 package.
Package call
In Java, there are two ways to call classes in other packages.
1) Add the complete package name before 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); }}
Running results:
Wed Dec 03 11:20:13 CST 2014
2) Introduce the class 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 run result is the same as above.
In actual programming, there is no need to write the class to be introduced in such detail. You can directly introduce all classes in a specific package, such as import java.util.*;.
Class path
When importing a class, 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 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 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 there is an import p1.p2.Test; statement at the beginning of the source file Demo.java, then the compiler will check E:/Java/ Whether the Test.java or Test.class file exists in the workspace/Demo/src/p0/p1/p2/ directory. If it does not exist, the D:/Program Files/jdk1.7.0_71/lib/p1/p2/ directory will continue to be retrieved. , if there is no existence in both directories, an error will be reported. Obviously, Test.java is located in the E:/Java/workspace/Demo/src/p1/p2/ directory. If the compiler cannot find it, it will report an error. What should I do?
The classpath can be specified through the classpath option of the javac command.
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 figure below:
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 add the path through the classpath option of the java command, as shown in the figure below:
Note the difference between java commands and javac commands. Executing javac commands requires entering the current directory, while executing java commands requires entering the upper directory of the current directory, and the class name must be included with the package name in front of it.
It can be understood in this way that javac is a platform command that operates on specific platform files and indicates the compiled file path. Java is a virtual machine command that uses dotted description form for class operations, that is, the description of the class must be described in dotted form, and cannot add extensions, and pay attention to the case of the class name.
These commands are quite complicated, and actual development requires the help of Eclipse, which is very convenient to manage packages and compile and run programs under Eclipse. Eclipse actually executes these commands.
Access permissions for packages
Classes, methods or member variables declared as public can be used by any class under any package, while classes, methods or member variables declared as private can only be used by this class.
Classes, methods, and member variables without any modifiers can only be accessed by all classes in this package, and no class outside the package can access it.