For Programmers: Free Programming Magazines  


Home > Archive > Cobol > March 2006 > Re: Java compatibility issues (WAS: MF having issues?)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Java compatibility issues (WAS: MF having issues?)
Frank Swarbrick

2006-03-08, 6:55 pm

Michael Wojcik<mwojcik@newsguy.com> 03/08/06 12:21 PM >>>
>
>In article <476delFe2lamU2@individual.net>, "Frank Swarbrick"

<Frank.Swarbrick@efirstbank.com> writes:
method[color=darkred]
from[color=darkred]
>
>Same reason why you need them around a literal program name in a call
>statement. The method name can be a data-item as well as a literal:
>
> move "myMethod" to method-name
> invoke myObject method-name ...
>
>This is actually quite different from the C-derived languages, which
>support *neither* of those name-resolution methods directly. In C,
>for example, a called function can only be specified using a variable
>of a function-pointer type, or the name of a function - which is
>actually the name of a function-type variable, and decays to a
>pointer-to-function-type variable in most contexts. (Function-type
>variables in C are immutable, so "variable" is a bit of a misnomer,
>but from the language's point of view that's what they are.)
>
>C-derived languages thus require other mechanisms to call a function
>using a name determined at run-time, and in fact there's no standard
>way to do that in C.
>
>Thus in Java you write:
>
> myObject.myMethod(...);
>
>because you can't write:
>
> String myMethodName = "myMethod";
> myObject.myMethodName(...);
>
>But in COBOL, in effect, you can do the latter, so it has to be
>distinguished from the equivalent of the former.


Makes sense! Thanks for the clarification.

I believe you can do something similar in Java, though. I just now tried
this.

instead of:
try {
worksock.connect(s_addr);
}
catch (Exception e) {
System.out.println(e);
return;
}

You can do:
try {
Class[] methodParms = new Class[1];
methodParms[0] = SocketAddress.class;
Class socketClass = worksock.getClass();
Method myMethod = socketClass.getMethod("connect", methodParms);
Object[] parmObjs = new Object[1];
parmObjs[0] = s_addr;
myMethod.invoke(worksock, parmObjs);
}
catch (Exception e) {
System.out.println(e);
return;
}

Not that I'd recommend it! You may be able to simplfy it somewhat, but I'm
not Java expert, so I'm not sure how...

Frank


---
Frank Swarbrick
Senior Developer/Analyst - Mainframe Applications
FirstBank Data Corporation - Lakewood, CO USA
Michael Wojcik

2006-03-09, 6:55 pm


In article <4793b3FekqcoU1@individual.net>, "Frank Swarbrick" <Frank.Swarbrick@efirstbank.com> writes:
> Michael Wojcik<mwojcik@newsguy.com> 03/08/06 12:21 PM >>>
>
> Makes sense! Thanks for the clarification.
>
> I believe you can do something similar in Java, though. I just now tried
> this.
>
> ...
> Method myMethod = socketClass.getMethod("connect", methodParms);
> Object[] parmObjs = new Object[1];
> parmObjs[0] = s_addr;
> myMethod.invoke(worksock, parmObjs);


Right, but there's the extra indirection of creating a Class object
and a Method object and so forth. (This is what there's no standard
for in C. There *is* a standard way to do it in Java, as you've just
described.)

COBOL, on the other hand, has syntactic sugar to do the "look this
name up and resolve it to the code I want to call" bit, right in the
CALL or INVOKE verb.

Languages with first-class functions, like Scheme and OCaml, have yet
another variation on this: you can have function names and function
variables and functions you compute at runtime, and you can pass
functions as parameters and return new functions. They're even more
flexible when it comes to invoking functions and methods because the
thing they're invoking may not have existed until right before the
invocation.

In summary:

C: You can only call functions known at compile time, directly or
through pointers, in the standard language.

Java: You can call methods using a name known at runtime, but you
have to go through extra hoops to do so.

COBOL: You can call programs and methods directly or by name, but
they have to have been previously compiled.

Functional languages: You can call functions you just made up a
moment ago in the running program.

--
Michael Wojcik michael.wojcik@microfocus.com

Duck: No secret what's worth a hoot ought to be kept quiet.
Pogo: Secrets is usually perty doggone fascinatin'.
Duck: Egg-zackly ... it's completely illogical to keep a secret secret.
Pogo: An' unfair. -- Walt Kelly
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com