0% found this document useful (0 votes)
6 views4 pages

Java vs C/C++: Key Differences

cpp and jvm

Uploaded by

Ishan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Java vs C/C++: Key Differences

cpp and jvm

Uploaded by

Ishan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

FUNDAMENTALS OF JAVA 4.

A1.7 Difference between Java, C and C++

Datatype:
In Java int takes four bytes as compared to C.C++, which take two bytes ot memoy
In Java char takes two bytes as compared to C, C++, which take one byte
memory.
In javanew datatypeboolean is added, it is not available in C, C++t.
Java doesn't supporttypedefand enum

Operators:
Java adds a new operator in bitwise that is right shift operator >>>
in C
which was not supported
% may be applied in Java for dividing float values,

and C++.
which is frequently used in C, Ct+ to
operator sizeofO,
Java doesnot supportspecial
find the memory size bytes of data elements.

Keywords: inaiy
inal,
keywords such as super, try, catch,
Java introduced some new
synchronized,package, interface etc.
that were used in
C and C++ such as typedef, enum,
Java dropped some keywords
delete etc.

Loops: statements
arrays. All other control
for loop used with
Java suPport a special type of
C++.
are same as in C,

Break and Continue: support labeled


continue statements, Java
Inaddition normal use of break and
to the
control over loops.
to provide better
break and continue statements

Pointers: in C
which is very important concept
the conceptof pointers,
Java does not support
and C++.
files:
and header
Preprocessor directives
directives and header
files.

Java does not have preprocessor


to employ inbuilt
java class library.
Java use import keyword
classes.
of packages to organize
Java use the concept

Portability:
is interpreted
intermediate code i.e. Bytecode which
Java programs are compiled to and can be
The byte code is machine independent
by the java run time environment.
executed on any plateform.
dependent executable code.
InC and C++ the source code in compiled to machine
and is not portable.
That is executed on the actual machine
FUNDAMENTALS OF JAVA
4.10

Inheritance:
C.
The concept of inheritance is not supported in is
inheritance directly which
do not support multiple
Java support inheritance but
available in C++.

Operator overloading:
is not supported in C.
overloading
The conceptof operator feature of C++.
which is an important
• Java doesn't support operator overloading

String representation:
terminated array of characters.
and C++ string is represented as null
In C

isan object.
In java every string

Memory management:
mamagement is programmer's responsibility.
In C and C++ memory

without worrying about


deallocation.
In javaprogrammer uses
memory freely

C++ delete() is used to deallocate memory.


InC free() function and in
garbage collection routine.
using
Injava nemory is deallocated

Multithreading:
used in java and not in C and C++.
The concept of multithreaded programming is

Exception handling:
The concept of exception handling is used in java and C++ but not in C.

Java provides better way of exception handling as compared to C++.

Console I/O:
. The
C+t.
method of console input/output is entirely different in java as comparedIto
Cand
1ogram to terminae w

ng an inputmismatch occurs. Por example,if you input 12

insteadoPemN wiCe
3.7 JAVA VIRTUAL MACHINE
machine
Most prgramming languages such as CC#
compile source code directly into
such as
de suitable for
exUion on a particular nmieroprocessor architecture or operating system
neural'. But Javia is
WindowS Or Unix This nnIety does not make these language 'architecural
anvwhere approach, This Teature
an arhitectural neutral languase as it follows Write once execute
the machine lan-
of Java is achieved
as the Java compiler
does not translate the source code into
code
on. Instead the compiler translates source
guage ot the computer that the program is running is needed
into byteade which is not understood
by the underlying operating system. So an application
operating system,
that can convert this bytecode into
nmachine code understood by the underlying

This isaomplished using the Java Vitual Machine.


(JVM) as a hypothetical computer platform
Tou can thìnk of the Java Virtual Machine is an cmulator i.e. a
not really exist as actual hardware. Instead
it

design or a computer that does


the JVM) that can
drive to act like a computer (namely
program thatsets side a part of your hard
CxeCute Java programs, Because of
JVM youcan execute a Java program on any compter hat has
without recompiling the program. This is how Java
provides
a Java runtime environment installed
platforn independence.

0101.e**.
JVM
for Windows

Java
Source Code WINDOWS OS
bytecode
JVM 0101...*
Java Complier bytecode for Unix

(javac)

UNIX OS
bytecode
[Link]
JVM 0101.
for Macintosh

MACINTOSH OS
Fig. 3.13

3.8 BÝTECODE
When youwork with conventional programming languages like Cor C++,the source code is

compiled into an executable form known as primary code or machine code. The machine code is a

pattern of0's and I's understandable by the underlying hardware architecture of the computer on
which the program is intended to run.

On the other hand in Java, source code is not complied into machine code bu rather into a

Learn Programing in Java 54


special intermediate format known as bytecode. Java bytecode is the forn of instructions that the

JVM (Java Virtual Machine) reads and translates them one by one into machine code that the
computer understands. When JVM loads a class file, it gets one stream of byte codes for each
method in the class. The bytecodes for that method will be executed, when the method is invoked
during the course of running the program.

Common questions

Powered by AI

Java manages memory through an automatic garbage collection process that relieves programmers from the responsibility of manually deallocating memory. In contrast, C and C++ require programmers to manage memory explicitly using functions like 'free()' in C and 'delete' in C++. The advantage of Java's approach is that it reduces the risk of memory leaks and other memory-related errors by automatically reclaiming memory that is no longer in use .

Labeled 'break' and 'continue' statements in Java provide enhanced loop control by allowing not only the exit from or continuation of the current loop, but also the ability to specify which loop to break from or continue at. This feature addresses the limitations of traditional 'break' and 'continue' statements in C and C++, which only operate on the nearest enclosing loop, thereby improving code readability and reducing complexity when dealing with nested loops .

In C and C++, strings are represented as null-terminated arrays of characters, which can lead to errors such as buffer overflows if not carefully managed. Java treats strings as objects, which allows for strings to be immutable and managed more safely and conveniently. This approach in Java simplifies string manipulation and reduces errors related to memory management around strings, such as buffer overflows .

Java avoids the complexity and potential ambiguity of multiple inheritance present in C++. Multiple inheritance can complicate software design by introducing the 'diamond problem', where a class might inherit the same method from multiple paths. Java addresses these issues by allowing multiple inheritance only through interfaces, promoting more robust and simpler design patterns. This design choice in Java reduces complexity, avoids method ambiguity, and enhances the maintainability of the codebase .

Java's lack of support for the 'sizeof' operator, which is heavily utilized in C and C++ for determining the memory size of data elements, means that Java abstracts away this level of hardware interaction. This limitation implies that Java is less suitable for system-level programming where precise memory size calculation is required. On the other hand, it simplifies memory management for application-level programming, allowing developers to focus more on business logic without worrying about low-level memory management .

The absence of pointers in Java significantly enhances security and reliability by preventing illegal access to memory. Pointers, as used in C and C++, can lead to vulnerabilities such as buffer overflows and memory corruption if not managed properly. Java avoids these risks by disallowing direct access to memory addresses, thereby eliminating a large class of errors and vulnerabilities associated with pointers, making Java programs more robust and secure by default .

Java's built-in support for multithreading allows for more straightforward implementation of concurrent tasks, greatly improving the efficiency and responsiveness of applications. This feature is natively supported through the 'Thread' class and 'Runnable' interface, making it easier to develop complex, parallel processing applications. In contrast, C and C++ lack this native support, requiring the use of external libraries or custom implementations, which increases the complexity and potential for errors in concurrent programming .

The Java Virtual Machine (JVM) plays a crucial role in Java's platform independence by executing Java programs on any device with a Java runtime environment, without the need for recompilation. This is achieved because Java source code is compiled into an intermediate format called bytecode rather than machine code specific to an operating system. This bytecode is then interpreted by the JVM, which converts it into machine-specific code at runtime. In contrast, languages like C and C++ compile source code directly into machine code that is specific to the architecture of the hardware, making such programs machine-dependent .

Java provides a more structured approach to exception handling as compared to C++ by offering specific constructs like 'try', 'catch', and 'finally', which help in clearly defining the error handling and recovery path in the code. Java's exception handling enforces that exceptions are declared or handled by the method, which improves the reliability and maintainability of code. In contrast, C++ exception handling is less explicit, potentially leading to unhandled exceptions and program crashes if not properly managed .

Java's bytecode is a key component of its 'Write Once, Run Anywhere' capability, as it is machine-independent and can be executed by any system with a Java Virtual Machine, regardless of the underlying hardware architecture. In contrast, C and C++ compile source code directly into machine-dependent executable code, which must be recompiled for different platforms. Java's bytecode, being interpreted by the JVM, enables seamless portability across different platforms without requiring recompilation .

You might also like