one , enumeration

1. Meaning of enumeration

An enumeration is a type that consists of a fixed set of constants , The type keyword of the enumeration is enum

2. Use of enumerations

Define enumeration class

packageenumtdome;

// use enum Keyword creation public enumGender {

// Direct value setting in enumeration class No data type is required

one,two,three,four,five,six,seven

}

Application of enumeration

packageenumtdome;importjava.util.Scanner;public classtest {

// Instantiate enumeration class

Gender ger;public static voidmain(String[] args) {

test test=newtest();

// Fill in the enumeration type

test.name(Gender.one);

}public voidname(Gender day) {switch(day) {caseone:

System.out.println(1);break;casetwo:

System.out.println(2);break;casethree:

System.out.println(3);break;casefour:

System.out.println(4);break;casefive:

System.out.println(5);break;casesix:

System.out.println(6);break;caseseven:

System.out.println(7);break;

}

}

}

two , Entity classes and API

1.API

Commonly used API Bao you :ava.lang (Enum, Packaging ,Math,String,StringBuffer,System),java.util( Tool class )
java.io( Input / output stream ) java.sql( database )

Api It can be compared to a dictionary According to class name Find within document

2. Packaging ( Entity class )

The wrapper class converts basic type data into objects : Each basic type is Java.lang There is a corresponding packaging class in the package

Function of packaging : A series of practical methods are provided Collection is not allowed to store basic data type data , When storing numbers , Packing type to be used

2. All wrapper classes can take the corresponding basic data type as a parameter , To construct their instances public Type(type value) as :
except Character Out of class , Other wrapper classes can construct their instances with a string as a parameter public Type(String value) as :

3. matters needing attention

Boolean Class constructor parameters are String Type time , If the string content is true( Regardless of case ), Then Boolean Object representation true, Otherwise, it means false
When Number The parameters of the packaging class construction method are String
Type time , String cannot be empty null, And the string must be resolvable to the data of the corresponding basic data type , Otherwise, the compilation fails , Thrown at runtime NumberFormatException abnormal

4. Common methods of packaging

(1)  XXXValue(): Convert wrapper class to base type  byteValue(),intValue() longValue(),shortValue()
doubleValue(),floatValue() charValue(),booleanValue()

(2)  toString(): Returns the basic type data represented by the wrapper object as a string ( Basic type -> character string ) or  
parseXXX(): Convert the string to the corresponding basic data type data (Character except )( character string -> Basic type )

(3)  XXX.valueof: Basic data type And string to wrapper class

The code is implemented as follows :

packageshitilei;importjava.util.ArrayList;importjava.util.List;importcom.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory.Zephyr;public
classTest {public static voidmain(String[] args) {// The basic data type is "." No content
You can convert basic data types to objects // Method 1

int i=9;

List list=newArrayList();

list.add(i);// The basic data type cannot be stored in the collection itself But there was a problem when adding jdk Operation of automatic packing // All wrapper classes have construction methods Use the corresponding data type as a parameter

int j=9;

Integer j1=newInteger(j);

Double double1=new Double(9.8);// Method 2 // except Character Construction methods other than Take string as parameter

Integer j3=new Integer("9");// The string must conform to the defined data format, otherwise an error will be reported

Double double2=new Double("9.8");//Character caCharacter=new
Character("a");// Compilation error // Method 3 //valueof()//(1) Basic data type repackaging class

Integer iiiInteger=Integer.valueOf(99);//(2) How to convert a string into a wrapper class

Integer
iinInteger=Integer.valueOf("99");//***************************************************************************// Packaging class conversion basic type

Integer aaInteger=new Integer(99);

System.out.println(aaInteger.intValue());// Basic type to string

int num=9;

String ing=Integer.toString(9);// Other similar // String to basic data type

int num1=Integer.parseInt("99");

}

}

5. Unpacking and packing

concept : stay jdk1.5 After version Automatic conversion of basic data types and packaging classes    Packing : An object whose base type is converted to a wrapper class Unpacking : The value of the wrapper class object converted to the base type

characteristic :JDK1.5 after , Mixed mathematical operations of basic data type and packing type are allowed Wrapper classes are not intended to replace basic data types Used when the basic data type needs to be represented by an object

6.math class

java.lang.Math Class provides common mathematical operation methods and two static constants E( Base of natural logarithm ) and PI( PI )

Common methods :

packageshitilei;public classMathtest {public static voidmain(String[] args)
{// Random generation 0-10 Floating point number

System.out.println(Math.random()*10);// Generate random integers Cast to int

System.out.println((int)Math.random()*10);// absolute value

System.out.println(Math.abs(-33));// Maximum

System.out.println(Math.max(22, 5));

}

}

7.Random class

Random rand = new Random(); // Create a Random object

for (int i = 0; i < 20; i++) {// Random generation 20 Random integers , And display

int num = rand.nextInt(10);// Returns the next pseudo-random number , Integer

System.out.println(" The first "+(i+1)+" A random number is :"+num);

}

packageshitilei;importjava.util.Random;public classRodemtest {public static
voidmain(String[] args) {// Different seed construction objects generate different random numbers

Random rand = new Random(56); // Create a Random object

Random rand2=new Random(56);

System.out.println(( rand.nextInt(10)));

System.out.println( rand2.nextInt(10));

}

}

This shows that the same seed value is used to initialize two Random object , Then call the same method with each object , The random number obtained is also the same

8.String class

String Class in java.lang Package , With rich methods Calculates the length of the string , Compare strings ,

packageshitilei;importjava.util.Scanner;public classStringTest {public static
voidmain(String[] args) {

String a=" admin ";

String p="123456";

Scanner input=newScanner(System.in);

System.out.println(" enter one user name ");

String username=input.next();

System.out.println(" Please input a password ");

String pwd=input.next();//trim() Remove the spaces at both ends of the string

a.trim();//lenght() Judge string length //String From the beginning equels Method becomes to compare whether two strings are the same object

if (pwd.length()<6&&username.equals(a)) {// The comparison conditions can be written as pwd.length()<6&&username==a
== Is the address of the comparison

System.out.println(" Login succeeded ");

}else{

System.out.println(" The login length is less than six digits or the user name is wrong ");

}//*************************************************************

String str1="BDQN";

String str2="bdqn";// Case sensitive

System.out.println(str1.equals(str2));// Case insensitive

System.out.println(str1.equalsIgnoreCase(str2));// Convert lowercase comparison

System.out.println(str1.toLowerCase().equals(str2.toLowerCase()));// Convert to uppercase comparison

System.out.println(str1.toUpperCase().equals(str2.toUpperCase()));

}

}

Extract string

packageshitilei;importjava.util.Scanner;importsun.net.www.content.audio.wav;public
classStringjiequ {public static voidmain(String[] args) {

String shou="hello my name is wuxuewei";// Find where a character appears

System.out.println(shou.indexOf("w"));// Find last w Location of occurrence

System.out.println(shou.lastIndexOf("w"));// Intercept string

System.out.println(shou.substring(4));// Intercept from the fourth to the last // Starting position End position

System.out.println(shou.substring(0, 5));

System.out.println("*******************************************");// Practice of character interception

Scanner input=newScanner(System.in);

System.out.println("===== Welcome to submit your assignment =====");

System.out.println(" Please enter a file name ");

String name=input.next();

System.out.println(" Please enter email address ");

String yx=input.next();// check filenames

boolean nameflag = false;int i=name.indexOf(".");if
(i!=-1&&i!=0&&name.substring(i).equals(".java")) {

nameflag=true;

}else{

System.out.println(" filename not valid ");

}// Check mailbox name

boolean yxflag = false;int aite=yx.indexOf("@");int dian=yx.indexOf(".");if
(aite!=-1&&dian!=-1&&dian>aite) {

yxflag=true;

}else{

System.out.println(" Invalid mailbox ");

}if (yxflag &&nameflag) {

System.out.println(" Submitted successfully ");

}else{

System.out.println(" Submission failed ");

}

}

}

System.out.println("*************** String splitting ****************");// String splitting method split();

String shici = " Outside the Pavilion Ancient road edge The evening wind blows the willows and drunk the setting sun ";

String[] chafen= new String[100];

System.out.println(" Before splitting " +shici);

System.out.println(" After splitting ");

chafen= shici.split(" ");for(String string : chafen) {

System.out.println(string);

}

System.out.println("********* Number of occurrences of query characters ********");

System.out.println(" Please enter a string ");

String chaifen2=input.next();

System.out.println(" Please enter the characters you want to query ");

String chazi=input.next();

String chaifen2s[]=chaifen2.split("*");int count=0;for (int j = 0; j <
chaifen2s.length; j++) {if(chaifen2s[j].equals(chazi)) {

count++;

}

}

System.out.println(" The character of the query appears "+count);

9.StringBuffyer class

StringBuffer: When the string is frequently modified , Use it to greatly improve the efficiency of program execution

StringBuffer Use of :

packageshitilei;public classStringBufferDome {public static voidmain(String[]
args) {//String →StringBuffer

StringBuffer str=new StringBuffer("hello");//StringBuffer→String

String s=str.toString();//StringBuffer Add content

str.append("word");

System.out.println(str);//insert Insert content

str.insert(5, ",");

System.out.println(str);

}

}

10. Get current time

packageshitilei;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;public
classDateDome {public static voidmain(String[] args) {// current time

Date date=newDate();

System.out.println(date);

System.out.println("******************************************");// set time format

SimpleDateFormat fomator=new SimpleDateFormat("yyyy-MM-dd-mm:ss");

String string=fomator.format(date);

System.out.println(string);

System.out.println("*********************************************");// adopt calendar class

Calendar s=Calendar.getInstance();

System.out.println(s.get(Calendar.YEAR)+" year "+(s.get(Calendar.MONTH)+1)+" month "+s.get(Calendar.DAY_OF_MONTH));

System.out.println(" Today is Sunday "+s.get(Calendar.DAY_OF_WEEK));

}

}

Technology