Home > Back-end >  Why the program error, and also what the program returns the type of data
Why the program error, and also what the program returns the type of data

Time:09-29

Public class Test {

Public static Object XXXX () {
Return 1 l;
}

Public static void main (String [] args) {
//int I=(int) XXXX ();
//System. Out. Println (I);
}

}

CodePudding user response:

CodePudding user response:

The method name in front of what type, back is what kind,
1 l said Long integer data, the return type is a Long, involve the upcasting, become the Object, all the Java class is a subclass of Object class,
Packaging cannot cast each other between data types,
You can like this:
 
Long I=(Long) XXXX ();
System.out.println(i);

If you really want to use the int type that like this:
 
Int I=Integer. ParseInt (XXXX (), toString ());
System.out.println(i);

CodePudding user response:

refer to the second floor Little5 response:
method name in front of what type of return is what type,
1 l said Long integer data, the return type is a Long, involve the upcasting, become the Object, all the Java class is a subclass of Object class,
Packaging cannot cast each other between data types,
You can like this:
 
Long I=(Long) XXXX ();
System.out.println(i);

If you really want to use the int type that like this:
 
Int I=Integer. ParseInt (XXXX (), toString ());
System.out.println(i);

Since the return is a Long, why write Long I=(Long) XXXX ()

CodePudding user response:

Turn not long, not automatically split open a case

CodePudding user response:

reference 4 floor m0_47020112 response:
turn not long, not automatic unpacking it

Need, XXXX () method in return automatically convert 1 l to Object type and returns
Now you want to put an Object type value is assigned to a Long, of course, to force converted

In fact also can use int I=(Integer) XXXX ();
Like the Integer value is assigned to an int variable is automatically split open a case

CodePudding user response:

reference qq_42926427 reply: 3/f
Quote: refer to the second floor Little5 response:
method name in front of what type of return is what type,
1 l said Long integer data, the return type is a Long, involve the upcasting, become the Object, all the Java class is a subclass of Object class,
Packaging cannot cast each other between data types,
You can like this:
 
Long I=(Long) XXXX ();
System.out.println(i);

If you really want to use the int type that like this:
 
Int I=Integer. ParseInt (XXXX (), toString ());
System.out.println(i);

Since the return is a Long, why write Long I=(Long) XXXX ()


Run time is Long, but the compiler doesn't know, he just know that you return the Object type of Object, so in order to compile, you have to turn a strong

CodePudding user response:

Personal opinion:
Although you return is long, but the return value is the Object of XXXX (), will be long this value to the Object, then you use the basic data type int to cast the Object is not only to the wrapper class long long or int wrapper classes Integer to turn,

CodePudding user response:

reference qq_42926427 reply: 3/f
Quote: refer to the second floor Little5 response:
method name in front of what type of return is what type,
1 l said Long integer data, the return type is a Long, involve the upcasting, become the Object, all the Java class is a subclass of Object class,
Packaging cannot cast each other between data types,
You can like this:
 
Long I=(Long) XXXX ();
System.out.println(i);

If you really want to use the int type that like this:
 
Int I=Integer. ParseInt (XXXX (), toString ());
System.out.println(i);

Since the return is a Long, why write Long I=(Long) XXXX ()

Brother, this is typical of concrete to the abstract, and abstract to concrete,
The return type is Object, but the actual return is Long, the equivalent of the Long abstract Object, that is an up transformation process, concrete to the abstract,
But you here to use the specific type, so it needs to happen downward transition, abstract to concrete, it is because we know that the real is Long, if you use someone else's method return value, don't know, you need the if instanceof type (object) to judge,
Of course, if you directly so that the Object I=XXXX (); Don't turn, but when you want to use specific numerical, or need to transform, after all, the Object is abstract, like men and women are human beings, a method return type is a human, but returned to the Object of a woman, this is true, but you want to use the method return values gave birth to a child, woman must be converted to the specific Object,
  • Related