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

  • SEARCH
  • Home
  • 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 1031163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:53:06+00:00 2026-05-16T13:53:06+00:00

I need to do something like this, if (first_var > second_var) int difference =

  • 0

I need to do something like this,

if (first_var > second_var)
  int difference = first_var - second_var;
if (first_var < second_var)
  int difference = second_var - first_var;

When I try to compile this, an error comes stating the variable ‘difference’ may not have been initialized. Making the variable ‘difference’ global doesn’t help either.

  • 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-16T13:53:07+00:00Added an answer on May 16, 2026 at 1:53 pm

    The issues that you need to learn are:

    • The scope of variables
    • Block declaration and how that creates new scopes
    • Why you should prefer to use {...} block for if statements
    • How to guarantee definite assignment
    • When to use if-else instead of if (something) {...} if (!something) {...}

    By the way, the idiomatic way to find the difference of two values is:

    int difference = Math.abs(firstVar - secondVar);
    

    Do note that Math.abs has a corner case: when the argument is Integer.MIN_VALUE, the returned value is Integer.MIN_VALUE. That’s because -Integer.MIN_VALUE == Integer.MIN_VALUE. The issue here is 32-bit two’s complement representation of numbers.

    References

    • JLS 16 Definite Assignment
    • JLS 14.4.2 Scope of Local Variable Declarations
    • JLS 14.9.2 The if-then-else Statement
    • Math.abs(int)
    • Wikipedia/Two’s complement (read: the most negative number)

    Attempt #1: Declaring before the if

    Here’s one attempt to fix the snippet, by declaring the variable before the if

    int difference;
    if (first_var > second_var) {
      difference = first_var - second_var;
    }
    if (first_var < second_var) {
      difference = second_var - first_var;
    }
    
    // note: difference is not definitely assigned here!
    //           (but at least it's in scope!)
    

    We now have a problem with definite assignment: if first_var == second_var, the variable difference is still not assigned a value.


    Attempt #2: Initializing at declaration

    Here’s a second attempt:

    int difference = 0;
    if (first_var > second_var) {
      difference = first_var - second_var;
    }
    if (first_var < second_var) {
      difference = second_var - first_var;
    }
    
    // note: difference is in scope here, and definitely assigned
    

    Beginners tend to do this, but this precludes the possibility of making difference a final local variable, because it’s possibly assigned value twice.


    Attempt #3: if-else

    Here’s a better attempt:

    final int difference;
    if (first_var > second_var) {
      difference = first_var - second_var;
    } else {
      difference = second_var - first_var;
    }
    
    // note: difference is in scope, and is definitely assigned here,
    //       (and declared final)
    

    There are still ways to improve on this.


    Attempt #4: The ternary/conditional operator

    Once you’re more comfortable with the language and programming, you may use the following idiom:

    final int difference = (first_var > second_var) ? first_var - second_var
                                                    : second_var - first_var;
    

    This uses the ?: ternary/conditional operator. Do be careful with this operator; it has some behaviors that may be surprising, and it definitely can be abused. Use carefully, judiciously, idiomatically.

    References

    • JLS 15.25 Conditional Operator ?:
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need something like this http://jonraasch.com/blog/a-simple-jquery-slideshow but w/o the absolute positioning. Is it possible?
Ok, I need something like this: datediff(second, date_one, date_two) < 1 dates are stored
What I need is something like this: /<[\w\d]+ ([\w\d]+\=[w\d])+\/>/ Something that would match several
I need to do something like this: <input type=button value=click id=mybtn onclick=myfunction('/myController/myAction', 'myfuncionOnOK('/myController2/myAction2', 'myParameter2');',
I need to do something like this: Section 1, Chapter 1 title is Dogs
Hello I need to do something like this: UPDATE tbl SET pozn=CONCAT(pozn, '+attach_str+') WHERE
I guess this is a simple question. I need to do something like this:
I have something like this: function showFunction () { // need position and place
I need to get to work something like this: ./foo.py [-b option [-a]] with
I need(for rapid prototyping and libraries integration) something like this(extensions for usual arrays) double[]

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.