For Programmers: Free Programming Magazines  


Home > Archive > Java Help > July 2006 > Int or short?









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 Int or short?
zecreven@gmail.com

2006-07-25, 7:02 pm

Hello,
I am coming from C++ world to java world.. in C++, int type is the
fastest type that computer handles. I mean if you prefer working with
int other than short or long, it will show better performance. Is the
same thing true for java?

AndrewMcDonagh

2006-07-25, 7:02 pm

zecreven@gmail.com wrote:
> Hello,
> I am coming from C++ world to java world.. in C++, int type is the
> fastest type that computer handles. I mean if you prefer working with
> int other than short or long, it will show better performance. Is the
> same thing true for java?
>


nope - they are typically both the same size inside the JVM - its only
their allowed range of values that differs.

Thomas Fritsch

2006-07-26, 4:03 am

zecreven@gmail.com wrote:
> I am coming from C++ world to java world.. in C++, int type is the
> fastest type that computer handles. I mean if you prefer working with
> int other than short or long, it will show better performance. Is the
> same thing true for java?

No, in Java int is always defined to be 4 bytes, independently of which
type (2, 4, or 8 bytes) is handled fastest by the computer.

--
Thomas
Paul Cager

2006-07-26, 7:03 pm

zecreven@gmail.com wrote:
> Hello,
> I am coming from C++ world to java world.. in C++, int type is the
> fastest type that computer handles. I mean if you prefer working with
> int other than short or long, it will show better performance. Is the
> same thing true for java?
>


The Java compiler will typically generate less efficient bytecode when
working with shorts (as it must emit extra bytecodes to "convert" shorts
back into ints). However in just about any situation where performance
is important, this will not matter - the bytecode will have been JITed
into native code.

Just a couple of points, as you said you are coming from C++:

* The sizes of ints, shorts etc. in Java are platform independent,
unlike C. So a Java int might not be the same as a native integer.

* There is generally very little point considering these
micro-efficiencies in Java (I'll admit I'm not much of a fan of
micro-efficiencies in *any* language). You'll probably find a wealth of
performance advice in this newsgroup and on the net; it mostly boils
down to "Write clear, maintainable code; measure the performance; if
improvement is required use a profiler to find hot spots; improve design
and/or hot-spots; measure again".

Paul
Sponsored Links







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

Copyright 2008 codecomments.com