What is a best practice for objects created and literals

If I have to run a method several times, is it better to do this:
int i = 0;
public void test(){
     i = rand.nextInt();
     System.out.println(i);
} 
Or is it better to do:
public void test(){
     int i = 0;
     i = rand.nextInt()
     System.out.println(i);
}
Assuming that the variable 'i' will NOT again the program, considering that I have will take place probably this method a hundred times or more, I just want to know which is faster or better.

And if it was an object rather than a literal? -What changes it?

What happens if the method is static? -What changes it?

It is best to declare variables in the lowest scope possible. So, given the choice between these two pieces of code and other assumptions you mention, the second part of the code is better.

(By the way the rule is true because it makes your code easier to understand. In the second part of the code you posted, it is obvious that variable 'i' isn't used everywhere except in the "test" method In the first part of the code, the reader cannot tell. Less the player should reflect the code you write, the better code.)

However if the second part of the code is what you want, then it is too long. Why you initialize the variable to zero, and then in the next line assign another value to it? Why not simply initialize this another value in the first place?

It's a line too.

Then, if you are going to assign a value to a variable and then print the value of this variable, why not just print the value directly? Why have the variable at all?

This is so two lines too.

It is usually best to write less code, and no more code. Most code you write more code must contain errors. Beginners used to write a lot too much code. However, it is possible to go overboard with the idea of writing less code; It is possible to write a long and unreadable 'one-liners', which do the work, but when some other poor decoration needs to change to something else, it's impossible.

You will notice that I have not address the issue of the "faster" at all. This is because that things like when you declare variables are a triviality in terms of speed things run.

Tags: Java

Similar Questions

  • What are the best practices for creating only time data types, and not the Date

    Hi gurus,

    We use 12 c DB and we have a requirement to create the column with datatype of time only, if someone please describe what are the best practices for the creation of this.

    I would strongly appreciate ideas and suggestions.

    Kind regards
    Ranjan

    Hello

    How do you intend to use the time?

    If you are going to combine with DATEs or timestamps from a other source, then an INTERVAL DAY TO SECOND or NUMBER may be better.

    Will you need to perform arithmetic operations on time, for example, increase the time to 20%, or take an average?   If so, the NUMBER would be preferable.

    You are just going to display it?  In this case, DAY INTERVAL in SECONDS, DATE or VARCHAR2 would work.

    As Blushadow said, it depends.

  • What is the best practice for the enumeration for the ADF?

    Dear all,

    What is the best practice for the enumeration for the ADF?

    I need to add the enumeration to my request. ex: sex, marital status.

    How to deliver? Declarative custom components or is there another way?

    Thank you.
    Angelique

    Check out this topic - '5.3 fill view object Rows with static data' in Guide of Dev
    http://download.Oracle.com/docs/CD/E17904_01/Web.1111/b31974/bcquerying.htm#CEGCGFCA

  • What are the best practices for a new employee to learn inside the instance of their business of Eloqua as efficiently as possible?

    We have companies everything changed at some point in our lives. And we all go through the process in the first weeks, where you feel new and are just trying to figure out how not to get lost on your way in the mornings.

    On top of that, trying to familiarize yourself with your new company Eloqua instance can be a daunting task, especially if it's a large organization.

    What are the best practices for new employees to learn as efficiently and effectively as possible?

    I am in this situation right now. Moved to a much larger organization. It is a huge task trying to understand all the ins and outs not only society, but also of the eloqua instance, especially when she is complex with many points of integration. I find that most of the learning happens when I really go do the work. I spent a ton of time going through the programs, documentation, integrations, etc., but after awhile, it's all just words on a page and not absorbed.

    The biggest thing that I recommend is to learn how and why things are made the way they are currently, ask lots of questions, don't assume not that things work the same as they did with your previous employer.

    Download some base in place level benchmarks to demonstrate additional improvement.

    Make a list of tasks in the long term. As a new pair of eyes, make a list of things you'd like to improve.

  • What is the best practice for a 'regular' Server VMware and VDI environment?

    What is the best practice for a "regular" VMware Server and VDI environment?   A single environment (ESXi and SAN) can accommodate two if it is a whole new configuration?  Or even better to keep separate?

    Enjoying inputs.

    Quick and dirty answer is that "it depends."

    serioulsy, it depends really two things budget and IO.  If you had the money for two without then buy two and don't have to host your server environment and the other for VDI desktop, their IO profiles are completely different.

    If this is not the case, try to keep each type of use for their own dedicated LUN.

  • Best practices for managing exceptions and success messages.

    Hey people,

    These days I've been shooting packages to clean my application. And question came to my mind, ' should I treat my exceptions the right way?


    So I want to ask you met guys, what is the best practice for this? (I want to learn it until it's too late )

    Currently I have a function that returns "OK" if all goes well.


    return('OK');  

    Can I manage my exceptions like this

      EXCEPTION
        WHEN OTHERS THEN
          ROLLBACK;
          RETURN (SQLERRM);
    
    

    At THE SUMMIT, I have a process that calls the function and then checks if the function returned "OK".

         IF cRet not LIKE 'OK%' THEN
          RAISE_APPLICATION_ERROR(-20000,cRet);
         END IF;
    
    

    And in 'process Error Message' I put "#SQLERRM_TEXT #" so that I can see what error occurred.

    Question aside, how do you manage your messages of success?

    Currently in 'process success Message' put something along the lines "Action completed successfully". What to do about all the processes.

    Do you want to do differently?

    I really want to hear what you have to say since I'm tired of feeling like this is a terrible way to manage these things.

    Hi Para,

    Para wrote:

    I don't know of situations where my service throw exceptions like no_data_found.

    and I need to know that the process is not so I can get to see my # #SQLERRM_TEXT.

    I got this by increasing the error in the application (which I think is a bad way to go) if the return is anything other than 'OK '. I get my application error in the process of the apex, and not in my service.
    And I want to show the inline error in the notification. (Which I am currently with my approach).

    You can use APEX_ERROR. ADD_ERROR in your PL/SQL process to throw exceptions in Oracle APEX.

    Reference: Re: Re: error in the processing of the page management

    Kind regards

    Kiran

  • What is the best practice for creating Volumes

    Hello.

    I have storage SAN Dell Equiliser PS 6000 model. configured with RAID 50. and FEPs 4TB

    1. I want to present the server 2 hosts 2 TB and 2 TB Volumes. is it good to put it this way or

    2. can I submit each Volume separately to each Vertual Machine... ???

    My friends said that method 1 is good practice.

    3 and I use the IP address of the host to present volumes it's correctly.

    4. If I use host NIC to present Volumes. so can I use the same NETWORK card like switch virtual or not.

    Please give me the urgency to answer.

    Thank you.

    Hello

    Re: 1

    If you have a table of 4 TB, use all the available space on the table.  This will reduce performance long-term.  The table needs unallocated free space.  If you use replication or snapshots.

    Re: 2

    If you do not use a file system capable of cluster, then do not connect several servers the same volumes.  Each server has access to its own volume.  You can connect either using the hypervisor (AKA Raw Device mapped LUN) or directly from the virtual machine.  Which is known as the "Storage Direct".  If your server is running Windows, with this configuration, you can use the Host Integration Toolkit / edition of Microsoft (HIT / ME) to provide improved MPIO and better integration with SharePoint, MS SQL and MS Exchange.

    Addresses using IP is fine to control access.  It's a personal preference.  It is possible to make a mistake and allow multiple access to the server when using just IP addresses.  Or an address can be spoofed as well.  A safer option is to use a CHAP username / password combination.

    About your last two questions, I'm not sure that I completely understand.

    The iSCSI network must be on its own.  If each virtual machine or a physical host linking would have their own network interface cards.  Whether it's virtual network adapters or the physical network interface cards. With virtual switches, it should have devoted HW NIC and iSCSI traffic.  But those that can be shared by multiple virtual machines.

    Kind regards

    Don

  • What is the best practice for managing for results in doubles because of several sessions

    I work with PowerCLI 5.0.  I quite often find that I have a few sessions open at the same time connected to a server that hosts a particular virtual machine and vCenter.  I ran into a problem when I tried the cmdlet Add-USBController of VMware vSphere PowerCLI Reference book.

    The following lines are (roughly) in the extract cmdlet of the book:

    $vmView get - vm 'test ' | Get-View

    $vmView.ReconfigVM_Task ($spec)

    I got the following error "method call failed because [System.Object []] contains a method named"ReconfigVM_Task"" etc.

    It turns out that I was getting an array of vmViews for 'test' (instead of one), because I had a connection to the host and vCenter.  Once I deleted the host sessions, the script worked well.

    So now my question - what is the best way to mitigate this in my scripts? At least, I guess I need to make sure that I received only an object in the view of the GET. should I remove all duplicates? kill all other sessions when I run the script?

    Thank you

    Maureen

    The best method is to use the server parameter on cmdlets where it is available, in this way, you can limit the objects returned to this server for vSphere.

    If you have multiple sessions on the same server from vSphere open, you could direct the result of a Select-Object cmdlet.

    Like this

    Get-VM-name MyVM-Server $vcenter | Select - 1 first

  • Request for advice: generally speaking, what is the best practice for managing a paid and a free application?

    Hi all

    I recently finished my first app of cascades, and now I want to inspire of having a more feature rich application that I can then sell for a reasonable price. However, my question is how to manage the code base for both applications. Any have any "best practices", I would like to know your opinion.

    You use a revision control system? This should be a prerequisite...

    How the different versions of the application will be?

    Generally if you have two versions that differ only in terms of having a handful of features disabled in the free version, you must use exactly the same code base. You could even just it for packaging (build command) was the only difference, for example by adding an environment variable in one of them that would be checked at startup to turn paid options.

  • What is the best practice for the double management interfaces?

    Hello community!

    I'm upgrading to a few host ESX to ESXi 4.1U1 4.0 in the coming weeks. My question is about how to configure the management networks. Obviously in ESX 4.0 Classic I have a Service Console port (on vSwitch0) group and a group of ports VMkernel (also on vSwitch0) which provides my host with SC and vmotion capabilities, as we all know. Note: my vSwitch0 has two vmnic attached to it, is pending and is active. That's just how we have our double installation of switches, so it must be active / standby.

    I got to thinking (book the great from HA and DRS deepdive Duncap Epping and Frank Denneman), that I should consider carefully when my network mangement I improve these hosts to ESXi 4.1 - which of course done away with the Service Console and use the vmkernel instead.

    The question is, in which best practices and account with my setup: I have two vmkernel ports? If so, how should I configure each for traffic management and vmotion vmkernel?

    I think it will be a good discussion to have.

    Thank you all,

    Matt

    The NIC vSwitch0 value active/active, the vswif (and future vmkernel management) team of NIC Active vmnic6 and vmnic1 ensures and leaves the vMotion vmkernel NIC team as is.

    This will allow you to use two physical network interface cards at the same time while having a failover plan and keep your physically separate management and vMotion traffic.

    In the end:

    vSwitch: vmnic1 vmnic6 active, active.

    VMK (Mgt): active eve of vmnic1 vmnic6.

    VMK (vMotion): active standby, vmnic6 vmnic1.

  • What is the best practice for disk hard extension for VM?

    My client asked D drive expansion for 100 GB for VM is 2008. I have a few questions to this

    1. can I use HDD for the extension (using the same disk and even Datastore1)?

    Ex: D drive has now 400GB, if I add an another 100 GB on the same hard drive, it will go 500 GB

    2. can I use different hard drive for expansion (creating new HARD drive and using the same datastore1)

    Ex: Now a 400 GB drive, created the new HARD drive and added 100 GB using the same Datastore1


    3. can I use different and expanded to different hard drive Datastore2 hard drive?

    Ex: Car has now 400 GB, new HDD created with 100 GB of different datastore2

    (1) Yes, you can use the same disc - and it will be expanded to 500 GB - you must use the disk manager in 2008 to extend the drive to make space available

    2 & 3) Yes, you can create a new HARD drive and add to the machine and it can be on another store of VMFS data

  • Best practices for the CFC and CFFUNCTION

    I'm curious as to what must be the best place for cffunctions.  Be in the Application.cfc file if they need to be called from various locations throughout a site.

    Or is it a better idea to put the cffunctions on one page and use cfinclude to put the page in pages where functions can be used?

    Or is there a better way to deal with them?

    I have a bunch of small savers of time code

    You can still use an inclusion, of course. You should think about CFC when certain functions share themes in common, when functions become complex, or when the number of functions becomes important.

    Maybe they should all go in a CFC as UDF. CFC.

    Yes, they could go on a CFC, but not necessarily all in a CFC. You should consolidate functions only if they express the behavior of a particular concept. For example, you could do functions that manipulate strings, such as getRandomString, part of StringManipulation.cfc.

    I think I can call one of the several functions in a CFC.

    You can call any number of functions a CFM page or function in an another CFC.

    I did not understand if a CFC is a process, or can be a collection of processes.

    A function through an instance of a component call, as follows, is a process


    However, there are ways to create two or more of these processes to run at the same time.

    I'm not too clear on where put it while it is accessible by all pages of a site.

    As Dan said, you can put inside or outside of the wwwroot folder. The important thing is to distribute your FSTC and CFC in folders so that your application will have a logical, easy to follow structure.

  • What are the best practices for using Lightroom in two places

    Here's the problem:

    Photographer working weekends and holidays at another place with a second laptop (no).  Photographer wants to keep two separate but identical to the entire photo library copies and the catalog LR for safety.  At the end of a period of work at location 1, photographer wants to take all the changed files (some new, some just changed) and update the LR and the library catalogue to slot 2.  This could be done via the internet, but it is perhaps more practical transport changes on a disk "intermediary".

    Does anyone have an opinion on the best ways to do this?  Is there a way to just export (as the catalog) files and the changed files and then import it into a different location?  Manually copying files of photos that we think have been modified to run the risk of missing some.  Just copy everything means carrying close to 2 TB and this will also take a long time before each trip.  Not so convenient every weekend.

    Tips and ideas appreciated.

    Create a smart collection - published in X days. Select it and export as catalog, including negatives. Keep to the other computer and import the catalog to enter the catalog of another computer (or just open it directly). Reverse the process to bring things back.

    John

  • What are the best practices for editing files .mov (output of a Ninja Blade ProRes recorder) on a PC?

    We recorded using our recorder ProRes Ninja, resulting in files large .mov (as expected). While my Mac laptop is capable of playing Quicktime files, my PC cannot of course. In the meantime, I create compressed copies of the files (such as .mp4s) for sequencing on my PC, because it is a much more powerful machine. However, I don't know how to use these compressed files affect the quality of the final product.

    I have to encode files .mov to a different output for use on my PC, is there a way for my PC read uncompressed files or .mp4 files will be sufficient for the high quality of desired output?

    Thanks for your help!

    For a PC, you have to download the Quicktime Player... Although I found that the best work, go ahead and get the Qt Pro license (bit less $ 30) seem to make the best out of my 1080 p mov files. Many users pc found here as soon as they have downloaded Qt, their mov worked well. So... you should be able to use fine as-is in mov codec after d - l QuickTime or at worst, Qt Pro license number is entered in your Qt player.

    Neil

  • What is the best practice for the parameters of the dns client on a windows 2008 r2 domain controller

    have a secondary site and installed a windows 2008r2 domain controller. Primary DNS either 127.0.0.1 and secondary be point to the PDC FSMO role holder is also a DNS server?

    Hello

    Your question of Windows 7 is more complex than what is generally answered in the Microsoft Answers forums. It is better suited for the IT Pro TechNet public. Please post your question in the Technet Forum. You can follow the link to your question:
    http://social.technet.Microsoft.com/forums/en/category/w7itpro

Maybe you are looking for