Java BASIC program Structure design character type

Source: Internet
Author: User

Character type

Conceptually, Java's character type is a sequence of Unicode characters.

Immutable:

Once a string has been defined, there is no way to modify it. Java does not provide a way to modify the string, for a C program, the string is equivalent to the array, you can change any of the underlying value. But Java is not.

If you want to modify the subscript for a Java string, you can only create a new string and then implement it by substring and stitching, which is somewhat inefficient.

However, the Java designers think that the string modification operation is very rare, for the operation of the string, mostly comparisons, and merging operations. So the Java designer sets the string to immutable and then implements the share.

No strings are mutable and sharing is not possible:

You can imagine putting a string in a common pool, a string variable pointing to a value in the pool, and if you copy a string, the source string and the copied string share the same location.

Java designers believe that sharing brings more efficiency than a modifiable string.

Detect equality:

Because immutable, so can not use = =, only with equals.

Because string is an object, the = = in Java is used to compare the same position in the stack to which the object is pointing.

Even so, if we use the = comparison object to point to the same position in the stack, it is not.

    1. package com.zjf;
    2.  
    3. public class Test {
    4.  
    5.    public static void Main (string[] args) {
    6.  
    7.       string S1 = "zhang ";
    8.       string s2 = " Zhang ";
    9.       system. Out.println (S1 = = s2);
    10.    }
    11. }

The result is true. For us, we didn't do s1=s2, and the result is true, which is not the result we want. Although sting a = B, then use a = = B is possible, but if there is no a=b, can also a==b, so for us, there will be unpredictable results.

So what if we want to use = to compare the contents of two strings in a consistent order? Or not.

  1. Package COM.ZJF;
  2. public class Test {
  3. Public static void Main (string[] args) {
  4. String S1 = "Zhang";
  5. String s2 = "Zhangjianfeng". Substring (0, 5);
  6. System.out.println (S1);
  7. SYSTEM.OUT.PRINTLN (S2);
  8. System.out.println (S1 = = s2);
  9. }
  10. }

The result is:

Zhang

Zhang

False

Two strings are Zhang, use = but not more successful.

The reason is that because Java virtual machines share only string constants, the results of operations such as + and substring are not shared.

using = = for string comparisons, the program will have a bug, and the bug is somewhat random. Do not use.

Code points and Code units

The Java string consists of a char sequence, which is a unit of code that uses UTF-16 encoding to represent the UNICOD code point.

The code point is a word we face in life, because the way UTF-16 is stored, for some code points requires 32 bits, that is to say two code units to store, in Java, a char is a unit of code.

This will cause some misunderstanding.

First, the length method returns the number of code units. Rather than the number of code points.

Second, the Charat method gets the code unit, not the code point. If you want to get a code point, String provides a codepoint method,

The following code:

Avoid using char, the author says, because it's too low-level. In fact, although it is rare to see these special characters, the scene using char is prudent.

Build string:

To build a string using a shorter string:

    • StringBuilder
    • StringBuffer Thread Safety
Packet Introduction Issues

Packages like Sting,integer,stringbuilder, located in the Java.lang directory, do not require import and can be identified.

Read input: Java.util.Scanner

A simple text scanner that can use regular expressions to parse basic types and strings.

Know:

Scanner uses the delimiter mode to break its input into markup, which by default matches whitespace. You can then use a different next method to convert the resulting token to a different type of value.

For example, the following code enables a user to read a number from system.in:

Scanner sc = new Scanner (system.in);

int i = Sc.nextint ();

Looking at an example, the following code enables the long type to be allocated through the items in the Mynumbers file:

Scanner sc = new Scanner (New File ("Mynumbers"));

while (Sc.hasnextlong ()) {

Long along = Sc.nextlong ();

}

The scanner can also use a separator that is different from the white space. Here is an example of reading several items from a string:

String input = "1 Fish 2 fish Red Fish Blue fish";

Scanner s = new Scanner (input). Usedelimiter ("\\s*fish\\s*");

System.out.println (S.nextint ());

System.out.println (S.nextint ());

System.out.println (S.next ());

System.out.println (S.next ());

S.close ();

The output is:

1

2

Red

Blue

The following code uses a regular expression to parse all 4 tags simultaneously and produces the same output as the previous example:

String input = "1 Fish 2 fish Red Fish Blue fish";

Scanner s = new Scanner (input);

S.findinline ("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");

Matchresult result = S.match ();

for (int i=1; I<=result.groupcount (); i++)

System.out.println (Result.group (i);

S.close ();

The default whitespace delimiter used by the scanner is identified by the character.iswhitespace.

Relative directory for file:

Know:

If a relative directory is used when creating a file, then:

Relative path names must be interpreted using information from other pathname names. By default, the classes in the Java.io package always parse relative path names based on the current user directory. This directory is specified by the system property User.dir, which is usually the calling directory for the Java virtual machine.

That is, relative to the User.dir directory. So we try not to use relative directory, here encountered, so labeled under, understand under.

Text output:

Java.io.PrintWriter

Prints the formatted representation of the object to the text output stream . This class implements all the print methods in PrintStream. It does not contain methods for writing raw bytes, and for these bytes, the program should write using an encoded byte stream.

The function of this class is to put an in-memory object, in the form of formatted text, output into the stream. Its API is as follows:

The API for Java.io.PrintStream and Java.io.PrintWriter is basically the same, except that one is output in bytes, one is a character, but this is an internal implementation.

These two classes are rarely used because System.out is using PrintStream, which we'll look at here.

Java BASIC program Structure design character type

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.