Are primitive types necessary for Game Programming?

Started by
16 comments, last by TheChubu 11 years, 1 month ago

public static void main(String[] args)
{
int appleCount = 10;

System.out.println("You have " + appleCount + " apples");
}

Why would this syntax need to change? You could also have String number = 10.toString();. I'm sure that you realize there is already not a one to one mapping from syntax to code. The for ( String s : strings) is clearly one example of this. So the above example could be swizzled to "Integer.toString(10);" by the compiler.So I don't see why you'd have to change the syntax to support treating primitives as objects in the language but have them as primitives in the run time. Smalltalk does this.. they are called immediate directs.

Advertisement

I don't think any syntax would need to change ... I think this whole thing is more about adding functionality rather than taking functionality out of Java. In other words, make a list of everything you can do with an Integer that you can't do with an int. Now extend the implementation of the VM or Java libraries or whatever such that you can do all those things with an int. I'd be surprised if this is not what is under discussion.


I think they must mean, and not sure because it's been a while since I touched Java, but getting rid of the versions of primitive types that don't behave like objects, Like in Java, isn't there int and Integer? and of those only Integer would let you do introspection and could be inserted into the old-style Java containers in which the data values had to inherit from Object and so forth. With Integer in theory you could do some like 2.ToString()...

IIRC, this is basically part of it.
the idea would basically putting a layer of gloss over int vs Integer, making it look more like 'int' was simply an instance of Integer, ...

practically though, you can't really (actually) get rid of primitive types and have any semblance of performance.

I could be wrong though, given I am not really a Java developer...


You can remove primitive types from a high level language without losing performance as long as the compiler is good enough to cut away the overhead when its not needed.
This is an advantage all high level languages have, they are usually free to do almost whatever they want with your code as long as the result doesn't change. (Actually writing a compiler that can perform great optmizations is the hard part (and we're still not at the point where compilers always do a better job than a skilled programmer))
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

I'm in the "Java lunacy" bracket here too.

Now, for certain types of code this may be an eminently sensible suggestion. For people learning about OO, having everything be an object means that everything behaves consistently and there are no weird edge cases. That's all good stuff.

However it's a clear case of taking something that's good in certain special cases and trying to apply it as good in every general case. And that's nuts. With specific reference to game programming, for example, this may be just fine for game logic or scene tree walking, but show me how you'd fill in the pointer returned by a glMapBufferRange call without primitive types and without having to do some awful contortions.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.



I think they must mean, and not sure because it's been a while since I touched Java, but getting rid of the versions of primitive types that don't behave like objects, Like in Java, isn't there int and Integer? and of those only Integer would let you do introspection and could be inserted into the old-style Java containers in which the data values had to inherit from Object and so forth. With Integer in theory you could do some like 2.ToString()...

IIRC, this is basically part of it.
the idea would basically putting a layer of gloss over int vs Integer, making it look more like 'int' was simply an instance of Integer, ...

practically though, you can't really (actually) get rid of primitive types and have any semblance of performance.

I could be wrong though, given I am not really a Java developer...


You can remove primitive types from a high level language without losing performance as long as the compiler is good enough to cut away the overhead when its not needed.
This is an advantage all high level languages have, they are usually free to do almost whatever they want with your code as long as the result doesn't change. (Actually writing a compiler that can perform great optmizations is the hard part (and we're still not at the point where compilers always do a better job than a skilled programmer))


that is why I wrote "(actually)", as-in, even if at the high-level it all looks like objects, at the CPU level if one is doing much beyond normal integer arithmetic, performance will suffer pretty hard.

so, for most high-level features which exist in a language, there needs to be a way to largely optimize them away in the implementation.

it is sort of like with dynamically-typed languages. the high-level language may look dynamically-typed, but for a reasonably high-performance implementation, the VM will have to aggressively try to eliminate these dynamic types (via type-inference or similar), ...

likewise goes for the JVM faking the existence of instances of the Integer class...

Looks to me like they want to steal some idea from Ruby this time, but only maybe possibly and only 3 versions from now and only if thats really what they want to do. Other than that the linked article doesnt give any real information besides "we continue to work on Java".

Edit: Oh I forgot, they admit Java is the new COBOL. tongue.png

I was stumbling around the net and discovered that suggestions have been made for primitive types to be removed from future versions of the Java programming language.

This is insanity in the Java community.

.Net (C#) has a best-of-both-worlds solution called "boxing".

If they made a change to Java they should implement a similar feature.

Removing primatives would wreck performance. You would have to allocate from the free-store for every field.

It would no longer be possible to create an actual array. You'd always get an array of handles.

Java already provides this feature it was introduced a while back it's called auto-boxing and unboxing which automatically wraps and unwraps int to Integer and vice versa whenever a primitive is passed when an Object is expected or when an Object is passed when a primitive is expected.

public static void main(String[] args)
{
int appleCount = 10;

System.out.println("You have " + appleCount + " apples");
}

Why would this syntax need to change? You could also have String number = 10.toString();. I'm sure that you realize there is already not a one to one mapping from syntax to code. The for ( String s : strings) is clearly one example of this. So the above example could be swizzled to "Integer.toString(10);" by the compiler.So I don't see why you'd have to change the syntax to support treating primitives as objects in the language but have them as primitives in the run time. Smalltalk does this.. they are called immediate directs.

As said in reply to Shannon, Java already provides this feature, removing primitive types makes this feature redundant so it would likely be removed along with the primitives.

I can't justify removing primitives from Java so I am not going to defend the idea, I posted this thread because I was looking for an explanation as to why this would be suggested and what ramifications there would be as a result. I was curious to know if it seemed counter intuitive to anyone else so I asked if primitives were needed by programmers. i personally use primitives where they make sense, i.e. where I don't need an Object but the suggestion that appears to be made is that people want Java to become a language where everything really is an Object.

While boxing/unboxing and all sort of interoperations between int and Integer, or between Java types and native code types (IntBuffer, FloatBuffer, etc) are covered, they have their quirks here and there (specially dealing with collections, sometimes you have to be aware of when the boxing/unboxing is happening), and they still work pretty fine nevertheless.

Thing is, like String, Integer, Float, etc are immutable objects. So one of the worst offenders (performance wise) you could make, which is appending massive quantity of String objects (instead of mutable StringBuilders), could start happening with Integers. Every time you operate on an integer (addition, subtraction, everything) you'd be creating massive amount of new objects.

Probably JVM devs have some way to deal with that stuff with good performance anyway, the thing is that like String, it will probably end in the list of "Things to watch out for" that a programmer has to have in mind.

"Why my array averaging method is so slow!?"

"Well, you're using an Integer instead of IntegerBuilder. Use an IntegerBuilder and refactor those 30 methods that indirectly depend on Integer's immutability so it doesn't crash everything"

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

This topic is closed to new replies.

Advertisement