tags:

views:

630

answers:

8
+2  Q: 

Why Java programs?

I've had little exp. with java and since now i've worked with c++. what makes this one more special and preferred?

Moreover I would like to know about the use of System.in classes and parseInt classes.

+2  A: 

This is really very broad and I think these are really 2 or 3 different questions. I'll address the first one very briefly. Java utilizes garbage collection, or autmatic memory management. That is, arguably, the biggest difference between it from a language like C++. There are clearly some potential for increase in productivity in that you don't have to worry as much about memory, although in reality you do need to pay attention to your references. Perhaps you could refine your question a bit.

BobbyShaftoe
A: 

If you like to program really the object oriented way, then you need to go from C++ to Java. One of the problems with C++ is that most programmers actually use it as C and don't exploit all its OO features. Java is here stricter.

Janko Mivšek
+8  A: 

In theory (and sometimes in reality) Java programs also run on multiple platforms, "write once - debug, er, run everywhere" type of thing. That makes it very useful for a variety of projects.

In my personal experience, while learning Java shortly after being introduced to C++, Java seemed simpler and easier to learn and understand, hence more productive, as was said before. While program structure and syntax is very similar, there is no need to worry about pointers and other potentially dangerous language features.

MK_Dev
I wonder why has been this response flagged as offensive.
DrJokepu
Maybe a Sun employee objecting to the use of the "write once debug everywhere" jibe ;-)
Steve Jessop
I think too many people on this site jump to marking things as "offensive" at even the hint of something negative ... i.e. the "write once, debug everywhere ..." sort of thing.
BobbyShaftoe
+2  A: 
  • Java works in browsers! (Milpa for example). You can say Flash too, but with Java you can leverage the numerous classes coming with it (another advantage over C++, even if both languages has a good set of free libraries on the Net) and your knowledge of the language.
  • As said, Java is supported on many platforms with minimal adjustments, with a fast, efficient VM, from big servers to mobile phones.
  • OO support is arguably better designed, avoiding mistakes done in C++. Somehow, C# is to Java what Java is to C++ ^_^ (I won't argue on this, I don't know C# enough actually, it is just an historical point).
  • In the same spirit, Java is slightly more abstract, avoiding pointers, manual memory management and some other low level stuff.

That doesn't mean than one is better than the other, STL helps C++ for some of the issues above, etc.

I am not sure how to answer the last sentence, these are object and method respectively, not classes.
I never used System.in yet, I suppose it is usable if you feed the Java program with < or | on the command line. And parseInt is a static method of Integer class.

PhiLho
+11  A: 

Java is vastly easier to work with, especially when developing large programs.

  • Debugging: Java generates nice Stacktraces
  • Stability: You can catch every exception
  • Development Speed: you need no linker (which can take many minutes in C++); with a modern IDE (e.g. eclipse) you can edit code in-place while the program is running
  • Garbage Collection and run-time type safety eliminate whole classes of errors
  • really good free (as in beer) IDEs
mfx
A: 

With C++ you're programming "on the metal", whereas with Java you're programming towards a virtual machine. The Java software stack all the way down to the VM is constructed to give a highly abstracted programming experience. This is most clearly apparent in the use of datatypes "that just are" (i.e. the programmers need no understanding of how they translate into memory areas), garbage collection "that just works" (the programmers don't have to deal with allocation and deallocation issues) and the ubiquity of exceptions for error handling and propagation. Pointers are not part of Java, the system takes care of where and how things are allocated.

From this, you might see that the design philosophy of Java is very different from C++: Java tries to enforce that the programmer should stick to certain ways of working which are considered to be safe and to make programming easier. Some people hate this aspect of Java, other people love it.

harms
A: 

It really depends on what you're trying to do.

For a lot of higher level functionality where optimal performance may not matter, Java is easier and more reliable to use. For example, garbage collection, array checking, etc. It's also sandboxed, of course.

For me, another major benefit of Java is the use of reflection and of run time class loading. I write a lot of plugins within pluggable architectures, and can ensure I can add more new classes to a running program on any platform. Last time I tried to do that in C++, I had to mess with DLLs and COM.

Uri
A: 

The language features have already been mentioned (GC, reflection etc.). But there is another major difference: Libraries. I know there is the STL and Boost and all kinds of libraries out there, but they are not all of a piece, they all feel different. Often you are forced to use all kinds of C-APIs (e.g. threading or sockets, just to mention two things). All the C++ evangelists will now jump in and tell about some kind of cool OO-socket or OO-threading library, but they are not part of the STL. They should be. Its almost 2009 and everything is networked and multithreaded. This ought to be part of the standard library. Why is it bad to use those C-APIs? Because it is hard to use them in an object oriented programm. Try using Win32's CreateThread() with the listener-pattern (C#-users: read "delegates").

For a "rich client application", where performance is not a big deal, I would always use Java or C#. If I need raw speed (think signal processing or embedded applications), I would rather use C instead of C++.

BTW: I have used all four languages (C, C++, Java, C#) for a long time.

EricSchaefer
C-APIs can easily be wrapped within a C++ class. Also, those C-API calls that support things such as sockets aren't actually part of the ANSI library, and thus can't be portably included in STL.
Raymond Martineau