• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

General Questions about java

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 2662
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79973
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java programs might have run slower 10 years ago, but that is no longer true.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Evil is afoot. But this tiny ad is just an ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic