Java Advanced Features Utility class

Source: Internet
Author: User
Tags comparable wrapper

enum enum

An enumeration is a class, which is a reference data type

Enumeration classes that define multiple enumeration values based on requirements,

once the enumeration value is defined, it is not allowed to be changed, Static Constants

01. Define the sex private property of the gender type in the Student class:

Private Gender sex;

02. Create an enumeration of gender

public enum gender{

man ("male"), women ("female");    

Private String sex;

Private Gender (String sex) {
This.sex=sex;
}

Public String Getsex () {
return sex;
}

public void Setsex (String sex) {
This.sex = sex;
}

}

03. Calling in the test class

stu.setsex (Gender.man)

String sex = Stu.getsex (). Getsex ();
SYSTEM.OUT.PRINTLN (sex);//console Output---> Male

1. We are using a package that does not fundamentally prohibit the user's input

2. Use enumerations to fundamentally limit the user's input

3. All enumeration values are static constants that can be enumerated by the class. Enumeration values

4. The constructor method in the enumeration class must be private

Wrapper classes for basic data types

1. The generic,<> in the collection does not allow the base data type to be present, and the wrapper class can

2. Define a variable with a basic data type, and the variable name can point out something

3. The basic data type cannot be converted to an object, and the wrapper class can

4. All packaging classes are final decorated and are not allowed to be inherited

5. Use wrapper classes when basic data types need to be converted to objects

After 6.jdk1.5, the basic data types and wrapper classes are allowed to be mixed, and the bottom has boxing and unboxing operations

basic data type     wrapper class
byte          byte
short         short
int          integer
long          long
float         float
double        double


Char Character
Boolean Boolean

Both of the above are implements Java.io.Serializable, comparable<t>

For example:
Public Serializable Getnum (Serializable s) {
}

We call Getnum (the argument can be any of 8 wrapper classes)
The return value can also be any one of 8 wrapper classes

Boxing and unpacking---> Conversion of wrapper classes and basic data types

01. Boxing: Convert the basic data type to the corresponding wrapper class integer num=1

02. Unpacking: Converting the wrapper class to the corresponding basic data data type int Num2=num

Basic Data type conversions:

01. Automatic Type Conversion

02. Forcing type Conversions

Reference data type conversions

01. Down Conversion

02. Upward Conversion

/**
* 1. All packaging classes have corresponding basic data types as parameters to construct their own instances
*/
@SuppressWarnings ("unused")
@Test
public void Test01 () {
BYTE b=new byte ((Byte) 1);
Short S=new Short ((short) 1);
Integer i=new integer (1);
Long L=new long (1);
Float f=new Float (1.0);
Double D=new double (1.0);

Character c=new Character (' 1 ');
Character c2=new Character ((char) 20);
Boolean Bo=new Boolean (true);
}
/**
* 1.Float There are three ways to instantiate the method parameters are double float and string
* 2. In addition to the character 7 packaging classes have a string as a parameter to build their own instances
* 6 Types of wrapper classes inherit number
* So when you use string as a parameter to create your own instance
* Throws NumberFormatException if the argument cannot be converted to a value
*/
@SuppressWarnings ("unused")
@Test
public void Test02 () {
BYTE b=new byte ("1");
Short S=new Short ("1");
Integer i=new Integer ("1");
Long L=new Long ("1");
Float f=new Float ("1");
Double D=new Double ("1");

Character c=new Character ("1");
Boolean Bo=new Boolean ("1");
Boolean Bo2=new Boolean (true);
System.out.println (bo+ "\ n" +bo2);
}
/**
* 1. PARSEXXX (String s) in all 7 packaging classes except character
* For example, byte b=new byte ("1");
* B.parsebyte (String);
* 01.4 types of integer corresponding wrapper classes are parsexxx (String s,int radix) radix binary conversion
* 02.4 Other types without parsexxx (String s,int Radix)
* 03.Character no parsexxx ()
*/
@SuppressWarnings ("unused")
@Test
public void Test03 () {
BYTE b=new byte ("1");
Short S=new Short ("1");
Integer i=new Integer ("1");
Long L=new Long ("1");

Float f=new Float ("1");
Double D=new Double ("1");
Character c=new Character (' 1 ');
Boolean Bo=new Boolean ("true");

System.out.println (BO);
}
/**
* 4. Binary conversion requires learning bit arithmetic
*/
@Test
public void Test04 () {
System.out.println ("2 binary 10 corresponding data----->" +integer.tobinarystring (10));
SYSTEM.OUT.PRINTLN ("8 binary 10 corresponding data----->" +integer.tooctalstring (10));
SYSTEM.OUT.PRINTLN ("16 binary 10 corresponding data----->" +integer.tohexstring (10));
}
/**
* 5.valueOf
* Convert basic data types to corresponding packaging----> boxing
* Xxxvalue 8 package types are available.
* Convert XXX type to corresponding basic data type---> Unboxing
*/
@Test
public void Test05 () {
int num=3;
Integer i=integer.valueof (num);
Num=i.intvalue ();
}
/**
* 6. Classic Face Test
* because integer.valueof (num) caches data between -128~127
* If our data is in this range, do not go back to create new objects, but instead get them from the cache
* Otherwise it will be new Integer ()
*/
@Test
public void Test06 () {
int num1=127;
int num2=127;
System.out.println (NUM1==NUM2);
Integer a1=new integer (127);
Integer b1=new integer (127);
System.out.println (A1==B1);
Integer a=127;
Integer b=127;
System.out.println (A==B);
Integer c=128;
Integer d=128;
System.out.println (C==d);
System.out.println ("1" +1+1);
}

/**
* 111
* 211
*/

@Test
public void Test07 () {
System.out.println ("1" +1+1);
System.out.println (1+1+ "1" + 1);
}

Math
01. is an arithmetic class
02. Final Retouching
03. All methods except the construction method are static methods, which are convenient for us to use

Ceiling function:
Ceil Day up 3.1 = = "4 3.0 = =" 3
Floor down 3.9 = = "3

Rounded
Round (2.5) ==>3
Round (2.498) ==>2

Random
A random Boolean value

Java Advanced Features Utility class

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.