Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


Java Tutorials



Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java: Java vs. C

[an error occurred while processing this directive] Is Java easier or harder than C?

Java is harder because ...

  • Java is more powerful and can do much more than C. For example, C doesn't have a graphical user interface (GUI), and C doesn't have any way to do object-oriented programming (OOP). It's possible to write in Java in a C style, avoiding the new powerful features of Java. But that is foolish.
  • Java either checks for errors, or makes you check for errors. C lets you do many things that would cause errors (for example, convert strings to integers, or do I/O), but doesn't make you write code to handle the errors. Java makes you write try...catch statements around things that might cause problems.

Java is easier because ...

  • Java checks for errors. For example, Java checks subscripts to make sure they are in the correct range.
  • Java does things for you. There are a huge number of things that Java has already written for you. For example, expandable arrays, many data structures, etc. In C it would take a very long time to write and debug these things by yourself.
  • Java doesn't have the most dangerous things. The things in C which cause the most program errors are pointers, pointer arithmetic, and memory management. Java has replaced these with much, much safer things: references, subscription, and garbage collection.
The reason that everyone is very enthusiastic about Java is because it is easier (faster, cheaper, ...) to produce good programs.

From C++ to Java

If you know C or C++, you already know a lot of Java.

The Java language is based mostly on C and C++. You'll see that a lot of the basic language elements, for example, the primitive types, operators, and statements of Java are taken directly from C.

Primitive Java types are similar to C

Java includes types which are similar to those in C/C++ (char, short, int, long, float, and double). Unlike C/C++, Java defines exactly how these types are implemented. For example, the length of an int in C might be 16 bits on a PC, 32 bits on a workstation, and 60 bits on an old supercomputer. Java defines exactly how ints are represented (32 bits, two's complement, big-endian). All Java types are completely defined, in contrast to C where NO types are well specified.

Portability and types

Because of the lack of complete type definitions, moving a C/C++ program from one machine to another can be a giant headache, or even impossible. For example, to preserve the range of an integer variable you might have to change shorts to ints, but then you also have to change the corresponding format specifiers, union declarations, bit field declarations, shift operators, etc. This is only one example of the tremendous number of portability problems in C/C++. There are entire books written about these problems. Because the Java types are well defined, there are none of these problems.

Additional primitive types

Java has two additional primitive types: boolean and byte. The boolean type is a nice addition that is like Pascal boolean; it can have only the values true and false. The byte type is used for 8-bit integers because char is a Unicode character, which requires 16 bits.

Summary of the Java primitive types

Java integer types are two's complement: byte (signed 8 bits), char (unsigned 16 bits), short (signed 16 bits), int (signed 32 bits), and long (signed 64 bits).

The real surprise here is that char is 16 bits! This is because the Unicode character set is used so that characters in all major human languages can be represented. C doesn't specify the character set for char (no, it isn't ASCII, it can be EBCDIC or any other code) -- another sad C portability story.

The two types float (32 bits) and double (64 bits) use the IEEE 754 floating-point representation.

Java and C Arrays

Java arrays are very similar to C arrays although they must always be dynamically allocated. However, Java arrays are not just pointers into memory, but separate objects. One of the good benefits of this is that array subscript bounds are checked, which makes finding bugs much easier.

Java Strings are not Arrays of char

In Java there is two special object types, String and StringBuffer, that are used to store strings of characters. C uses arrays of char, but this causes many problems. Of course, you can use an array of characters in Java if you need to, but it is usually easier to use String or StringBuffer

Java Operators are similar to C

Most of the Java operators are the same as those in C/C++, however Java specifies the exact order of evaluation in an expression, which C does not.

Rather than list all of the 40+ operators that are the same, it's easier to list the differences.

There is an additional right shift operator, >>>, to add zero bits on the left regardless of the sign of the left operand. The C comma operator is limited to the first and third parts of the for loop, which is just about the only place it is normally used in C programs anyway.

There are no pointer operators (&, *, ->) because Java uses something called references instead of pointers. References are really pointers to objects, but are simpler, and safer, than pointers.

Java statements are similar to C

You'll see all the usual statement keywords with the same meanings: if, else, switch, case, break, default, for, do, while, continue, and return.

A few statements have been added to handle exceptions and threads. Java exceptions are basically errors, and there are statements to help detect and handle these errors. Java also allows more than one part of a program to run simultaneously using threads. You won't have to use threads for most programming, but in the cases where you do, there are statements to control the different threads.

Object Oriented Programming - OOP

Classes form the basis for Object Oriented Programming (OOP). Java classes are like C++ class, which are something like a C structs that includes both data fields and functions. Objects are created when memory is allocated for one of these class "structs". The basic idea is simple.

Java support for large programming projects

For small programming projects it's sufficient to use functions, sometimes in separate source files. Large programming projects need more control over the structure and visibility of the program elements. Java provides this control in the form of classes, packages, and interfaces, along with a number of ways to control who can see what.

Class libraries

Sometimes someone says that Java is a small language, smaller than C++, and perhaps smaller than C. This may be true if you ignore one of the most important things: the class libraries. There are a huge number of methods (the special OOP word for function) in the thousands of classes that are grouped in packages. Packages and OOP are the reasons that it's possible to have such large libraries.

Ask programming questions?



Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (post your own) View All Comments Latest 10 Comments:

you guy have absolutely no idea what you are talking about.
1. Almost all GUI Toolkits have C bindings, while the GFX layer is what caused troubles in the OpenJDK, so I would not judge the power of a language upon its runtime library.
2. it's C that does a lot more than Java, not the other way! take a look at pointers, memory handling, lowlevel stuff and so on. To be fair, you can also implement Classes in Plain ANSI C, try googling around I you do not believe.
That said, I like and use both, Java for networking stuff, C for desktop/mobile/embedded so don't get me for a C Fanboy

Posted by Claudio on Tuesday, 01.6.09 @ 09:41am | #83533

can u please suggest good books for learning java script validations ,as i am beginer and doesnt know much about programming.

Posted by khan on Friday, 01.2.09 @ 11:41am | #83420

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name


Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]