0% found this document useful (0 votes)
8 views5 pages

Java What To Expect

This document outlines the differences and similarities between Java and C++, emphasizing that both languages are statically typed but with distinct structures for functions and classes. In Java, methods must be defined within classes and file names must match class names exactly, while C++ allows for more flexibility in function placement and naming. Additionally, Java organizes classes into packages that correspond to directories, contrasting with C++'s use of namespaces.

Uploaded by

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

Java What To Expect

This document outlines the differences and similarities between Java and C++, emphasizing that both languages are statically typed but with distinct structures for functions and classes. In Java, methods must be defined within classes and file names must match class names exactly, while C++ allows for more flexibility in function placement and naming. Additionally, Java organizes classes into packages that correspond to directories, contrasting with C++'s use of namespaces.

Uploaded by

colio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

How Java differs from

cpp
This is a more comprehensive breakdown of how the languages differ, Java is
strikingly similar to cpp and much of what work with java will work in cpp.

This document will be briefly showing these differences and similarities so it


will be easier to transfer your knowledge from one language to the next

In terms of studying you will be utilizing a similar book in order to learn the
language quickly. Then you will also be using the activities of the same cpp
150 course for this one. Be aware of that.

Declarations Abound
Much like cpp, java is a statically bound language. Meaning that every
special symbol must be declared and properly used or it will cause errors.
The compiler will keep track of this and will not allow you to deviate what
made that special symbol in the first place. An important point here is that
these errors will be caught at compile time, and they will always be
caught.

Python a programming/scripting language is dynamically typed. It can


legitimately claim to be strongly typed. We can to apply string operations to
ints or vice versa. But type errors are, in general, only caught when the
erroneous code is executed, not when the code is compiled/translated.
Classes
In cpp functions can be written as standalone prototypes and be use within
classes as methods. However they typically are represented with signature
functions in a multi file system where they are included in the main source
code file or within the same file system.

 However, in Java this is not the case. Functions are written


inside of classes as is there is no separation.
 Cpp can have unrestricted or ubounded functions. Java does not have
that sort of flexibility.
 They must be written within a class, so think of java of classes and
methods. Rather like in cpp functions and classes.

Java written method for a function within a class


public class AdditionUtilities {

public static int addEvenNumbers(int[] , int N) {

int sum = 0;

for (int i = 0; i < N; ++i) {

if (a[i] % 2 == 0) { // a[i] is even

sum += a[i];

return sum;

}
Files and Directories
File Names
File names of java are very specific much like when dealing with a multifile systems with
user defined header files. Remember this is all in classes meaning that all of your file
names have more purpose then just syntax.

Say you create Java class file, AdditionUtilities, where can we put
it?

 We must store this in a file named AdditionUtilities.java.


 The spelling of the file name, including the mixture of upper and lower
case letters, must exactly match the name of the class declared in the
file.

This is the same for cpp. However the application is a bit more restrictive for
java unlike cpp.

file names and directory names are part of the Java


syntax.
In C++, we could have store functions anywhere function in a file addEvens.cpp,
or addEvenNumbers.cpp, or even in HelloGeorge.cpp, They can even be standalone.

 In Java a public class (that is not nested within another class) must be
stored in a .java file with a name exactly matching the name of the
class.
 This being the case with the idea that the class has the function stored
with in it and the file itself .java is the class.
 So, if you want to use a function it must be used within class as a
method. Essentially java doesn’t have functions it only has methods
 That also means because the main function is a method it only has
member variables as well
4.2 Directories
But wait, there’s more!

To avoid the possibility that someone else might have used the name
“addEvenNumbers” in their own code, particularly in a library that we might
someday combine into our own project, classes are often “packaged up”.

If you come from C++


In C++, this is done with namespaces.

You are certainly familiar with the name space std, if only as a source of
annoyance because you need to take extra steps to use symbols from within
the C++ standard library.

If you wanted to declare a C++ variable, function, or class named “string”,


you can do so. That’s because your “string” will be in your own namespace
(by default, the empty-named global namespace) and your symbol string is
entirely different from std::string.

You might have encountered a few other namespaces, such as std::rel_ops (a


namespace nested inside the std namespace) or, perhaps boost (a
namespace used in a popular set of open-source libraries).

But, the truth is, most C++ programmers don’t make nearly as much use of
namespaces as the probably should.

In Java, package == directory


In Java, classes are commonly organized into packages.

 Packages often nest within one another.


 And the source code must be arranged accordingly.

 Each package name in Java will correspond to a directory of the name.


which is just simply the name of the class in that directory

 Packages is our cpp equalavent to headers files; however, they are also
tied with namespaces.
 This is #include and #import to be clear is the header file comparison.

You might also like