Actually,
"test".equals(t) WILL throw a NullPointerException when t is not set (try it
and see, I did). Any reference to a variable not initialized will result in
a NullPointerException.
>> The version with "test".equals() is in my opinion the better way, because
if prevents a possible NullPointerException when t is not set, like in
t.equals ("test").
Even if this were true (it is not, see above), you WANT an exception to be
thrown. Why? Believe it or not, exceptions are good. You WANT them to be
thrown so that you can catch problems with your code - in this case
forgetting to set t to some value. When the exception is thrown, you can
then handle it by correcting the coding error (if that is what it was), or
in the exception block.
Regards,
Dean Rush
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Ewald Ertl
Sent: Friday, July 10, 2009 3:18 AM
To: Free Java Programming Online Training Course By Sang Shin
Subject: [java programming] Re: String usage pros and cons
Hi,
On Jul 9, 10:41 pm, Fatih Ergin <[email protected]> wrote:
> String t = new String (“test”);
>
> If (“test”.equals(t))
> System.out.println (“true”);
> System.out.println (“false”);
>
> Is this a True statement ? What are the Pros and Cons using Strings
In java terms you need an else statement to separate the condition.
the if-keyword is in lower letters.
@Pros and Cons:
Depends on what you like to do.
The version with "test".equals() is in my opinion the better way, because if
prevents a possible NullPointerException when t is not set, like in t.equals
("test").
Ewald
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---