Talking about integer and string in Java

Source: Internet
Author: User
Tags try catch

There are eight basic data types in http://qxzxcjq-126-com.iteye.com/blog/883283 java: int, char, Boolean, Byte, long, double, float, short. Java is an object-oriented programming language, and data is an object in Java. The object we created with the base data type, such as int x = 0, where x has only the value of this property, there are no (or very few) other methods. This is inconvenient when some need to manipulate the data, programmers need to write a lot of code to implement some common functions, increase the workload and the size of the program.

Therefore, each basic data type in Java has its corresponding reference class, or encapsulation class. The reference class such as int is Integer,char is character. These classes are classes that have been written in the Java compilation environment and are already written in a number of common methods for that data type, and these methods are optimized. When programming, we only need to use these classes to create objects, we can call the corresponding methods directly. There are pros and cons, when creating objects of these classes, the time and space used is larger than the basic data type, which increases the time complexity and space complexity of the program, so when applied, if not necessary, try to use the basic data type.

Below, I mainly describe the use of some methods in the integer class.

When it comes to classes, the first thing to say is how the object is constructed. Each class in Java has a construction method that is called when the object of the class is created. If the constructor method is not overridden in the method, the default format is: The class name Object name = new class name (), and when overridden, calls the corresponding method according to the arguments passed.

Two construction methods are overridden in the integer class. The first is an integer (int value), passing in an int value as the value of the object, which is equivalent to the statement: integer object name = (int) value. This method is better understood, no longer an example.

The second method constructs an object for the integer (String s) method Integer , which represents String the int value indicated by the parameter. parseIntconverts the string to an int value in the same way as a method (a value of cardinality 10). The method detects an exception, that is, when the content contained by string s is not a number, or an integer that is not resolvable, a numberformatexception is thrown.

Java code
    1. String s = "0101010";
    2. Integer i = new Integer (s);
    3. System.out.println (i);

The result is: 101010

Java code
    1. String s = "DFADFFCFA";
    2. Integer i = new Integer (s);
    3. System.out.println (i);

Exception in thread "main" Java.lang.NumberFormatException:For input string: "DFADFFCFA"
At java.lang.NumberFormatException.forInputString (Unknown Source)
At Java.lang.Integer.parseInt (Unknown Source)
At java.lang.integer.<init> (Unknown Source)

According to the above code, this construction method does not need to be tested with a try catch or throws, because the exception is a runtime exception, and the virtual opportunity is automatically thrown when the program runs.

Here are a few recent ways to do file compression.

1, parseint

In the integer class, the method named Parseint has two methods, with different arguments (overloads of the method). Only one strirng is passed in, that is, parseint (string s) is parsing the characters in a string in decimal, returning a static int. As with the second construction method mentioned above, this method is also required for the characters contained in the string, except that the first character can be an ASCII minus sign () that represents a negative value, and the ‘-‘ ‘\u002D‘ characters in the strings must be decimal digits. If it does not meet the requirements, it will also run as an error and an exception of type runtime. For specific applications, see the code below

Java code
    1. String s = "11234516";
    2. int i = Integer.parseint (s);
    3. System.out.println (i);

Output is 11234516

Java code
    1. String s = "112adf34516";
    2. int i = Integer.parseint (s);
    3. System.out.println (i);

Exception in thread "main" Java.lang.NumberFormatException:For input string: "112adf34516"
At java.lang.NumberFormatException.forInputString (Unknown Source)
At Java.lang.Integer.parseInt (Unknown Source)
At Java.lang.Integer.parseInt (Unknown Source)

Another parseint needs to pass in string and int two arguments, parseint (string s, int radix), use the cardinality specified by the second argument, and resolve the string argument to a signed integer. In addition to the ASCII minus sign (), where the first character can be used to represent a negative value ‘-‘ ‘\u002D’ , the character in the string must be a number of the specified cardinality. This is a method of converting string data into a certain binary, of course, as long as the characters in the string meet the requirements of the binary.

Java code
    1. String s = "125ADF";
    2. int i = Integer.parseint (s,16);
    3. System.out.println (i);

Output results: 1202911

The exception is no longer displayed.

There is a way to convert a string into a corresponding integer, and there is a way to convert an integer into a string.

Tobinarystring, Tooctalstring, tohexstring, and ToString all need to pass in an int that converts the value of the int to a binary, octal, 16-decimal string with a maximum of 0.

Java code
    1. String s = integer.tobinarystring (511);
    2. System.out.println (s);

The result is: 111111111

The above methods are used in the Huffman encoding compression of the integer method, very practical value.

In addition to the reference types of these basic data types, the class of data manipulation that we most often touch is the string class. The most common thing we do with string-type data is to find the index bit of one of the characters or substrings. This has a number of methods in the String class. Indexof (int char) returns the index bit of the first occurrence of the specified character in a string, and Indexof (int char,int fromIndex) is the index bit of the first occurrence of char from the FROMINDEX bit. lastIndexOf (int char) and lastIndexOf (int char,int fromIndex) are exactly the opposite, returning the first occurrence of the index bit from the end of the head.

Java code
    1. String s = "Adlfldcidfkasdcieij";
    2. int index = S.indexof ("L");
    3. int last = S.lastindexof ("L");
    4. System.out.println ("Index is:" +index);
    5. System.out.println ("Last is:" +last);

The result is: Index Is:2
Last Is:4

The usual operation is to intercept a segment of the string and get a new string. There are substring methods in the string class, as well as overloaded phenomena. substring (int beginindex) and substring (int beginindex, int endIndex) All get substrings of the original string, but the first one is from Beginindex to the end of the original string, The second type is a substring from beginindex to endIndex-1 bits. Here is to note that the substring method is included with the head does not contain the tail!

Java code
    1. String s = "Adlfldcidfkasdcieij";
    2. String snew = s.substring (5);
    3. String str = s.substring (5,s.length ()-1);
    4. System.out.println ("Snew is:" +snew);
    5. System.out.println ("Str is:" +STR);

Result: Snew is:d Cidfkasdcieij
STR is:d Cidfkasdciei

Note the difference between the two substrings.

The above is I in the compression process involves some of the string class and the use of some methods in the integer class, it is inevitable that there is a problem, please correct me.

Talking about integer and string in Java

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.