Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Declare String Variable Outside of Try Block

 
Shankar sanjay
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we definite String object in side try block? I am using that string within try block?

Is there any difference in performance wise?



Example 1:

try
{

String tempString=null; //Declare Inside of try

//some operation done
}
catch (Exception ex)
{
// Not using tempString here
}

Example 2:

String tempString=null; //Declare outside of try

try
{
//some operation done
}
catch (Exception ex)
{
// Not using tempString here
}
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can. I don't think there is any performance difference between the two.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12344
39
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the 'code' tags whenever posting source code, even for something as simple as this.

My suggestion would be to declare the variable where it MAKES SENSE to declare it, and only worry about performance later. Where you declare it will determine its scope, which should be a greater concern than performance.
 
byronc Ozzie
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why does everyone always say, "dont worry about performance" - i have to optimize a system now fro latency and i wish this has been done all the way through
 
Tim Holloway
Bartender
Posts: 18423
60
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
byronc Ozzie wrote:why does everyone always say, "dont worry about performance" - i have to optimize a system now fro latency and i wish this has been done all the way through


Because one of the best ways to end up with software that's neither performant nor maintainable is to do premature optimization. Especially low-level optimization.

I've been tuning systems a long, long time now. Everything from IBM mainframes to dedicated real-time process controllers. And there's thing I'm certain of:

Inefficiency is never where you "know" it is.

Probably the worst case I ever saw was crashing a mainframe every afternoon. Do you have any idea how difficult it is to crash a mainframe? Those things are built from the hardware level up not to crash. The problem turned out to be a 1-line change to a config file.

I've seen sorts that performed abominably because someone used a heap or quicksort on data that was already nearly in order (which is worst-case for these sorts). A shellsort made all the difference, even though it's "inefficient".

You'll rarely see that much benefit by tweaking individual instructions or compiler settings. However, if you kept your architecture open, swapping in a better algorithm can improve things by several orders of magnitude. But it's hard to swap algorithms on a micro-optimized program, because they tend to have too much internal coupling.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why does everyone always say, "dont worry about performance" - i have to optimize a system now fro latency and i wish this has been done all the way through


Tim's comment is exactly correct. There have been many many bizarre rumours floating around in java from the beginning (version 1.02) about X is faster than Y. Most of these have been costly distractions from Tim's main point - Get the Algorithms to suit the problem! Not the cool shortcut thing you just heard about but the algorithm that fits and can be understood later.

For a fun read, google "premature optimization is the root of all evil"

When you are sure you have the optimum algorithm, if performance is still a problem, there are plenty for tools for locating the problem.

Us old programmers have plenty of war stories - one of my favorite was the new grad who implemented this lovely linked list structure when all she really needed was to locate a couple of values in a file.

Bill
 
Mukesh Ranjan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shankar,
More you restrict the scope of variabe, more will be the memory management, because it will be soon collected by garbage collector and it will improve performance,
so think before declaring variable, like here if the variable is required only within try block then it should not be declared outside try block.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mukesh Ranjan wrote:Shankar,
More you restrict the scope of variabe, more will be the memory management, because it will be soon collected by garbage collector and it will improve performance,
so think before declaring variable, like here if the variable is required only within try block then it should not be declared outside try block.


This is completely untrue, and absolutely counter to all the excellent advice given previously in this thread. References aren't cleared on exiting a block. Sure, the registers that hold variables inside a block may be reused outside of the block, but there's no guarantee of that.

Do not think about nonsense like this while programming. Declare variables where it makes sense to declare them -- and this is usually as close to their first use as possible.
 
Mukesh Ranjan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disagreed!!
why not to think for optimization at the time of development itself rather than after having the system crash,

I request people to maintain himself before putting their words at public forum
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mukesh Ranjan wrote:Disagreed!!
why not to think for optimization at the time of development itself rather than after having the system crash,

I request people to maintain himself before putting their words at public forum


For the reasons already detailed above. We've got an example right here, Mukesh is mistaken (sorry for using you as an example Mukesh) and following through that mistake could end up with software that is harder to maintain to no discernable benefit.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mukesh Ranjan wrote:why not to think for optimization at the time of development itself


Two reasons as least:

1) the compiler and JVM's JIT get better every release, and these days, they are better than most progammers.
2) the cost of engineers is far higher than the cost of hardware. Getting code to work properly, in all cases, with real world volumes is hard. Its easier to optimize, if needed, once the code actually works.

A correlary to #2 is that very few projects actually are well defined when they start, so what you think is important at the start of development may not even be in the project by the time the project manager has changed things a few times.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The real development time optimization is choosing the right pattern and the right algorithm.

No line by line optimization can save a project that picked the wrong pattern / algorithm at phase 1. See my earlier horror story.

Bill
 
SumitPal Pal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a famous quote from Donald Knuth - "Premature Optimization is the root of all Evil"
This is indeed very very true.

While programming - concentrate on the business logic and making sure you do the work correctly and not think about optimization.

The JVM has come a long long way since Java was released 12-14 years back.
The amount of optimization the JVM does now is mind boggling. ( if you are interested please read the articles from Brian Goetz ) - where he clearly says that micro benchmarks on the JVM makes no sense now - since the JVM uses lot of smart algorithms to optimize the programs.

So please let us not think we can outwit either the GC / JVM and write code that is smarter than either of them and write programs based on those assumptions.

It is always best to declare a variable where you need it in my opinion.
 
Tim Holloway
Bartender
Posts: 18423
60
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I spent a lot of my early years doing assembly-language programming. Back then, software companies would brag about their products being in assembler, because it was (de facto) "more efficient". Except of course, the only sort you can write in IBM mainframe assembler in one page or less is a bubble sort, so you saw a lot of bubble sorts where something more appropriate would have been better.

In fact, the main reason we used assembler was that we were doing OS-level programming, and none of the IBM language implementations was appropriate for the specialized environments that you have inside the OS itself. Ironically, Honeywell's GCOS OS system code could be programmed in COBOL, but that's a whole different story.

So we tried various libraries, macro packages, and third-party compilers in the ongoing struggle to be more productive. C wasn't yet an option.

One day we got a new Pascal compiler from IBM. The code it generated was so tight that doing the same degree of optimization manually would have been prohibitive. And, it fine-tuned every line of object code each and every time you made a source code change. We're talking major restructuring of the object code in some cases done at the drop of a hat.

It was at that point that I wrote off per-instruction optimization for anything more complex than interrupt service routines or similarly short, simple time-critical functions. It wasn't necessarily that I couldn't do the same degree of optimization myself, but the compilers were now doing things in milliseconds that would take me weeks to accomplish.
 
Why fit in when you were born to stand out? - Seuss. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!