# Just started learning Java...

## Recommended Posts

Kambiz    758
I have just started learning Java but there are at least 3 thing I'm missing from c/c++/c#: 1)unsigned types 2)function pointers/delegates 3)something like .Net Framework's CodeDomProvider If there are no Java equivalents for 2 & 3 I would give it up! So, dear Java fans: can you motivate me to learn Java? [Edited by - Kambiz on May 17, 2006 12:15:46 PM]

##### Share on other sites
Guest Anonymous Poster
Hahaha, what a lame message! Did you try to buy a book? Like "Head first Java" or "Core Java". First study a little, later start bitching and whining.

##### Share on other sites
Kambiz    758
There are some Java books available I can use and I have read some chapters from them, I thought about using Java for my next Project to make it cross platform but if the language is so much weaker than c#(at least from my point of view... (I really need dynamic code...)) I will go with Microsoft. Why should I spend so much time learning a new language when it is useless for me?

##### Share on other sites
Fred304    382
Quote:
 Original post by Kambiz 1)unsigned types

There aren't any. Why exactly do you need them?

Quote:
 2)function pointers/delegates

Simply declare an interface and have different classes implement it.
public interface FunctionPointer{	public int function(String a, int b, Object x);}public class Foo implements FunctionPointer{	public int function(String a, int b, Object x)	{		return a.length() + 5*b + x.hashCode();	}}public class Bar implements FunctionPointer{	public int function(String a, int b, Object x)	{		return a.indexOf('n') * b - x.toString().length() & 0x1234;	}}

Quote:
 3)something like .Net Framework's CodeDomProvider

Since I have no idea what this is I cannot comment on that.

##### Share on other sites
Kambiz    758
Thanks...
I think the reflection library could help me with number 3...

##### Share on other sites
Guest Anonymous Poster
Quote:
 Original post by KambizThere are some Java books available I can use and I have read some chapters from them, I thought about using Java for my next Project to make it cross platform but if the language is so much weaker than c#(at least from my point of view... (I really need dynamic code...)) I will go with Microsoft. Why should I spend so much time learning a new language when it is useless for me?

I don't think you understand what you are talking about at all. Each language has its own way of doing stuff, trying to program C++ in Java only leads to poor code and headaches.

All your so much "needed features" are done in one way or the other:

- Why in the h*** are you going to need unsigned types? Unless you are programming for embedded devices, and even so this is not a problem all the times since the data is there anyways, you would care about it?

Java developers don't use, don't need and don't want unsigned types. The only people I saw complaining about it are C++ developers trying to find a pretext for not to use it.

- What you do with "function pointers / delegates" is done with anonymous classes / listeners in Java in a much clearer way.

- I have no idea what CodeDomProvider does but have everything you could ever need for XML processing.

Java is Java, so either you make up your mind about learning it or not. The choice is yours, we don't need to prove anything to you.

Quote:
 language is so much weaker than c#

Millions of people disagree with you, all over IT industry where Java is #1. In the opensource too, take a look at sourceforge.

##### Share on other sites
Zahlman    1682
OP: Your post (a) sure looks like a troll to me, and (b) probably belongs in General Programming anyway. But I'll bite:

Quote:
Original post by Fred304
Quote:
 Original post by Kambiz 1)unsigned types

There aren't any. Why exactly do you need them?

Actually, there is one: 'char' is effectively 'unsigned short'.

But yeah, why do you think you need them? I can think of two reasons:

- "unsigned correctness", similar to "const correctness", which also isn't provided in Java. Well, you can choose a language according to how much type-checking you want it to do, but there are lots of people who prefer that the language check basically nothing at all at compile-time. So, to each their own.

- Optimizations where you need a value to range 0 to 255 and you want it to fit in a byte. Well, this can certainly be dealt with when you do need to: As long as you can keep track of which bytes you want to be "unsigned", just convert the signed value whenever you use it in an expression. Java promotes numeric subexpressions at least to 'int' anyway, so this is the trick:

byte x, y;// laterx = (byte)200; // it stores -56, but we can recover an unsigned valuey = (byte)150; // similarlyint z = (x & 0xff) * (y & 0xff); // 30000

Each bitwise-and causes the x and y to be promoted to int, and sign-extended. The 0xff literals already are ints, so nothing changes there. We use the bitwise-and to mask off the sign bits, and then multiply the new unsigned values.

Quote:

Quote:
 2)function pointers/delegates

Simply declare an interface and have different classes implement it.

Right. The main reason function pointers exist in C++ in the first place, and that C# has delegates to refine the concept, is C backwards-compatibility. You know, that non-OO language where you had to create those abstractions yourself.

##### Share on other sites
Guest Anonymous Poster
Quote:
 Original post by KambizIf there are no Java equivalents for 2 & 3 I would give it up! So, dear Java fans: can you motivate me to learn Java?

news flash: no one (with any luck) cares what language you use. if you have a question try asking it.

##### Share on other sites
Quote:
 Original post by Kambiz3)something like .Net Framework's CodeDomProvider

If all you want to do is compile Java to native binaries, then you might want to look into the GNU Java Compiler. If you actually want to compile code at run-time, try poking around for information on JIT (just-in-time) compiling.

##### Share on other sites
Guest Anonymous Poster
Quote:
 Original post by programwizardIf you actually want to compile code at run-time, try poking around for information on JIT (just-in-time) compiling.

What!?

Java already does that out of the box, no one needs to worry about "getting information on JIT".

##### Share on other sites
jfclavette    1058
Java doesn't have anything standardized akin to CodeDom and I'm somewhat unsure as to wether it is technically possible to implement something similar in a JVM. You could certainly load another class in another thread, but reloading code in the same thread is going to be harder, I believe.

##### Share on other sites
Guest Anonymous Poster
Quote:
 Original post by jfclavetteJava doesn't have anything standardized akin to CodeDom ...

You know, it would be really helpful if you all said what you plan to do instead of using some "MS patented terminology (tm)". WTF is CodeDom!? Will I need a dictionary to translate what you are saying?

Sorry, Java developers can't help you if they can't understand what you want.

##### Share on other sites
jfclavette    1058
Quote:
Original post by Anonymous Poster
Quote:
 Original post by jfclavetteJava doesn't have anything standardized akin to CodeDom ...

You know, it would be really helpful if you all said what you plan to do instead of using some "MS patented terminology (tm)". WTF is CodeDom!? Will I need a dictionary to translate what you are saying?

Sorry, Java developers can't help you if they can't understand what you want.

Compiling a .java file at runtime, loading the class and instantiating it. This is only a microscopic subset of what CodeDom can do, but I assume it's what the OP wants to do. It's also a significant enough building block to start building CodeDom-like functionnality in Java.

I tought 'Java developers' (somehow tying a language to the word programmers seems so pathetic to me) could use Google.

[Edited by - jfclavette on May 17, 2006 4:13:21 PM]

##### Share on other sites
Guest Anonymous Poster
Quote:
 Original post by jfclavetteCompiling a .java file at runtime, loading the class and instantiating it. This is only a microscopic subset of what CodeDom can do, but I assume it's what the OP wants to do. I tought 'Java developers' (somehow tying a language to the word programmers seems so pathetic to me) could use Google.

Google returned nothing but a bunch of gibberish. I thought '.Net developers' were could use complete meaningful sentences, is that difficult?

BTW, it's possible to mess with classloaders in Java, dynamically instantiate classes, use external scripting languages (from the 6 it will be in the standard JDK), etc, but I will leave that for you to look up on Google, ok?

Good search.

##### Share on other sites
jfclavette    1058
Quote:
Original post by Anonymous Poster
Quote:
 Original post by jfclavetteCompiling a .java file at runtime, loading the class and instantiating it. This is only a microscopic subset of what CodeDom can do, but I assume it's what the OP wants to do. I tought 'Java developers' (somehow tying a language to the word programmers seems so pathetic to me) could use Google.

Google returned nothing but a bunch of gibberish. I thought '.Net developers' were could use complete meaningful sentences, is that difficult?

BTW, it's possible to mess with classloaders in Java, dynamically instantiate classes, use external scripting languages (from the 6 it will be in the standard JDK), etc, but I will leave that for you to look up on Google, ok?

Good search.

I know it's possible to load from a JAR. What I'm interested in is compiling them without resorting to external tools and in a way where I can assume anyone with any JVM can do the same. Does Java maintain a reliable accessible path to the compiler (is the compiler included in the JVM ?) that is guaranteed to work in all case on any machine ? I'd be fine with that, even if it wouldn't offer the synctactic sugar .NET offers.

I would also like to load binaries from languages other than Java. I don't want to use an arbitrary interpreted scripting language, but say, C++ or C#. (Altough .NET requires a third-party library to load Java classes, the underlying used functionnality is CodeDOM, so loading C# classes in Java wouldn't be as easy, and as such, no popular projects doing so exist.)

##### Share on other sites
Kambiz    758
I know *exactly* what I want:
My application will have a small kernel, the kernel will load many PlugIns, compile them and use them when needed. In Managed C++ or C# I would use C# like a scripting language and compile all the PlugIns using the CodeDomProvider, now I want to know if it is possible to do the same thing in Java.
Please do not tell me I need to use a scripting language! If that is the only way in Java I will use c# for this Project at the cost of portability.

##### Share on other sites
rip-off    10979
Quote:
 Original post by KambizI know *exactly* what I want:My application will have a small kernel, the kernel will load many PlugIns, compile them and use them when needed. In Managed C++ or C# I would use C# like a scripting language and compile all the PlugIns using the CodeDomProvider, now I want to know if it is possible to do the same thing in Java.

AFAIK the java compiler is only available to those with the sdk, not the java runtime. Java can load and use arbitrary class files during execution (not only those included during compilation ), and can load DLLs ( but you need some special interfacing to talk to the JVM, namely the JNI ).

If I read the MSDN page here correctly:
Quote:
 . Several languages are supported by CodeDomProvider implementations that ship with the .NET Framework SDK.

Does this mean that an implementation of CodeDom only exists if you have a .Net SDK installed? Or idoes the SDK come with the runtime?

However, anyone who is going to be writing plugins for your program will surely have access to a compiler, so why would it be neccesary to have your program compile the .javas as opposed to just using the .class / .jar files?

##### Share on other sites
jfclavette    1058
Quote:
Original post by rip-off
Quote:
 Original post by KambizI know *exactly* what I want:My application will have a small kernel, the kernel will load many PlugIns, compile them and use them when needed. In Managed C++ or C# I would use C# like a scripting language and compile all the PlugIns using the CodeDomProvider, now I want to know if it is possible to do the same thing in Java.

AFAIK the java compiler is only available to those with the sdk, not the java runtime. Java can load and use arbitrary class files during execution (not only those included during compilation ), and can load DLLs ( but you need some special interfacing to talk to the JVM, namely the JNI ).

If I read the MSDN page here correctly:
Quote:
 . Several languages are supported by CodeDomProvider implementations that ship with the .NET Framework SDK.

Does this mean that an implementation of CodeDom only exists if you have a .Net SDK installed? Or idoes the SDK come with the runtime?

However, anyone whoe is going to be writing plugins for your program will surely have access to a compiler, so why would it be neccesary to have your program compile the .javas as opposed to just using the .class / .jar files?

The compilers, not the SDK, come with the Framework redistributable. As to why, say I want the users to be able to script my game trough a level editor. The scripting language I want is a subset of C#. They script the game trough my level editor, save, load the game, and their modifications take effect automatically.

##### Share on other sites
Guest Anonymous Poster
Quote:
 Original post by jfclavetteI know it's possible to load from a JAR. What I'm interested in is compiling them without resorting to external tools and in a way where I can assume anyone with any JVM can do the same. Does Java maintain a reliable accessible path to the compiler (is the compiler included in the JVM ?) that is guaranteed to work in all case on any machine ? I'd be fine with that, even if it wouldn't offer the synctactic sugar .NET offers.

See Java 6 Mustang releases for the Compiler API. Prior to that version you will to look up for libraries on the net.

Quote:
 I would also like to load binaries from languages other than Java. I don't want to use an arbitrary interpreted scripting language, but say, C++ or C#. (Altough .NET requires a third-party library to load Java classes, the underlying used functionnality is CodeDOM, so loading C# classes in Java wouldn't be as easy, and as such, no popular projects doing so exist.)

[/quote]

Anything else? Maybe a cup of coffee? Listen, you don't expect to produce decent production code in C++ for running in the JVM, do you?

So basically you are asking for some idiotic stuff that shouldn't be done nor attempted? If you need to use C++ then use C++, if you need C# then use C#.

Don't even come with J# as an example of Java for .Net, that's based in a very old version of Java that Microsoft had but was sued by Sun for having "incompatibilities".

There's the Java 6 Mustang scripting language support. Prior to that there was a framework for adding such languages to Java at Apache website that's not locked to a specific language.

Have a nice day.

##### Share on other sites
jfclavette    1058
Quote:
 Original post by Anonymous PosterAnything else? Maybe a cup of coffee? Listen, you don't expect to produce decent production code in C++ for running in the JVM, do you?

Yes. It works in .NET.

Quote:
 So basically you are asking for some idiotic stuff that shouldn't be done nor attempted? If you need to use C++ then use C++, if you need C# then use C#.

I've written my whole game server in C++. I want to write the client in Java while reusing the common classes I need. Isn't that valid ?

Quote:
 Don't even come with J# as an example of Java for .Net, that's based in a very old version of Java that Microsoft had but was sued by Sun for having "incompatibilities".

I wasn't talking about J# but about the flurry of Open Source or proprietary hooks to compile and run Java code for .NET.

Quote:
 There's the Java 6 Mustang scripting language support. Prior to that there was a framework for adding such languages to Java at Apache website that's not locked to a specific language.

I wasn't aware of that. Why is it so underused compared to similar .NET functionnality ? I see tons of .NET hooks for languages such as FORTRAN, COBOL and friends, yet nothing for Java.

##### Share on other sites
Guest Anonymous Poster
Quote:
 Original post by KambizI know *exactly* what I want:My application will have a small kernel, the kernel will load many PlugIns, compile them and use them when needed. In Managed C++ or C# I would use C# like a scripting language and compile all the PlugIns using the CodeDomProvider, now I want to know if it is possible to do the same thing in Java.Please do not tell me I need to use a scripting language! If that is the only way in Java I will use c# for this Project at the cost of portability.

Wow! Microkernels!

OSGI has been doing this for the last six years, and some big projects use it. http://www.osgi.org/ , http://download.eclipse.org/eclipse/equinox/

For the scripting stuff use either the framework at Apache or Java 6 that has the scripting engine bundled.

For compiler stuff use the compiler API in Java 6.

Good luck.

##### Share on other sites
Kambiz    758
Quote:
 Does this mean that an implementation of CodeDom only exists if you have a .Net SDK installed? Or idoes the SDK come with the runtime?

You can use the c# and jscript.net compilers directly in your application. They are integrate in the .Net framework and the user does not need to install the SDK. You can even compile the code to an assembly in memory and then use it.
I have used c# and jscript.net as scripting languages in some of my c# programs, having such a compatible scripting language makes everything easy and such compiled scripts run as fast as your code.

##### Share on other sites
Guest Anonymous Poster
Quote:
 Original post by jfclavetteI've written my whole game server in C++. I want to write the client in Java while reusing the common classes I need. Isn't that valid ?

You would need to use JNI for interfacing with C++, which is not good because the VM don't have a clue of what you are doing with the memory.

JNI should be used only for legacy stuff.

Not sure what would be the best way in this case.

Quote:
 Original post by jfclavette

There's the Java 6 Mustang scripting language support. Prior to that there was a framework for adding such languages to Java at Apache website that's not locked to a specific language.
[/quote]

I wasn't aware of that. Why is it so underused compared to similar .NET functionnality ?[/quote]

The Java 6 feature is fairly new. There are tens of languages ported to the JVM but only now it's going to be official part of the JDK to support them.

I think the reason for people not to talk about it too loud is that they don't trust such languages too much and prefer to use Java.