Java JDK Objects of use

Source: Internet
Author: User
Tags date tostring

In Java, you will often talk about classes (class) and objects (object) of these two nouns, to discuss the difference between the two nouns, the content of object-oriented programming should be discussed in detail. Here's a simple explanation: A class is like a design diagram of an object that defines what an object can manipulate.

The object is like a concrete tool, not in terms of Java, but the class defines how the tool is generated. For example, if you have a design diagram of a pair of scissors, and a pair of scissors is made from this design, the design is similar to the class, and the scissors produced are similar to the tools you are talking about.

Before you start learning Java object-oriented Programming, learn how to use a series of standard tools (i.e., standard objects) that J2SE provides, and indicate the design diagram (i.e., class) to use to produce concrete tools (i.e. objects). What the tool's diagram content is actually you don't have to worry about, all you have to do is manipulate these tools and work with the process Control syntax described in Chapter 3rd to complete the required procedures.

To give a simple example, if you want to write a program to get the current system time, as long as the creation of a java.util.Date tool. There is no need to worry about how date actually gets time to the system. Example 4.1 demonstrates how to get the system time.

Example 4.1 Nowtime.java

import java.util.Date;
  public class NowTime {
    public static void main(String[] args) {
      Date date = new Date();
      system.out.println(date.toString());
    }
}

Date date indicates that the tool you specified is a date type, and that the reference name (reference name) is Date;new date () to produce such a date tool for the program. You can imagine a reference name as a card that is now tied to a date tool. After you obtain the instance of date (Instance), you can manipulate it by referencing the name. It's like having a "on duty" card on a company employee, a card on which employee, and the employee who has the card when calling through the "on duty" card.

The operation of the ToString () method In example 4.1 requires that an instance of date produce the current system time and then output it in text mode (for a detailed description of the date operation, see chap. 18th). The results of the implementation are as follows:

Tue May 16:06:46 gmt+08:00 2005

The most obvious thing about another action object is the manipulation of strings, which are objects in Java and an instance of the Java.lang.String class. In the 6th chapter, we will describe the character of the string in detail, showing several simple string operations in Example 4.2 first.

• Example 4.2 Stringdemo.java

public class StringDemo {
    public static void main(String[] args) {
      String text = "Have a nice day!! :)";
      system.out.println("原文:" + text);
      // 返回全为大写的字符串内容
      system.out.println("大写:" + text.toUpperCase());
      // 返回全为小写的字符串内容
      system.out.println("小写:" + text.toLowerCase());
      // 计算字符串长度
      system.out.println("长度:" + text.length());
      // 返回取代文字后的字符串
      system.out.println("取代:" + text.replaceAll("nice", "good"));
      // 返回指定位置后的子字符串
      system.out.println("子字符串:" + text.substring(5));
    }
  }

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.