Self-learning java early experience [notes] # # fourteenth day of classes and objects

Textbook: "Java core technology Volume"

Chapter Four Objects and Classes

First Experience 4.2 Date of classes and objects

First, few places prone to ambiguity

1, "the constructor's name should be the same as the class name"

We mentioned in previous chapters: in the creation of the class, must be consistent with the file name. So to this chapter, at first glance these words, my heart logic is: class name = file name, the file name and class name should be called Date. In fact, only the name of the constructor and the class Date is on it, the file name and the constructor is best not to name the same. Because some compiler does not automatically add the feature pack, so it can not recognize we refer to the Date class in the programming process, will not prompt you to add a Date package inside. Final result must also be wrong, it's usually the error message is: Can not format given Object as a Date for that matter, simply import Date package to run the program. (Import java.util.Date).

2, the conventional variable object variable

Studied the integer variable, such as int n; floating-point variables, such as float k; object variable and the difference between these variables is that: they are used to define the object / declaration. The Date class object variable as: Date deadline.

3, did not object variable contains an object

This is actually very good understanding, no one would think the above example n is truly contains a positive integer. All objects in the object reference variables are stored in one place only.

4, an object is set to null variable

null null variable and there is a difference, like a string of previous study, empty string is a string of length 0, the null string indicates that the string is not associated with any object.

Second, use the Date class to get the current time Method:

1, a simple constructor method:

String s=new Date().toString();
            System.out.println(s);

or:

System.out.println(new Date());

2, reference DateFormat and SimpleDateFormat:

Date birthday=new Date();
            DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(df.format(birthday));

 

Guess you like

Origin www.cnblogs.com/yizhinailu/p/12499992.html
Recommended