Java BASICS (6): java Basics

Source: Internet
Author: User
Tags character classes

Java BASICS (6): java Basics
I. Packaging

JAVA is an object-oriented language. java classes connect methods with data,Basic type objects cannot be defined in JAVA,To treat basic types as objects for processing, java provides a packaging class for each basic type..

The relationship is as follows:

Basic Type Packaging type
Byte Byte
Short Short
Int Integer
Long Long
Float Float
Double Double
Char Character
Boolean Boolean

 

 

 

 

 

 

 

 

1. Number

BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short are all subclasses of the Number class.

The Character and Boolean parent classes are objects and do not belong to any subclass under Number.

View API documentation:

The Number class is an abstract class and cannot be directly created using the new keyword. The Number class has six methods, and some of these methods are abstract methods (abstract METHODS). The Number subclass needs to override these abstract methods.

2. Integer Packaging

View API documentation:

Understanding: the Integer class contains an int value. It can be understood that there is a variable box that stores the int value, that is, there is an int value in the Integer class.

Since the parent class Number already provides six methods, The subclass can call the parent class to provide six methods and convert the int value in the Integer object to the other six basic types, for example, convert to byte, short, int, long, float, double.

Similarly: for other types of the Number subclass, such as the Double class, you can call the six methods provided in the Number class to convert the values stored in the Double to these six values.

Check the Integer source code. You can see that the Integer class contains an int field and a constructor.

When an Integer object is created using the Integer Constructor (such as new Integer (123), the value is stored in the int type field.

2.1 Integer Constructor

View API documentation:

View the Integer source code:

The source code of the Integer (String s) constructor shows that the value is assigned to the variable this. value, this. value is an int variable defined in Integer to store values.

Note:

1. An exception occurs when you try to convert a string to a numeric type that contains characters that cannot be converted. For example, it contains letters (A, B, C) or operators (+.

2. If the value cannot exceed the int range, for example, 12345678987654321.

3. Automatic packing and unpacking

Automatic Packing: Converts an Integer from the basic type (int) to the packaging type (Integer) without manual conversion. The Compiler automatically calls Integer. the valueof method converts the basic type (int) to the (Integer) type.

Method 1: Use the constructor new Integer (123 );

Method 2: assign a value directly to Integer;

Integer integer = 123;

Automatic unpacking: Converts an Integer from the packaging type to an Int. The Compiler automatically calls the Integer. intValue converts the packaging type (Integer) to the basic type (int ).

Method 1: Call the intValue method in Integer;

Method 1: assign a value directly to int;

The conversion between the eight basic types and the packaging type supports automatic unpacking and packing.

4. short, Long, and Double

In addition to the Integer packaging class, there are also Short, Long, and Double packaging classes, and the structure is similar to Integer.

5. Boolean and Character classes

The Character and Boolean parent classes are objects.

The Boolean class has no maximum or minimum values.

Character represents one Character, while String represents multiple characters.

6. String type

View API documentation:

From the API, we can see the features of the String class:

  1. The index starts from 0;

2. Once a String object is created, its length and content cannot be changed;

How to Create a String object:

1. Direct assignment, String name = "abcdefg ";

2. Call the String constructor.

1 public class Demo {2/** 3 * Test to create a String 4 */5 @ Test 6 public void test1 () {7 String s1 = new String (); // represents an empty String 8 String s2 = ""; 9 String s3 = null; 10 11 System. out. println (s1 = s2); // false12 System. out. println (s1.equals (s3); // false13 // System. out. println (s3.equals (s1); When s3 is placed before equals, a null pointer exception is reported. lang. nullPointerException14} 15}

Analysis:

 String s1 = new String (); // s1 will point to a specific String instance. The default value is "" and a space is allocated in the memory;

String s2 = ""; // s2 corresponds to an empty String. It declares the object reference and allocates a space in the memory;

String s3 = null; // The s3 reference is null;

Note: s1 and s2 are instantiated, while s3 is not instantiated, but s1 and s2 refer to different address spaces, with the same value and are empty.

6.1 common methods of the String class

1. char charAt (int index );

Take the character at the specified position in the string, and the index starts from 0.

1 public class Demo {2/** 3 * Test charAt 4 */5 @ Test 6 public void testCharAt () {7 String str = "abcdefg "; 8 for (int I = 0; I <str. length (); I ++) {9 char element = str. charAt (I); 10 System. out. print ("[" + element + "]"); // output: [a] [B] [c] [d] [e] [f] [g] 11} 12} 13}

2. int codePointAt (int index );

Returns the characters (Unicode code points) at the specified index ).

1 public class Demo {2/** 3 * Test codePointAt 4 */5 @ Test 6 public void testCodePointAt () {7 String str = "abcdefg"; 8 int codePointAt = str. codePointAt (2); 9 System. out. println (codePointAt); // output: 99 represents the c ASCII code 10} 11}

3. String concat (String str );

Connects the specified (passed in) string to the end of the (current) string.Used to connect strings.

1 public class Demo {2/** 3 * Test concat 4 */5 @ Test 6 public void testConcat () {7 String str1 = "String 1111 "; 8 String str2 = "String 2222"; 9 String concatStr = str1.concat (str2); // create a String of 10 systems. out. println (concatStr); // output: String 1111 character string 222211} 12}

4. boolean endsWith (String suffix );

Test whether the string ends with the specified suffix.

1 public class Demo {2/** 3 * Test endsWith 4 */5 @ Test 6 public void testEndsWith () {7 String str = "abcd"; 8 boolean result = str. endsWith ("d"); 9 System. out. println (result); // return: true10} 11}

5. boolean isEmpty ();

Returns true only when length () is 0.

1 public class Demo {2/** 3 * Test isEmpty 4 */5 @ Test 6 public void testIsEmpty () {7 String str = ""; // cannot be null 8 boolean result = str. isEmpty (); 9 System. out. println (result); // return: true10} 11}

6. int length ();

Returns the length of the current string.

7. String replace (char oldChar, char newChar );

Returns a new string, which is obtained by replacing all the oldChar in the string with newChar.

1 public class Demo {2/** 3 * Test replace 4 */5 @ Test 6 public void testReplace () {7 String str = "String 222 "; 8 String newStr = str. replace ("222", "replace"); 9 System. out. println (newStr); // output: String replacement 10} 11}

8. String substring (int beginIndex );

Returns a new string starting from beginIndex. It is a substring of this string.

String substring (int beginIndex. int endIndex );

Returns a new string, which is a substring of this string;Note: Left closed and right open, left included, and right not included.

1 public class Demo {2/** 3 * Test substring 4 */5 @ Test 6 public void testSubString () {7 String str1 = "abcdefg "; 8 String substring = str1.substring (1); 9 System. out. println (substring); // output: bcdefg10 11 String str2 = "abcdefg"; 12 String substring1 = str2.substring (1, 3); 13 System. out. println (substring1); // output: bc Note: do not include the character 14} 15 with the index of 3}

9. String toLowerCase ();

Converts all characters in this String to lowercase letters.

10. String toUpperCase ();

Converts all characters in this String to uppercase.

11. String trim ();

Ignore leading and trailing white spaces of the string.

1 public class Demo {2/** 3 * Test trim 4 */5 @ Test 6 public void testTrim () {7 String str = "233333"; 8 String trim = str. trim (); 9 System. out. println (trim); // output: 233333 no space left 10} 11}
6.2 StringBuffer class and StringBuilder class

View API documentation:

Common StringBuffer Methods

1. StringBuffer append (Object o );

Append a specified object of any type to this StringBuffer object.

2. StringBuffer delete (int start, int end );

Remove characters from the substring of this sequence.

3. StringBuffer insert (int offset, Object o );

Insert the string representation of any type of parameter into this sequence.

4. StringBuffer replace (int start, int end, String str );

Replace the characters in the substring of this sequence with the characters in the given String.

1 public class StringBufferTest {2/** 3 * Test StringBuffer Method 4 */5 @ Test 6 public void testStringBufferMethod () {7 // 1. stringBuffer append (Object o); add 8 StringBuffer str = new StringBuffer ("abcdefgh"); 9 str. append ("ijk"); 10 System. out. println (str); // output: abcdefghijk11 12 // 2. stringBuffer delete (int start, int end); delete 13 str. delete (0, 3); 14 System. out. println (str); // output: defghijk left closed right open [) 15 16 // 3, StringBuffer insert (int offset, Object o); insert 17 str. insert (1, "333"); 18 System. out. println (str); // output: d333efghijk19 20 // 4, StringBuffer replace (int start, int end, String str); replace 21 str. replace (1, 4, "222"); 22 System. out. println (str); // output: d222efghijk23} 24}
Comparison between StringBuffer and String
1 public class StringBufferTest {2/** 3 * test StringBuffer 4 * concatenate a 0-9999 integer into a string; 5 * effects similar to 123456789101112 .... 9999; 6*7 * Analysis: 1. dynamic acquisition of 10000 values, using the for loop 8*2. define an empty string str 9*3. loop splicing 10 */11 @ Test12 public void testStringBuffer () {13 long begin1 = System. currentTimeMillis (); 14 String str1 = ""; 15 for (int I = 0; I <10000; I ++) {16 str1 + = I; 17} 18 System. out. println (str1); 19 long end1 = System. curren TTimeMillis (); 20 System. out. println (end1-begin1); // output: 974 takes more time because 10000 objects need to be created. 21 22 System. out. println ("========="); 23 24 // use stringBuffer to create only one object 25 long begin2 = System. currentTimeMillis (); 26 StringBuffer str2 = new StringBuffer (); 27 for (int I = 0; I <10000; I ++) {28 str2.append (I ); 29} 30 System. out. println (str2); 31 long end2 = System. currentTimeMillis (); 32 System. out. println (end2-begin2); // output: 11 time spent very little 33} 34}
StringBuilder class

Summary:

1. String: the String cannot be changed;

2. StringBuffer: Variable, thread security;

3. StringBuilder: variable. It is thread-insecure and has higher performance than StringBuffer;

4. String, StringBuffer, and StringBuilder all represent character sequences, and the interfaces are CharSequence;

5. There are many StringBuffer and StringBuilder methods that are the same as those in String;

Unique Method: it can be used to change the methods (add, delete, insert, replace, and reverse) in object values)

 

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.