Java Career - Java Foundation - Other Objects

Keywords: Java Attribute less jvm

Lecture 1: System Classes

I. overview

1. System is a class describing some information of the system. The attributes and methods in the class are static. Cannot be instantiated, no constructor is provided.

2. Field Summary

out: Standard output stream. The default is the console.

in: Standard input stream. The default is the keyboard.

 

Two, method

1. Obtain the attribute information of the system:

        PropertiesgetProperties();

Explain:

1) The double-column set returned by this method is the key-value pair; since Properties is a subclass of Hahstable, that is, a subclass object of the Map set, the elements in the set are extracted by the Map method.

(2) The collection stores strings without generic definitions

2. Get the specified attribute information:

        String getProperty(Stringkey);

3. Define specific information within the system:

        String setProperty(Stringkey,String value);

4. How to load some attribute information when the jvm starts up:

Command: java-D<name>=<value> can set unique system attribute information

Example:

  1. import java.util.*;  
  2. class SystemDemo   
  3. {  
  4.     public static void main(String[] args)   
  5.     {  
  6.         Properties p=System.getProperties();//Get the current system properties  
  7.         sop(p);  
  8.   
  9.         String value=System.getProperty("os.name");//Gets the system properties indicated by the specified key  
  10.         sop("os.name="+value);  
  11.   
  12.         System.setProperty("wode","My_PC");//Set the system property information indicated by the specified key  
  13.         sop(p);  
  14.           
  15.         String s= System.getProperty("woshi");//When there is no key value in the system, return null  
  16.         sop(s);  
  17.   
  18.     }  
  19.   
  20.     public static void sop(Object obj)  
  21.     {  
  22.         System.out.println(obj);  
  23.     }  
  24. }  

 

Lecture 2: Runtime classes

I. overview

1. Every java application has an instance of Runtime class, which can be connected with the environment in which it runs. The application can not create its own instance of Runtime class, which is created by the bottom of the system.

2. Constructors are not provided in this class. Explain that new objects are not allowed. Then it comes to mind that the methods in this class are static. Looking at the API documentation, we found that there are also non-static methods in this class. It shows that this kind of method will certainly provide a way to get the original object. Moreover, the method is static and the return value type is the original type.

3. It can be seen from the above characteristics that this kind of design is accomplished by using the singleton design pattern.

 

Two, method

1. Get this class of objects

        static RuntimegetRuntime();

2. Executing the specified string command in a separate process

        Processexec(String command);

3. In Process, there is a way to kill the child process, which can end the process opened by the exec method.

        void destroy();

Example:

  1. class RuntimeDemo   
  2. {  
  3.     public static void main(String[] args)throws Exception  
  4.     {  
  5.         Runtime r=Runtime.getRuntime();//Get this class of objects  
  6.   
  7.         Process p=r.exec("notepad.exe");//Enforcement of Notepad Procedure  
  8.         r.exec("notepad.exe  RuntimeDemo.java");//Open the specified file in Notepad  
  9.   
  10.         Thread.sleep(3000);//Let the thread wait for 3 seconds  
  11.         p.destroy();//Kill the designated process  
  12.     }  
  13. }  

 

Lecture 3 Time Class

I. Date class

1. Overview

The Date class represents a specific instant, accurate to milliseconds.

The default display format in java is: Mon Jun 1022:35:21 CST 21013

2. Custom Format

The default format does not necessarily meet everyone's needs, so you need to customize the formatting mode. Because most of the methods in the Date class are out of date, only its subclasses can be found to implement them. The format method can be implemented in the subclass DateFormat, but DateFormat is an abstract class and cannot be instantiated. But there's a SimpleDateFormat subclass below, which defines the time pattern.

Specific steps:

Create Date objects

2) Encapsulate time patterns into SimpleDateFormat objects

3) Call format method to let format mode specify Date object

The code is as follows:

  1. import java.util.*;  
  2. import java.text.*;  
  3. class DateDemo   
  4. {  
  5.     public static void main(String[] args)   
  6.     {  
  7.         Date d=new Date();//Creating Date Objects  
  8.         //sop(d);  
  9.   
  10.         //// Encapsulate the schema into the SimpleDateformat object.  
  11.         SimpleDateFormat sdf=new SimpleDateFormat("yyyy year MM month dd day E HH:mm:ss");  
  12.         //Call the format method to format the schema to specify the Date object.  
  13.         String time=sdf.format(d);  
  14.         sop(time);  
  15.     }  
  16.   
  17.     public static void sop(Object ojb)  
  18.     {  
  19.         System.out.println(ojb);  
  20.     }  
  21. }  


II. Carendar Classes

1. Overview:

Calendar is an abstract class that provides some methods for the conversion of a particular instant to a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH.

2) Calendar can acquire the time of year, month, day and so on. The corresponding subsection values are also provided. Refer to the API documentation.

2. Methods:

2.1. Basic acquisition time

1) Year of acquisition: Calendar.YEAR

2) Access month: Calendar.MONTH

3) Get date: Calendar.DAY_OF_MONTH

4) Access Week: Calendar.DAY_OF_WEEK

5) Get hours: Calendar.HOUR_OF_DAY

6) Get minutes: Calendar.MINUTE

7) Get seconds: Calendar.SECOND

2.2. Setting time:

1) According to calendar rules, add or subtract the specified amount of time for a given calendar field:

                void add(int field,int amount);

2) Get the corresponding time value for the specified calendar field:

                int get(int field);

3) Set the time value corresponding to a given calendar field to a given value:

                void set(int field,int value);

Set the values of the calendar fields YEAR, MONTH and DAY_OF_MONTH:

                void set(int year,int month,int date);

Example:

  1. import java.util.*;  
  2. class  CalendarDemo  
  3. {  
  4.     public static void main(String[] args)   
  5.     {  
  6.         Calendar c=Calendar.getInstance();  
  7.           
  8.         sop(pattern(c));//Display today's date and current time  
  9.           
  10.         c.add(Calendar.YEAR,3);//Year changed to 3 years later  
  11.   
  12.         sop(pattern(c));//Display the specified date and time  
  13.   
  14.     }  
  15.   
  16.     //Gets the specified date and time  
  17.     public static String pattern(Calendar c)  
  18.     {  
  19.         String s=year(c)+month(c)+day(c)+week(c)+" "+time(c);  
  20.         return s;  
  21.     }  
  22.       
  23.     //Year of acquisition  
  24.     public static int year(Calendar c)  
  25.     {  
  26.         return c.get(Calendar.YEAR);  
  27.     }  
  28.     //Get month  
  29.     public static String month(Calendar c)  
  30.     {  
  31.         String[] yue={"January","February","March","April",  
  32.                     "May","June","July","August",  
  33.                     "September","October","November","December"};  
  34.         return yue[c.get(Calendar.MONTH)];  
  35.     }  
  36.     //get date  
  37.     public static int day(Calendar c)  
  38.     {  
  39.         return c.get(Calendar.DAY_OF_MONTH);  
  40.     }  
  41.     //Get week  
  42.     public static String week(Calendar c)  
  43.     {  
  44.         String[] day={"","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};  
  45.         return day[c.get(Calendar.DAY_OF_WEEK)];  
  46.     }  
  47.     //Get the time in time: minutes: seconds  
  48.     public static String time(Calendar c)  
  49.     {  
  50.         String s=c.get(Calendar.HOUR_OF_DAY)+":"+c.get(Calendar.MINUTE)+":"+c.get(Calendar.SECOND);  
  51.         return s;  
  52.     }  
  53.   
  54.     //Printout  
  55.     public static void sop(Object obj)  
  56.     {  
  57.         System.out.println(obj);  
  58.     }  
  59. }  

Little exercise:

  1. /* 
  2. Practice 
  3. 1,How many days are there in February of any year? 
  4.     Idea: Set a time according to a given year. 
  5.     c.set(year,2,1)//March 1 of a certain year. 
  6.     c.add(Calenar.DAY_OF_MONTH,-1);//3 On January 1st, one day forward, is the last day of February. 
  7.  
  8. 2,Get the present moment of the day before 
  9.     Ideas: c.add(Calenar.DAY_OF_MONTH,-1); 
  10.  
  11. 3,Calculate the number of days available for classes, no classes on six days a week, and the number of days available for classes from September to October in any year. 
  12.     Train of thought; 
  13.     1,Set a time based on a given year to be c.set(year,8,1)// September 1 of a given year 
  14.     2,Define a counter to determine whether the specified time is Saturdays or Sundays, not counts plus 1? 
  15.     3,Date offset 1 day until the last day of the month 
  16. */  
  17.   
  18. import java.util.*;  
  19.   
  20. class  OtherClassTest  
  21. {  
  22.     public static void main(String[] args)   
  23.     {  
  24.         //Exercise 1 Test  
  25.         sop("2012 In February, 2000"+whatDay(2012)+"day");  
  26.   
  27.         //Exercise 2 Test  
  28.         sop("At this moment yesterday:"+lastNowTime());  
  29.   
  30.         //Exercise 3 Test  
  31.         sop("2013 September to October"+howDay(2013)+"Heaven lesson");  
  32.     }  
  33.   
  34.     //Exercise 1  
  35.     public static int whatDay(int year)  
  36.     {  
  37.         Calendar c=Calendar.getInstance();  
  38.         c.set(year,2,1);  
  39.         c.add(Calendar.DAY_OF_MONTH,-1);  
  40.         return c.get(Calendar.DAY_OF_MONTH);  
  41.     }  
  42.   
  43.     //Exercise 2  
  44.     public static String lastNowTime()  
  45.     {  
  46.         Calendar c=Calendar.getInstance();  
  47.         c.add(Calendar.DAY_OF_MONTH,-1);  
  48.         return pattern(c);  
  49.     }  
  50.   
  51.     //Exercise 3  
  52.     public static int howDay(int year)  
  53.     {  
  54.         Calendar c=Calendar.getInstance();  
  55.         c.set(year,8,1);  
  56.         int count=0;  
  57.         for (int x=c.get(Calendar.DAY_OF_MONTH); x<=30;x++ )  
  58.         {  
  59.             if(c.get(Calendar.DAY_OF_WEEK)!=1&&c.get(Calendar.DAY_OF_WEEK)!=7)  
  60.                 count++;  
  61.             c.add(Calendar.DAY_OF_MONTH,1);  
  62.         }  
  63.         return count;  
  64.     }  
  65.   
  66.     //Gets the specified date and time  
  67.     public static String pattern(Calendar c)  
  68.     {  
  69.         String s=year(c)+month(c)+day(c)+week(c)+" "+time(c);  
  70.         return s;  
  71.     }  
  72.       
  73.     //Year of acquisition  
  74.     public static int year(Calendar c)  
  75.     {  
  76.         return c.get(Calendar.YEAR);  
  77.     }  
  78.     //Get month  
  79.     public static String month(Calendar c)  
  80.     {  
  81.         String[] yue={"January","February","March","April",  
  82.                     "May","June","July","August",  
  83.                     "September","October","November","December"};  
  84.         return yue[c.get(Calendar.MONTH)];  
  85.     }  
  86.     //get date  
  87.     public static int day(Calendar c)  
  88.     {  
  89.         return c.get(Calendar.DAY_OF_MONTH);  
  90.     }  
  91.     //Get week  
  92.     public static String week(Calendar c)  
  93.     {  
  94.         String[] day={"","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};  
  95.         return day[c.get(Calendar.DAY_OF_WEEK)];  
  96.     }  
  97.     //Get the time in time: minutes: seconds  
  98.     public static String time(Calendar c)  
  99.     {  
  100.         String s=c.get(Calendar.HOUR_OF_DAY)+":"+c.get(Calendar.MINUTE)+":"+c.get(Calendar.SECOND);  
  101.         return s;  
  102.     }  
  103.   
  104.     //Printout  
  105.     public static void sop(Object obj)  
  106.     {  
  107.         System.out.println(obj);  
  108.     }  
  109. }  

 

Lecture 4. Math Class

I. overview

Math classes contain methods for performing basic mathematical operations, such as elementary exponents, logarithms, square roots, and triangular functions. This class is all static methods.

 

Two, method

1. doubleceil(double d); //Returns the smallest integer greater than the specified data

2. Double floor (double d); //Returns the largest integer less than the specified data

3. Double pow (double a, double b); //Return the b-th power of a

4. long round(doubleb); //Returns the value rounded by b.

5. doublerandom(); // returns a positive double value, which is a random number greater than or equal to 0.0 and less than 1.0

 

III. Random Classes

This is a separate class in java.util that is used to get random numbers. The random method in Math is the same, but this class has its own method, which can strongly convert the corresponding random number to the specified basic data type.

For example: intnextInt(int n); / / Returns a pseudo-random number, which is an int value evenly distributed between 0 (including) and specified value (excluding) from this random number generator sequence.

Little exercise:

  1. /* 
  2. Exercise: Given a decimal, keep the last n digits of the decimal. 
  3.     1,First multiply the given decimal by the n-th power of 10, rounding it around to preserve the integer part. 
  4.     2,Divide the integer by the n power of 10. 
  5. */  
  6. class MathTest   
  7. {  
  8.     public static void main(String[] args)   
  9.     {  
  10.           
  11.         double d=retention(13.6572,2);  
  12.   
  13.         System.out.println("d="+d);  
  14.     }  
  15.       
  16.     //Reserving decimal method  
  17.     public static double retention(double d,int n)  
  18.     {  
  19.         double d1=d*Math.pow(10,n);//The decimal digits to be reserved are first changed into integers  
  20.         long l=Math.round(d1);//Round decimal  
  21.         return l/Math.pow(10,n);//Returns a decimal number that retains the specified decimal digit  
  22.     }  
  23. }  

Posted by Mr Camouflage on Thu, 18 Apr 2019 13:36:38 -0700