Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

General Questions about java

 
Selami Suyabakan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to java, trying to learn it and have some very basic questions to clear in my mind.

1. Why does Java provide both ints and floats, if floats can encode any possible int?
2. Why does Java provide four types for storing integer data (byte, short, int, and long)?
i.e., why would we use one over another? I know that they are different in bit size, but in evaluation are they converted to int?
3. Why do we expect Java programs to run slower than equivalent C or C++ programs?
4. What advantages do ArrayLists have over arrays? I know that ArrayLists are dynamic unlike arrays in java. Any other advantages?
5. If ArrayLists have advantages, is there any points to use classical arrays ? Are they still useful, if so why?
 
Jan Cumps
Bartender
Posts: 2588
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I 'll pick this one:
3. Why do we expect Java programs to run slower than equivalent C or C++ programs?
We do not expect that.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) A float can hold values much larger than the largest int, but it uses the same amount of storage. It does this by sacrificing precision. An int can represent "2 billion and three" exactly, while a float can represent that value only approximately. This makes floats unsuitable for calculations involving money, for example.

2) Smaller types use less memory! This can make a big difference if you've got a large number of objects with several "byte" members as opposed to "int" members, for example.

3) We don't; they don't. Hmmm. I'm starting to suspect these are homework questions.

4, 5) If you don't have arrays in the language, how would you implement ArrayList ? And of course, often you need a container for exactly three objects, no more, no less.
 
Selami Suyabakan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answers.
Yes these are old exam questions which I could not answer and we are allowed to make research, google, post forums or ask java pros to learn.
I also want to understand the answers, because I want to learn java.
I heard that java programms are slower, is it not true?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49388
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java programs might have run slower 10 years ago, but that is no longer true.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15358
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Programs written in programming languages like C and C++ are compiled to native machine code for the computer that you are compiling them on. This makes them run fast, but also makes them specific for that type of computer.

One of the strengths of Java is that it is "write once, run anywhere". You can compile a Java program on a Windows machine and the resulting class file runs on any computer, no matter the operating system, for which there is a Java runtime environment available. The Java compiler compiles your Java source code to bytecode, which consists of machine code instructions for a virtual machine. The JVM (Java virtual machine) interprets and executes the bytecode when you run your Java program.

In principle, this is slower than how it works with C or C++, because extra work needs to be done when you run your program - the bytecode has to be interpreted while your program runs. In the early days of Java, Java programs indeed ran much slower than native code.

But over the years, Sun's implementation of Java has become very sophisticated. The current JVM contains a just-in-time compiler which translates the bytecode to native machine code on the fly, so that your program runs almost as fast as a native C or C++ program.

In theory, a JIT-compiled program could run even faster than an ahead-of-time compiled program, because the JIT-compiler can employ optimizations based on information of the runtime behaviour of your program. A traditional ahead-of-time compiler can't take advantage of such information. (Although some sophisticated compilers, such as Intel's C / C++ compiler, contains a feature where you can provide the compiler with runtime profiling information about your program, which the compiler uses to optimize the code when you compile it the second time.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic