• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

IF statement

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi All,

Just curious to know the difference in the below if statements.

Vector vTest = new Vector();

if ( vTest != null )

and

if ( null != vTest)


Thanks,

Ananth Ram
 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think there is no any difference between them.

avi sinha
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no real functional difference. but imagine you were doing an equals test (==), and mistyped it as '='

vTest = null

vs

null = vTest

The compiler would flag this in either case (since neither evaluate to a boolean True or False). However, other languages like C++ would happily let you assign null to vTest and continue on, causing all kinds of strange things to happen in your code.
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:There's no real functional difference. but imagine you were doing an equals test (==), and mistyped it as '='

vTest = null

vs

null = vTest

The compiler would flag this in either case (since neither evaluate to a boolean True or False). However, other languages like C++ would happily let you assign null to vTest and continue on, causing all kinds of strange things to happen in your code.



i agree sir.
in case of assignment it can lead to compilation error.

avi sinha
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Implementation wise there is some difference. Suppose you have created Vecor object and doing some operations on it and at some point of time you want to check wether it has become null or not.



If you are using first case: if ( vTest != null ) and suppose that vTest is null then on run time the application will throw NullPointerException.

If you are using second case: if ( null != vTest) and suppose that vTest is null then on run time the application will not throw NullPointerException.

Second case is good coding practice.

Thanks,
Vikash Anand.
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vikash Ananda wrote:

If you are using first case: if ( vTest != null ) and suppose that vTest is null then on run time the application will throw NullPointerException.


It will throw NullPointerException only when there is some operation on vTest. In that if statement i don't think it will throw such exception.
 
reply
    Bookmark Topic Watch Topic
  • New Topic