Java API ------ Boolean valueOf () method
// Excerpt from ocjppublic void testIfA () {if (testIfB ("True") {System. out. println ("True");} else {System. out. println ("Not true") ;}} public Boolean testIfB (String str) {return Boolean. valueOf (str);} What is the result when method testIfA is invoked? A. trueB. not trueC. an exception is thrown at runtime. d.
not to be null. However, with the third approach, you will not have to worry about whether object is a null value. For the sake of explaining the problem, let's analyze the relevant source code. JDK string.valueof (Object) source code is as follows:public static String valueOf (Object obj) {return (obj = = null)? "Null": obj.tostring (); }From the above source code can be very clear to see the null value without worrying reasons. However, this also g
Case1:object object to StringIt should be emphasized that the string.valueof () method, when the argument is type is object, and the value is null when he handles the waypublic static String valueOf (Object obj) {return (obj = = null)? "Null": obj.tostring ();}So in the back of Chengdu will not error but logically may be wrong, the general habit of writing is Str!=null str.length () >0When it is obtained by str = string.valueof (obj), the above judgm
The enumeration classes are new after Java 5, can be used to encapsulate constants, and can also provide some methods for the use of constants.Defines the syntax for an enumeration class: Public enum enumname{ //must be placed on the first row of member 1 (A, B ... ), member 2 (A, B ... ), member 3 (A, b ...). )........; //a,b can not Private Type A; Private Type B;
1.parse () is Simpledatefomat inside the method, you say should be parseint () or parsefloat () this method,As the name implies, parseint () is the conversion of string type to int type.such as String a= "123"; int b = Integer.parseint (a);So b is equal to 123.The 2.ValueOf () method, such as integer.valueof (), converts the string type to an integer type (note: It is an integer type, not an int type, and the int type is a simple type that represents
"); /*ordinal () returns the index position of the enumeration value in the enumeration, starting at 0*/System.out.println (ColorSelect.red.ordinal ());//0System.out.println (ColorSelect.green.ordinal ());//1System.out.println (ColorSelect.yellow.ordinal ());//2System.out.println (ColorSelect.blue.ordinal ());//3 /*enumeration implements the Java.lang.Comparable interface by default*/System.out.println (ColorSelect.red.compareTo (Colorselect.green)); System.out.println (Season
In the actual development and application of Java projects, it is often necessary to use the basic function of converting objects to string. This article will make a summary of the common conversion methods. Common methods are object.tostring (), (String) objects to be converted, string.valueof (object), and so on. The following is an analysis of these methods.
Method 1:Using the Object.ToString () method, see the following example:
Object object =
One, the codePackage Base.lang;/*** Classname:stateenum* @Description: TODO* @author fuming* @date November 27, 2016*/public enum Stateenum{Add the specified constant for the enumerationOnline (10),Offline (20);You must add a constructor, variable, to get the value of the variableprivate int mstate=0;Private Stateenum (int value){Mstate=value;}/*** @return The actual return value of the enumeration variable*/public int getState (){return mstate;}}Ii.
this constructor: protected Enum (String name, int ordinal ).
2. if you want to know more about Enum, you can use the decompilation tool to decompile the enumeration defined by yourself. It is a common class, but the Java language specification is limited from the Code perspective, execute javap com. enumtest. the Color command is as follows:
Code 2]
package co
();
// Implement the abstract method of the Enum class
Public static hr. test. Color valueOf (java. lang. String arg0 );
}
The features and usage of enumeration classes defined by enum are described in detail below. The following uses Color as an example)
1. The Color enumeration class is a class which cann
public static Hr.test.Color valueOf (java.lang.String arg0);
}
The following is a detailed description of the characteristics and usages of enum classes defined by the enum. (color example is used later)1, the Color enumeration class is class, and is a final class that cannot be inherited. Its enumeration value (Red,blue ...) are all class static constants
enummap
Public class test {public static void main (string [] ARGs) {// use enumset for enumset Principle Analysis
although the syntax structure of Enum is different from that of class, a class file is generated after compilation by the compiler. This class file is decompiled to show that a class is actually generated, which inherits java. Lang. Enum
.
){return x - y;}},TIMES("*"){double apply(double x,double y){return x * y;}},DIVIDE("/"){double apply(double x,double y){return x / y;}};private final String symbol;Operation(String symbol){this.symbol = symbol;}@Overridepublic String toString(){return symbol;}abstract double apply(double x,double y);}
Run as Java Application,we can see the console.the result shows operations?
1234
2.000000 + 4.000000 = 6.0000002.000000 -
Tips"Effective Java, third Edition" an English version has been published, the second edition of this book presumably many people have read, known as one of the four major Java books, but the second edition of 2009 published, to now nearly 8 years, but with Java 6, 7, 8, and even 9 of the release, the Java language has
Gain an in-depth understanding of enum in Java and Javaenum
We can use either the public static final String constant or the enum String constant to represent a limited number of things in a program. In general, the former is simple, but it cannot provide more information. In Java,
1. Application of Enum, including definition, traversal, switch, enumset, enummap, etc.
Import Java. util. enummap; import Java. util. enumset; public class enumtest {// defines an Enum Enumeration type, including two instances on, off public Enum state {On, off }; // test t
normal class?I'm telling you, actually, the enum in Java is just a class, and defining an enum is defining a class, which is a testament to Sexenum.class's anti-compilation.Javap-p Sexenum.classCompiled from "Sexenum.java"Public Final class Sexenum extendsjava.lang.enumpublic static final Sexenum GIRL;public static final Sexenum boy;private static final sexenum[
Java enum (enumeration) and javaenum
In actual programming, there are often such "datasets", their values are stable in the program, and the elements in the "dataset" are limited.
For example, seven data elements from Monday to Sunday constitute a "dataset" for a week, and four data elements for spring, summer, and autumn constitute a "dataset" for the Four Seasons ".
E
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.