# Java: Java vs. C

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 `int`s 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 `short`s to `int`s, 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.