J2SE Core Development Combat (ii)--string and packaging class

Source: Internet
Author: User
Tags java se

String and wrapper class I. Introduction to Experiments

In this chapter, we will learn some of the APIs used to process strings and the knowledge about wrapper classes.

Knowledge points in this chapter

    • String API
    • Packaging class and its application
Second, recognize the string Class 1. What is a String class

Java String Class (Java.lang.String) is the most used class in Java, is also the most special class, many times, we are both familiar and unfamiliar.

First you need to know what a string is:

A string is a series of characters consisting of numbers, letters, and underscores, which are data types that represent text in a programming language.

You can see more about the string.

Common ways to manipulate strings are to copy, calculate their length, find some characters in a string, and so on.

However, the string class in the Java standard library contains many methods, and it is unlikely to remember all classes and methods. The best way is to develop the habit of checking API documentation at any time. In the final reference we provide you with a link to the official API documentation, where you can see in detail each method of manipulating the string.

It is also important to consult the API documentation for future chapters, please keep this habit.

2. Manipulating strings with a self-contained method in the String class

Create a project in Eclipse StringUtil , make a new com.shiyanlou.course package, and create a StringTools class.

This will be used trim() , substring() , equals() and length() methods,
-The String trim() method returns a new string with the new string removing the trailing and trailing spaces in the original string.
-The String substring(int beginIndex, int endIndex) method returns a new string containing all the contents from the original string beginIndex endIndex-1 .
-- boolean equals(Object other) The method is used to compare two strings, if the original string is equal to ignoring case, other then returns true .
-The int length() method returns the length of a string.

This project first needs to get input from the user, then remove the extra space in the input string, and then compare it to the string, and shiyanlou if the two strings are the same, intercept the first 6 bits of the string as the new string.

The main code is as follows:

Again, you don't have to enter these Chinese comments in the lab environment.

Package Com.shiyanlou.course;import Java.util.Scanner; Public classStringtools { Public Static void Main(string[] args) {String A =NewString (); String B =NewString (); String C =NewString ();//Create 3 String objectsScanner scan =NewScanner (System.inch);//Create scannerSystem. out. println ("Please input a sentence with ten more letters include space (s):");//Get a string containing spaces for user inputA = Scan.nextline ();//Assign the input on the keyboard to string ab = A.trim ();//Remove the space from the head and tail in a and assign to B        if(B.equals ("Shiyanlou")) C = b.substring (0,6);Else{c =""; }//If the B string is Shiyanlou, take its No. 0 to 6th character as a substring and assign it to C        //If the B string is not Shiyanlou, the value of C is nullSystem. out. println ("A:"+A); System. out. println ("The length of A is:"+a.length ()); System. out. println ("B:"+B); System. out. println ("The length of B is:"+b.length ()); System. out. println ("C:"+C); System. out. println ("The length of C is:"+c.length ());//Outputs A, B, c three strings and their lengths, respectively}}

Click Compile and run, in the console first enter 5 spaces, followed by input shiyanlou , then enter 4 spaces and return, you can see the output as follows:

You might want to ask: why don't we use it when we're comparing strings == equals() ?
You can click here to learn more about "= =" and equals.

Third, the understanding of packaging Class 1. What is a wrapper class

We know that the Java language is object-oriented, but the basic data types in Java are not object-oriented. This has caused a lot of inconvenience in the actual use process. To solve this problem, the Java language designers have designed a class for each of the basic data types. The eight classes that correspond to the basic data types are collectively referred to as 包装类 (Wrapper Class), also known as the outer or data type classes.

The wrapper classes are located in the Java.lang package, and the following table shows the corresponding relationships between the wrapper class and the base data type:

Basic data Types corresponding Packaging class
Byte Byte
Boolean Boolean
Short Short
Char Character
Int Integer
Long Long
Float Float
Double Double

There are two main uses of wrapper classes:
-As the class type corresponding to the basic data type exists, it is convenient to involve the operation of the object.
-Contains related properties for each basic data type, such as maximum value, minimum value, and related action methods.

Below we have an example to further understand the packaging class.

2. Comparing double types

The first thing you should know about the double Double difference between the former is the basic data type, the latter is the reference type, which is the wrapper class. The basic data types can be compared directly using the normal operator " == ", but if " == " is used to compare reference types, only the memory addresses are judged to be the same, and the results are usually negative.

Create a project in Eclipse DoubleCompare , make a new com.shiyanlou.course package, and create a DoubleCompare class.

In the method of the class main() , define two strings that contain numbers and convert them to data of type double.

String"123.321";String"567.765";//显然,这里的123.321和567.765都不是数值,而是“一句话”//你也可以自定义这些数字Double num_1 = Double.parseDouble(s_1);Double num_2 = Double.parseDouble(s_2);//定义了Double类型的数据,并将字符串转换为双精度浮点数赋予其值

The above parseDouble(String s) is a method of converting a number from a Double string to a type in a class double .

We'll export it again.

System.out.println("number 1: " + num_1);System.out.println("number 2: " + num_2);

Then we compare the two data, the main code is as follows:

switch  (Num_1.compareto (num_2 ) {case -1 : System.out  println (" Number 1 is smaller than number 2 ");    break ; case  0 : System.        out . println ( "Number 1 is equal to number 2" );    break ; case  1 : System.        out . println ( "Number 1 is bigger than number 2" ); break ;}  

Believe you can understand the meaning of this branch statement. In particular, it is compareTo(Double anotherDouble) also a method provided by the double class that compares the size of the data of two double types, the return value is int type, and -1 0 1 represents less than, equal to, and greater than, respectively.

The complete code for the case is as follows:

Click Compile and run to see the output in the console:

Iv. Summary of the experiment

In this chapter we mainly learn the definition of string and commonly used to generate substrings, the removal of space, comparison and other methods, and the definition of packaging class and its main operating methods. For more information about them in the reference documentation, try the methods that are not yet available in this course, such as string stitching. In the experimental building after the end of the study, do not forget to look at these API documents, more hands-on writing code, Tiandaochouqin!

V. Homework

Use the and methods of the string class to toLowerCase() toUpperCase() write a string-case conversion tool.

Reference documents

For the string API, you can view the official Java SE documentation for more and better gameplay:
+ Java SE official API documentation-Java.lang.String

The following are the official technical documentation for some of the packaging classes in Java:

    • Java SE official API documentation-Java.lang.Byte
    • Java SE official API documentation-Java.lang.Boolean
    • Java SE official API documentation-Java.lang.Integer

We do not give all the packaging reference documents, please do your own Ctrl+F experience of the search process.

This text link: https://www.shiyanlou.com/courses/111 This lesson is the experiment Building original course, reprint please specify.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

J2SE Core Development Combat (ii)--string and packaging 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.