简体   繁体   中英

Variable types in Java?

Sometimes I see methods that have a parameter like public method(ObjectName variable)

Does that mean this method only accepts ClassName 's objects? For example from another class?

it might be a simple question but I am used to seeing only int's , String s etc. as parameters. I guess some methods can accept instances of other classes? How does compiler know then that the class in the parameter is valid? Does it know it from imports?

Basically, yes to all your questions. Some methods accept instances of classes, both others or this class. Compiler knows by imports and the other classes in the package. If you have a more specific question put some code up.

The formal rules can be found at http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12.2

Basically if a method accepts class A then it can also accept any subclass of A . A can be any class, BigInteger InputStream URL etc. If a method can accepts interface B then it will also accept any class that implements B .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM