Skip to main content

How Java differs from C and C++

How Java differs from C and C++

This appendix contains a description of most of the major differences between C, C++, and the

Java language. If you are a programmer familiar with either C or C++, you may want to review

this appendix to catch some of the common mistakes and assumptions programmers make when

using Java.

Pointers

Java does not have an explicit pointer type. Instead of pointers, all references to objects—

including variable assignments, arguments passed into methods, and array elements—are

accomplished by using implicit references. References and pointers are essentially the same things

except that you can’t do pointer arithmetic on references (nor do you need to).

Reference semantics also enable structures such as linked lists to be created easily in Java without

explicit pointers; merely create a linked list node with variables that point to the next and the

previous node. Then, to insert items in the list, assign those variables to other node objects.

Arrays

Arrays in Java are first class objects and references to arrays and their contents are accomplished

through explicit references rather than via point arithmetic. Array boundaries are strictly

enforced; attempting to read past the ends of an array is a compile or run-time error. As with

other objects, passing an array to a method passes a reference to the original array, so changing

the contents of that array reference changes the original array object.

Arrays of objects are arrays of references that are not automatically initialized to contain actual

objects. Using the following Java code produces an array of type MyObject with ten elements, but

that array initially contains only nulls:

 

MyObject arrayofobjs[] = new MyObject[10];

You must now add actual MyObject objects to that array:

for (int i; i< arrayofobjs.length. i++) {

arrayofobjs[i] = new MyObject();

 

Java does not support multidimensional arrays as in C and C++. In Java, you must create arrays

that contain other arrays.

Strings

Strings in C and C++ are arrays of characters, terminated by a null character (\0). To operate

on and manage strings, you treat them as you would any other array, with all the inherent

difficulties of keeping track of pointer arithmetic and being careful not to stray off the end of

the array. Strings in Java are objects and all methods that operate on strings can treat the string as a

complete entity. Strings are not terminated by a null, nor can you accidentally overstep the end

of a string (like arrays, string boundaries are strictly enforced).

Memory Management

All memory management in Java is automatic; memory is allocated automatically when an

an object is created, and run-time the garbage collector (the “GC”) frees that memory when the object

is no longer in use. C’s malloc and free functions do not exist in Java.

To “force” an object to be freed, remove all references to that object (assign variables holding

it to null, remove it from arrays, and so on). The next time the Java GC runs, that object is

reclaimed.

Data Types

As mentioned in the early part of this book, all Java primitive data types (char, int, long, and

so on) have consistent sizes and behaviour across platforms and operating systems. There are no

unsigned data types as in C and C++ (except for char, which is a 16-bit unsigned integer).

The boolean primitive data type can have two values: true or false. Boolean is not an integer,

nor can it be treated as one, although you cannot cast 0 or 1 (integers) to boolean types in Java.

Composite data types are accomplished in Java exclusively through the use of class definitions.

The struct, union, and typedef keywords have all been removed in favour of classes.

Casting between data types is much more controlled in Java; automatic casting occurs only when

there will be no loss of information. All other casts must be explicit. The primitive data types

(int, float, long, char, boolean, and so on) cannot be cast to objects or vice versa; there are

methods and special “wrapper” classes to convert values between objects and primitive types.

Operators

Operator precedence and association behave as it does in C. Note, however, that the new

keyword (for creating a new object) binds tighter than dot notation (.), which is different

behaviour from C++. In particular, note the following expression:

new foo().bar;

This expression operates as if it were written like this:

(new foo()).bar;

Operator overloading, as in C++, cannot be accomplished in Java. The operator of C has been

deleted. The >>> operator produces an unsigned logical right shift (remember, there are no unsigned data

types).

The + operator can be used to concatenate strings

 

Comments

Popular posts from this blog

4 Easy Steps Best Learning Plan For Programming

  4 Easy Steps Best Learning Plan For Programming in this article, we plan how to learn and practice programming in C language. in the program, we try to cover all the topics of basic C language like pointers, array, loops, if-else statements etc. we work on the following strategy... 1) learn the theory of programming. in this section, we are trying to cover all the important topics of basic C. here are some following topics which are cover in upcoming articles. table of content...... 1.variable constant and keywords. 2.instructions and operators. 3.conditional instructions. 4.loop control instructions. 5.functions and recursions. 6.pointers. 7.array. 8.strings. 9.structures. 10.file input/output. 11.dynamic memory allocations. 2) complete a practice set. we have written these practice sheets and source code for you. in these sheets we are trying to cover all the topics of the theory part 3) solve programming questions. in this section homework questions for you. 4) creating sof...

How to Attract People By Search Engine Marketing

How to Attract People By Search Engine Marketing: Any business that can come online must come online !! But it is not that you have created your website and people will just start coming to it. Search engine is the best way to call people to your website. Now the search engine is also many, whose user in India is something like this. Google 97.65% Bing 1.39% Yahoo 0.89% DuckDuckGO 0.03% (According to IndianMarketer) Everyone knows that Google is the most famous search engine, There are two ways to get traffic or logo from Google Search engine optamization (SEO) Search engine marketing (SEM) What is SEARCH ENGINE MARKETING? Search engine marketing or SEM is the best way to rank on Google in any category. Nowadays everyone wants to increase your bussiness, in such a situation where big companies have made their place on the first page of Google, the same SEM gives us the chance that we too can spend above money by coming to those websites. When we come above SEM, it is called inorgan...

How To Know Best Tools For Social Media Marketing Part 2

  Best Tools For Social Media Marketing Part 2 Today we will tell you about social media marketing tools Part 2. in this article remaining 6 tools, which are helping in a social media campaign. Unsplash: - It is very important to have amazing images while having high-quality textual content. Unsplash provides a database of high-resolution, great images that you can use with your posts, the fact that a good image has its own unique significance cannot be denied. Each image is properly tagged so that you can find the most appropriate image for your post. Marin: - If your agency runs a social media advertising campaign (campaign), then this social media management tool will be useful for you. Marin offers a range of features that promise to increase your ROI by up to 30% compared to native equipment. One of its cool features is called auto-split which helps your audience in the micro-segment. Nimble: - As your customer list grows, a powerful CRM is needed to automate data manag...