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

How Keyword and AD Auction Use and Work

How Keyword and AD Auction Use and Work: KEYWORD: MAIN PART OF SEM The keyword is the base of the search engine market. Whenever a user searches your product on google, he / she types Keyword only. Ads are shown to him only on the basis of his keyword. Now whenever you start SEM, you must first find the keyword related to the product, Which user can use to search your product. For keyword research you can use Google keyword planner. For example Maan is your tour and travel website. So go to the Google Keyword planner, Type tourist packages there Location - India Language - English Search network - Google On Google keyword planner you will find many related keywords. Who will meet your business. See competition and CPC from there and choose some keywords. Save these keywords in a separate file, they will be useful when creating ads. How does SEM AD AUCTION work? Every time someone searches on Google, ad auction runs. This ad auction checks the keyword that the user has written on G...

How To Know About Social Media Marketing

  WHAT IS SSM? Today there is hardly any person who is not familiar with the term social media social media. Today social media sites like Social Media, Facebook Facebook, Twitter Twitter, Instagram Instagram and WhatsApp Whatsaap have taken almost every person in their growing circle, Most of which people like social media even to like, share and comment. But today the increasing scope of social media has given rise to very economical and highly effective marketing, which is being used by millions of people today. This digital marketing is a part of Digital Marketing. Scope of Social Media Marketing: - Not a single person these days denies the popularity and importance of social media because for the past decade people have been using different social media platforms for different reasons regardless of their age and gender. Without doing. It shows social media and its reach scope to all. Users of Twitter and Facebook have grown very rapidly and grabbed their attention in...

How To Know Best Tools For Social Media Marketing Part 1

How To Know Best Tools For Social Media Marketing Part 1 Today we will tell you about social media marketing tools. Which will help you promote your brand online. This article will be in 2 parts, Part 1 and Part 2, in which 6-6 tools will be explained. Marketing experts know that every media marketing campaign they run on social media is a big and important part. Publicity and details on social media will soon be easily noticed by more and more people. As more and more businesses have become dependent on their online business presence, social media platforms have become a good and big medium for doing business. It has also become a means of building relationships and advertising products and services. . Fortunately, there are many social media tools in place for digital marketing agencies and marketers who are in charge of their clients' online brand presence to help them with their work. Help in advertising Digital marketers give their customers, more effectively, the social me...