Sign Up

Sign Up to our social questions and Answers to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In
Continue with Google
or use


Have an account? Sign In Now

Sign In

Login to our social questions & Answers to ask questions, answer people’s questions & connect with other people.

Sign Up Here
Continue with Google
or use

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Continue with Google
or use

Forgot Password?

Need An Account, Sign Up Here

You must login to ask a question.

Continue with Google
or use

Forgot Password?

Need An Account, Sign Up Here

Sorry, you do not have permission to add post.

Continue with Google
or use

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Oraask Logo Oraask Logo
Sign InSign Up

Oraask

  • Write
    • Add A New Post
    • Ask A Question

Oraask Navigation

Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Categories
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Dev Tools
    • Online Compiler
    • Base64 Converter
    • Oraask XML Formatter
    • Oraask JSON Formatter
  • Wiki
    • SQL Tutorials
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials

Googler

Explorer
Ask Googler
48 Visits
1 Follower
0 Questions
Home/ Googler/Answers
  • About
  • Questions
  • Answers
  • Best Answers
  • Posts
  • Polls
  • Asked Questions
  • Comments
  1. Asked: January 6, 2018In: Java

    How to avoid != null statements in java ?

    Googler
    Googler Explorer
    Added an answer on February 28, 2018 at 8:51 pm

    If you use (or planning to use) JetBrains IntelliJ IDEA, a Java IDE, you can use some particular annotations developed by them. Basically, you've got @Nullable and @NotNull. You can use in method and parameters, like this: [code] @NotNull public static String helloWorld() { return "Hello World"; } [Read more

    If you use (or planning to use) JetBrains IntelliJ IDEA, a Java IDE, you can use some particular annotations developed by them.

    Basically, you’ve got @Nullable and @NotNull.

    You can use in method and parameters, like this:

    [code]
    @NotNull public static String helloWorld() {
    return “Hello World”;
    }
    [/code]

    or

    [code]
    @Nullable public static String helloWorld() {
    return “Hello World”;
    }
    [/code]

    The second example won’t compile (in IntelliJ IDEA).

    When you use the first helloWorld() function in another piece of code:

    [code]
    public static void main(String[] args)
    {
    String result = helloWorld();
    if(result != null) {
    System.out.println(result);
    }
    }
    [/code]

    Now the IntelliJ IDEA compiler will tell you that the check is useless, since the helloWorld() function won’t return null, ever.

    Using parameter

    [code]
    void someMethod(@NotNull someParameter) { }
    [/code]

    if you write something like:

    [code]
    someMethod(null);
    [/code]

    This won’t compile.

    Last example using @Nullable

    [code]
    @Nullable iWantToDestroyEverything() { return null; }
    [/code]

    Doing this

    [code]
    iWantToDestroyEverything().something();
    [/code]

    And you can be sure that this won’t happen. 🙂

    It’s a nice way to let the compiler check something more than it usually does and to enforce your contracts to be stronger. Unfortunately, it’s not supported by all the compilers.

    In IntelliJ IDEA 10.5 and on, they added support for any other @Nullable @NotNull implementations.

    See blog post More flexible and configurable @Nullable/@NotNull annotations.

    See less
      • 0
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
1 2

Sidebar

Adv 250x250

Explore

  • Categories
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Dev Tools
    • Online Compiler
    • Base64 Converter
    • Oraask XML Formatter
    • Oraask JSON Formatter
  • Wiki
    • SQL Tutorials
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials

Footer

Oraask

About

Oraask is a website for developers and software engineers who want to learn new skills, share their knowledge, and solve their coding problems. Oraask provides free content on various programming languages and topics, such as Oracle, Python, Java, etc. Oraask also allows users to ask questions and get answers from other members of the community.

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy
  • Terms & Conditions

Follow

Oraask is licensed under CC BY-NC-SA 4.0Oraask CopyrightOraask CopyrightOraask CopyrightOraask Copyright

© 2019 Oraask. All Rights Reserved
With Love by Oraask.