Sign Up

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

Have an account? Sign In

Have an account? Sign In Now

Sign In

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

Sign Up Here

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.

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6116823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:15:03+00:00 2026-05-23T15:15:03+00:00

I know that when I read the answer to this I will see that

  • 0

I know that when I read the answer to this I will see that I have overlooked something
that was under my eyes. But I have spent the last 30 minutes trying to figure it out myself
with no result.

So, I was writing a program in Java 6 and discovered some (for me) strange feature.
In order to try and isolate it, I have made two small examples.
I first tried the following method:

private static int foo()
{
    return null;
}

and the compiler refused it: Type mismatch: cannot convert from null to int.

This is fine with me and it respects the Java semantics I am familiar with.
Then I tried the following:

private static Integer foo(int x)
{
    if (x < 0)
    {
        return null;
    }
    else
    {
        return new Integer(x);
    }
}

private static int bar(int x)
{
    Integer y = foo(x);

    return y == null ? null : y.intValue();
}

private static void runTest()
{
    for (int index = 2; index > -2; index--)
    {
        System.out.println("bar(" + index + ") = " + bar(index));
    }
}

This compiles with no errors! But, in my opinion, there should be a type conversion error
in the line

    return y == null ? null : y.intValue();

If I run the program I get the following output:

bar(2) = 2
bar(1) = 1
bar(0) = 0
Exception in thread "main" java.lang.NullPointerException
    at Test.bar(Test.java:23)
    at Test.runTest(Test.java:30)
    at Test.main(Test.java:36)

Can you explain this behaviour?

Update

Thank you very much for the many clarifying answers. I was a bit worried because
this example did not correspond to my intuition. One thing that disturbed me was
that a null was being converted to an int and I was wondering what the result would
be: 0 like in C++? That would hae been very strange.
Good that the conversion is not possible at runtime (null pointer exception).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:15:03+00:00Added an answer on May 23, 2026 at 3:15 pm

    Let’s look at the line:

    return y == null ? null : y.intValue();
    

    In a ? : statement, both sides of the : must have the same type. In this case, Java is going to make it have the type Integer. An Integer can be null, so the left side is ok. The expression y.intValue() is of type int, but Java is going to auto-box this to Integer (note, you could just as well have written y which would have saved you this autobox).

    Now, the result has to be unboxed again to int, because the return type of the method is int. If you unbox an Integer that is null, you get a NullPointerException.

    Note: Paragraph 15.25 of the Java Language Specification explains the exact rules for type conversions with regard to the ? : conditional operator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that it is possible to read commands output with a pipe? But
https://search.twitter.com/search.json?q=doug How do I read this like VIEW SOURCE, so that I know what
I know that |DataDirectory| will resolve to App_Data in an ASP.NET application but is
I know that XHTML doesn't support nested form tags and I have already read
I'm almost certain I know the answer to this question, but I'm hoping there's
I know that read() is a blocking call unless I make the socket non-blocking.
I know that generics are used to achieve type safety and i frequently read
I'm working on PostgreSQL 8.4 in read committed mode. I know that for each
Anybody in the know of a viewer (or Notepad++ plugin) that can read txt
I know that I can do something like $int = (int)99; //(int) has a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.