GOTO STATEMENTS in Java

I've kind of nested if blocks , but not sure if NamedBlocks might be used to accomplish the following.

if(obj != null)

{

if(obj.equals("1"))

{

goto L1;

}

else //2

{

System.out.println("...");

}

}

else

{

L1:

for(intj=0;j<vector.size();j++)

{

}

}

Basically I don't want the L1 block to be coded twice. One other option could be make it as a function.

Let me know if there is a better way to implement the above

Thanks,

VJ>

[573 byte] By [VPJa] at [2007-11-27 1:40:53]
# 1

Obviously, you know goto is not allowed in Java, right?

You have to rewrite your code to not use goto, like this:

if (obj!=null && !obj.equals("1"))

{

System.out.println("...");

}

else

{

for(intj=0;j<vector.size();j++) {...}

}

Or, even more simply:

if (!"1".equals(obj)) // Since "1".equals(null) is required to return false

{

System.out.println("...");

}

else

{

for(intj=0;j<vector.size();j++) {...}

}

>

KathyMcDonnella at 2007-7-12 0:55:38 > top of Java-index,Java Essentials,Java Programming...
# 2

if ((obj == null) || obj.equals("1"))

{

// do the code in the L1 block here

}

else

{

// do the code for the //2 block here

}

warnerjaa at 2007-7-12 0:55:39 > top of Java-index,Java Essentials,Java Programming...
# 3
TOO SLOW
DrLaszloJamfa at 2007-7-12 0:55:39 > top of Java-index,Java Essentials,Java Programming...
# 4

Kathy wrote:

>

> Or, even more simply

> if (!"1".equals(obj))

> {

> System.out.println("...");

> }

> else

> {

> for(intj=0;j<vector.size();j++) {...}

> }

Sorry, that second code in my reply was wrong. Please disregard it.

(I was trying to be too smart for my own good)

KathyMcDonnella at 2007-7-12 0:55:39 > top of Java-index,Java Essentials,Java Programming...
# 5
This would throw a runtime exception if obj is null then the 'OR' condition will throw the exception.I don't think you can combine both conditions in if statement.
VPJa at 2007-7-12 0:55:39 > top of Java-index,Java Essentials,Java Programming...
# 6

> This would throw a runtime exception if obj is null

> then the 'OR' condition will throw the exception.

I assume you are talking about this code I wrote:

> if (!"1".equals(obj))

> {

> System.out.println("...");

> }

> else

> {

> for(intj=0;j<vector.size();j++) {...}

> }

This code is incorrect, but it will NOT throw a null pointer exception.

That's because equals(null) is required to return false WITHOUT throwing a null pointer exception.

KathyMcDonnella at 2007-7-12 0:55:39 > top of Java-index,Java Essentials,Java Programming...
# 7

> This would throw a runtime exception if obj is null

> then the 'OR' condition will throw the exception.

>

> I don't think you can combine both conditions in if

> statement.

(and no matter what code you're referring to, I don't see where you'll get a runtime exception...)

tsitha at 2007-7-12 0:55:39 > top of Java-index,Java Essentials,Java Programming...
# 8

> > This would throw a runtime exception if obj is

> null

> > then the 'OR' condition will throw the exception.

>

> Which code are you referring to?

> Please quote part of the message, so that we know

> which code you are talking about.

He replied to my reply.

Nope, you are incorrect. You should read up on the short-circuit boolean operators. If obj is null, it won't evaluate the 2nd part of the statement.

And Kathy, I think the second version of your code is fine, unless there's something in my eye.

warnerjaa at 2007-7-12 0:55:39 > top of Java-index,Java Essentials,Java Programming...
# 9
> And Kathy, I think the second version of your code is> fine, unless there's something in my eye.Ah, you're right! :)This is getting too late in the day for me. I'll leave now. :)
KathyMcDonnella at 2007-7-12 0:55:39 > top of Java-index,Java Essentials,Java Programming...
# 10

sorry about the miscommunication,

I was referring the following code,

if ((obj == null) || obj.equals("1"))

{

as well as

if (obj!=null && !obj.equals("1"))

{

System.out.println("...");

}

else

which might throw NullPointer exception, I tried the same on 1.4JRE, its working,but I vaguely remember having got a runtime exception on, may be 1.3_06 JRE.

I still have that piece of code in my project,(platform uses 1.3 JRE)

if(ddEnrollDetails.getCheckingDDANo() != null)

{

if(!(ddEnrollDetails.getCheckingDDANo().trim().equals("") || ddEnrollDetails.getCheckingDDANo().trim().equals("0")))

instead of single if block, I had to implement in nested if.

any comments pls, especially does JRE version makes the difference.

Thanks,

VPJ

VPJa at 2007-7-12 0:55:39 > top of Java-index,Java Essentials,Java Programming...
# 11

> sorry about the miscommunication,

>

> I was referring the following code,

> > if ((obj == null) || obj.equals("1"))

> as well as

> > if (obj!=null && !obj.equals("1"))

> which might throw NullPointer exception

No it won't. The "short circuit" behavior was in C and C++. And was in Java from day 1.

So, no, the Java version doesn't matter.

Your old code must have caused the exception for a different reason

KathyMcDonnella at 2007-7-12 0:55:39 > top of Java-index,Java Essentials,Java Programming...
# 12
Thanks Kathy for the clarification. I appreciate it.
VPJa at 2007-7-12 0:55:39 > top of Java-index,Java Essentials,Java Programming...