Business
Jobs
  • About Us
  • Solutions
    • Candidates
      Highly qualified candidates in 48h.
    • Assessments
      500+ technical and psychological assessments, plus anti-fraud system.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Pay in 15+ LATAM countries without setting up a local entity.
  • Pricing
  • Jobs

0

199
Views
La mejor manera de "negar" una instancia de

Estaba pensando si existe una forma mejor/más agradable de negar una instanceof de Java. En realidad, estoy haciendo algo como:

 if(!(myObject instanceof SomeClass)) { /* do Something */ }

Pero creo que debería existir una sintaxis "hermosa" para hacer esto.

¿Alguien sabe si existe y cómo se ve la sintaxis?


EDITAR: Por hermoso, podría decir algo como esto:

 if(myObject !instanceof SomeClass) { /* do Something */ } // compilation fails
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

No, no hay mejor manera; el tuyo es canónico.

over 4 years ago · Santiago Trujillo Report

0

No sé qué imaginas cuando dices "hermoso", pero ¿y esto? Personalmente, creo que es peor que la forma clásica que publicaste, pero a alguien le podría gustar...

 if (str instanceof String == false) { /* ... */ }
over 4 years ago · Santiago Trujillo Report

0

Podrías usar el método Class.isInstance :

 if(!String.class.isInstance(str)) { /* do Something */ }

... pero todavía es negado y bastante feo.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!