my dog learned polymorphism*
The moose likes Beginning Java and the fly likes General Questions about java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "General Questions about java" Watch "General Questions about java" New topic

General Questions about java

Selami Suyabakan

Joined: Mar 20, 2009
Posts: 2
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

Joined: Dec 20, 2006
Posts: 2491

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.

OCUP UML fundamental and ITIL foundation
youtube channel
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

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.

[Jess in Action][AskingGoodQuestions]
Selami Suyabakan

Joined: Mar 20, 2009
Posts: 2
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

Joined: Oct 13, 2005
Posts: 37953
Java programs might have run slower 10 years ago, but that is no longer true.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

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.)

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: General Questions about java
Similar Threads
ArrayList Implementation
Dought With Collections
Preparing for Java 5 programmer certification
Dynamically create a class at runtime
Why C++ when Java!