java people..wtf is going on here.
#1
class Cat { }
class Dog {
public static void main(String [] args) {
Dog d = new Dog();
System.out.println(d instanceof Cat);
}
}


Why the fuck won't that compile? you can't compare an object to some other object? do some swapping and you see that:

System.out.println(d instanceof Cat); //does not compile
System.out.println(d instanceof Dog); //compiles
if(d instanceof Cat); //also compiles

So, i know this isn't how you'd normally use this..but something funky is going on here. For some reason when it's used as an expression there it fails. WTFZO!?
2013 Cadillac ATS....¶▅c●▄███████||▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅||█~ ::~ :~ :►
2008 Chevy Malibu LT....▄██ ▲  █ █ ██▅▄▃▂
1986 Monte Carlo SS. ...███▲▲ █ █ ███████
1999 F250 SuperDuty...███████████████████►
1971 Monte Carlo SC ...◥☼▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙☼◤
  Reply
#2
and also

boolean b = (d instanceof Cat);
System.out.println(b);
also compiles
2013 Cadillac ATS....¶▅c●▄███████||▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅||█~ ::~ :~ :►
2008 Chevy Malibu LT....▄██ ▲  █ █ ██▅▄▃▂
1986 Monte Carlo SS. ...███▲▲ █ █ ███████
1999 F250 SuperDuty...███████████████████►
1971 Monte Carlo SC ...◥☼▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙☼◤
  Reply
#3
lookup what instanceof compares. you are attempting to check inheritance w/o using inheritance.

instanaceof sucks, use .class for that kind of shit.
  Reply
#4
2 classes in 1 file?

Does dog have a default constructor?

What error message are you getting exactly?
Why do people just post what they are thinking? Without thinking.

2012 Ford Mustang
1995 BMW 540i/A
1990 Eagle Talon TSI AWD
  Reply
#5
ViPER1313 Wrote:2 classes in 1 file?

Does dog have a default constructor?

What error message are you getting exactly?

it's an example, doesn't matter, he's probably getting an Inconvertible types error.
  Reply
#6
the compiler inserts a default constructor automatically as long as it isn't overloaded already...

and I don't know what the exact error message is...i just noticed it when I was reading something
2013 Cadillac ATS....¶▅c●▄███████||▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅||█~ ::~ :~ :►
2008 Chevy Malibu LT....▄██ ▲  █ █ ██▅▄▃▂
1986 Monte Carlo SS. ...███▲▲ █ █ ███████
1999 F250 SuperDuty...███████████████████►
1971 Monte Carlo SC ...◥☼▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙☼◤
  Reply
#7
incompatable types
2013 Cadillac ATS....¶▅c●▄███████||▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅||█~ ::~ :~ :►
2008 Chevy Malibu LT....▄██ ▲  █ █ ██▅▄▃▂
1986 Monte Carlo SS. ...███▲▲ █ █ ███████
1999 F250 SuperDuty...███████████████████►
1971 Monte Carlo SC ...◥☼▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙☼◤
  Reply
#8
try casting to Object first and it should work
SM #55 | 06 Titan | 12 Focus | 06 Exige | 14 CX-5
  Reply
#9
well, i know. I just think it's lame i can't use it in the method directly. The other ways work.

And i guess besides, i would never be doing this anyway..just thought it was interesting.
2013 Cadillac ATS....¶▅c●▄███████||▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅||█~ ::~ :~ :►
2008 Chevy Malibu LT....▄██ ▲  █ █ ██▅▄▃▂
1986 Monte Carlo SS. ...███▲▲ █ █ ███████
1999 F250 SuperDuty...███████████████████►
1971 Monte Carlo SC ...◥☼▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙☼◤
  Reply
#10
instanceof is when you dont know the concrete type of the object and you are using some abstract parent type to point to the object. if you are using a more concrete type, then you made a mistake. "you would never be doing this anyway" is exactly why it throws an error.
it does seem a little strange especially if coming from c++, but java babysits you more, which IMO is a good thing. no type saftey is a huge pita
SM #55 | 06 Titan | 12 Focus | 06 Exige | 14 CX-5
  Reply


Forum Jump: