• Post Reply Bookmark Topic Watch Topic
  • New Topic

which is best coding practise  RSS feed

 
Kaleeswaran Karuppusamy
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have some doubt

1.which is the best way why?

if( variable == null )

or

if( null == variable )

2. for( int i=0 ; i<10 ; i++ )

or
for(int i=0 ; i<10 ; =++i) //which one is best
somebody said second one is best..because first statement create temporary variable when we increment i++ is it true?


3 .somebody is said singleton pattern object never ever gorbage collect this object is it true?

4. garbage collector wont collect static reference is it true?


please suggest me


 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To 1: I always use the first version, same applies for the second example.

As for the singleton, it a java object like any other, hence it will get Garbage Collected if it is not referenced, same applies for static variables.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say the same as Sebastian, except for #1.
There I prefer to use if( null == obj ), just to be safe in case I made a mistake typing "=" for "==". It would lead to a ompilation error in this case.

Rest all is fine.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. I prefer variable == null. Himanshu Kansal gives a valid reason to use null == variable, but I find that in practice I never use = instead of == by accident. And writing null == variable gives me the feeling that I'm talking like Yoda...

2. I've heard that argument (about the unnecessary temporary variable) before when I was programming in C++. What happens is that the expression i++ (post-increment) returns the old value of i. When i is incremented, the old value of i must be retained in an invisible temporary variable to be returned as the result of the expression. However, since the result of the expression is never used, there's a good chance that the compiler will optimize it away. If you really want to know, disassemble the class with javap and look at the bytecode assembly language to see if it stores the old value of i or not.

3. If you have a singleton like this:

Then the private static member variable 'instance' will contain a reference to the singleton after you've called getInstance(), and that instance won't ever be garbage collected.

4. I don't know what you mean by this. Objects that are not referenced anywhere are garbage collected. That doesn't have anything to do with static member variables.
 
Anuj Prashar
Ranch Hand
Posts: 99
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu Kansal
There I prefer to use if( null == obj ), just to be safe in case I made a mistake typing "=" for "==". It would lead to a compilation error in this case.



IMHO using = instead of == in if statement lead to compilation error in all cases because boolean expression is required inside paranthesis of if loop.
Please correct me if i am wrong.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anuj Prashar wrote:IMHO using = instead of == in if statement lead to compilation error in all cases because boolean expression is required inside paranthesis of if loop.
Please correct me if i am wrong.

Almost right.

Mistaking a == for a = is only possible if both variable types are boolean or (since Java 5.0) Boolean. An example:
Of course, this problem is a lot worse in C/C++ where anything can be evaluated as if it were a boolean.
 
Anuj Prashar
Ranch Hand
Posts: 99
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clearing my doubt
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!