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 6626143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:51:32+00:00 2026-05-25T21:51:32+00:00

Look at this Java code: class PerformanceTest2{ public static void main(String args[]){ Long sum

  • 0

Look at this Java code:

class PerformanceTest2{

    public static void main(String args[]){

        Long sum = 0L;

        for(int i=0;i<Integer.MAX_VALUE;i++)
            sum += i;

        System.out.println("Sum = " + sum);
    }
} 

It is observed that it takes longer for this code since sum is ‘Long’ & not ‘long’. So in every iteration what happens is:

sum = new Long(sum.longValue() + i); (for sum+=i;)

So, a new object is created every time. Doesn’t Java support C++ like feature of returning a reference so that we could’ve written (possibly):

sum.longValue() += i;

possibly not having to create sum object every time around the loop? Am I right?

  • 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-25T21:51:32+00:00Added an answer on May 25, 2026 at 9:51 pm

    Well, it doesn’t call the constructor. It uses:

    for (int i = 0; i < Integer.MAX_VALUE; i++)
    {
       long tmp = sum.longValue(); // Unboxing
       tmp += i;
       sum = Long.valueOf(tmp); // Boxing
    }
    

    The wrapper objects are deliberately immutable – they could easily have been designed to be mutable, but immutability is often a very useful feature. If you want to write your own mutable wrapper type, you’re very welcome to – at which point you could have code such as:

    LongWrapper sum = new LongWrapper(0L);
    for (int i = 0; i < Integer.MAX_VALUE; i++)
    {
        sum.add(i);
    }
    System.out.println("Sum = " + sum);
    

    Or possibly:

    LongWrapper sum = new LongWrapper(0L);
    for (int i = 0;i < Integer.MAX_VALUE; i++)
    {
        sum.setValue(sum.getValue() + i);
    }
    System.out.println("Sum = " + sum);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this Java code: public class Main { public static void main(String[] args) {
Here is the code: import java.lang.*; import java.io.*; class string { public static void
Let's look at the simple Java code in the following snippet: public class Main
In Java, a 'static method' would look like this: class MyUtils { . .
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
My main class look like this: public class Soundboard extends Activity { SharedPreferences preferences;
In Java, it would look like this: class Foo { float[] array; } Foo
Look ath this method of ThreadPoolExcecutor: public void execute(Runnable command) { ... if (runState
I have a .java src file that looks like this: class Test { public
I have several objects that look like this: class PhoneNumber { String getNumber(); String

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.