If you did not initialize a local variable the variable might contain a garbage value which cannot be determined (in both C++ and Java ). For example
int x;
x will not be intialized automatically to zero( if it is an instance variable, then yes it would be intialized to zero in Java).
You are responsible for assigning an appropriate value before you use x. If you forget you get a compile-time error telling you the variable might not have been initialized. (Many C++ compilers will warn you about uninitialized variables, but in Java these are errors.)
The "C++ vs Java" Blog
Tuesday, 31 March 2015
Scope of variables in C++ vs Java
The scope of a variable could be defined as the places where the variable is accessible. For the following program, both C++ and Java behaves exactly in the same way. The curly braces divides the scope of the variables, and any variable out of scope would not be available for use.
{
int x = 12;
// Only x available
{
int q = 96;
// Both x & q available
}
// Only x available
// q is "out of scope"
}
But there is a slight difference when it comes to declaring two variables with the same name in nested scopes. Refer to the following piece of code
{
int x =12;
{
int x = 13;
}
}
The above code is perfectly valid in C++, but not in Java. In C++ within the nested scope x always refers to the value 13, but out of the nested scope x becomes 12. In Java the compiler will announce that the variable x has already been defined. Thus the C and C++ ability to “hide” a variable in a larger scope is not allowed, because the Java designers thought that it led to confusing programs.
{
int x = 12;
// Only x available
{
int q = 96;
// Both x & q available
}
// Only x available
// q is "out of scope"
}
But there is a slight difference when it comes to declaring two variables with the same name in nested scopes. Refer to the following piece of code
{
int x =12;
{
int x = 13;
}
}
The above code is perfectly valid in C++, but not in Java. In C++ within the nested scope x always refers to the value 13, but out of the nested scope x becomes 12. In Java the compiler will announce that the variable x has already been defined. Thus the C and C++ ability to “hide” a variable in a larger scope is not allowed, because the Java designers thought that it led to confusing programs.
Wednesday, 3 September 2014
Boolean in C++ vs java
In java boolean is a type and and it can take two values namely, true and false. "boolean" is also a keyword in java, and therefore cannot be used to name identifiers.
in C++ bool is the corresponding variable type with boolean in java. Same as java it can take two values namely true(which converts to an integral one) and false(which converts to an integral zero). The difference is in C++ both true and false are also keywords.
In C++ identifiers cannot be named as true or false because they are keywords in the language, but in java they are not keywords but literals , and still cannot be used as identifiers.
in C++ bool is the corresponding variable type with boolean in java. Same as java it can take two values namely true(which converts to an integral one) and false(which converts to an integral zero). The difference is in C++ both true and false are also keywords.
In C++ identifiers cannot be named as true or false because they are keywords in the language, but in java they are not keywords but literals , and still cannot be used as identifiers.
Tuesday, 22 July 2014
Access Modifers in Java vs C++
Access modifiers enables or disables access to a particular component in a programming language. It could be a variable, method or a class.
in Java the access modifiers behave as follows.
in C++ there is no notion of a package.
A public member is accessible from anywhere outside the class but within a program.
A private member variable or function cannot be accessed, or even viewed from outside the class. Only the class and friend functions can access private members.
A protected member variable or function is very similar to a private member but it provides one additional benefit that they can be accessed in child classes which are called derived classes.
in Java the access modifiers behave as follows.
Modifier | Class | Package | Subclass | World ————————————+———————+—————————+——————————+——————— public | y | y | y | y ————————————+———————+—————————+——————————+——————— protected | y | y | y | n ————————————+———————+—————————+——————————+——————— no modifier | y | y | n | n ————————————+———————+—————————+——————————+——————— private | y | n | n | n y: accessible n: not accessible
The above figure is copied from stackoverflow, and it illustrates how Java access modifiers behave. In addition to the above explanation it should be noted that in case if there is no access modifier, whether the subclass can access the component depends on where the subclass is. If it is in the same package it can access, and if it is not in the same package it cannot access.
in C++ there is no notion of a package.
A public member is accessible from anywhere outside the class but within a program.
A private member variable or function cannot be accessed, or even viewed from outside the class. Only the class and friend functions can access private members.
A protected member variable or function is very similar to a private member but it provides one additional benefit that they can be accessed in child classes which are called derived classes.
Friday, 25 April 2014
Object Creation in C++ vs Java
It is a known fact among all developers, that both Java and C++ are object oriented languages. Which means the main way a program operates is using objects operating on other objects. Therefore we need to create objects in both of these languages to successfully run useful programs.
Before object creation, let's understand the fact that a computer program has both a stack and a heap.
What is the stack? It's a special region of your computer's memory that stores temporary variables created by each function (including the
The advantage of using the stack to store variables, is that memory is managed for you. You don't have to allocate memory by hand, or free it once you don't need it any more.Another feature of the stack to keep in mind, is that there is a limit (varies with OS) on the size of variables that can be store on the stack. This is not the case for variables allocated on the heap.
If you do not create the objects in the stack in C++, then you will have to manually manage the memory each object owns. Which means once the object is no longer needed, you have to free the memory it had owned. This is because C++ does not have a garbage collector built into the language ( although third party garbage collectors are available ). In Java there is no need to manually manage the memory ( it has a garbage collector built in).
Before object creation, let's understand the fact that a computer program has both a stack and a heap.
What is the stack? It's a special region of your computer's memory that stores temporary variables created by each function (including the
main() function). The stack is a "FILO" (first in, last out) data structure, that is managed and optimized by the CPU quite closely. Every time a function declares a new variable, it is "pushed" onto the stack. Then every time a function exits, all of the variables pushed onto the stack by that function, are freed (that is to say, they are deleted). Once a stack variable is freed, that region of memory becomes available for other stack variables.The advantage of using the stack to store variables, is that memory is managed for you. You don't have to allocate memory by hand, or free it once you don't need it any more.Another feature of the stack to keep in mind, is that there is a limit (varies with OS) on the size of variables that can be store on the stack. This is not the case for variables allocated on the heap.
Unlike Java, in C++ objects can also be created on the stack.
For example in C++ you can write
Class obj; //object created on the stack
Class obj=new Class(); //If you need to create the object in the heap in C++
In Java you can write
Class obj; //obj is just a reference(not an object)
obj = new Class();// obj refers to the object
Therefore it is clear that C++ differs from Java with the ability it has to create objects in the stack.If you do not create the objects in the stack in C++, then you will have to manually manage the memory each object owns. Which means once the object is no longer needed, you have to free the memory it had owned. This is because C++ does not have a garbage collector built into the language ( although third party garbage collectors are available ). In Java there is no need to manually manage the memory ( it has a garbage collector built in).
Tuesday, 21 January 2014
Compilation in C++ vs Java
If you know a programming language, and how to use its syntax, you should also know how your instructions are converted into machine instructions (which is 1s and 0s).
C++ is considered to be a purely compiled language whereas Java is not.
To those who have no idea about what a compiled language is, the definition is as follows.
Compiled Language: A language that requires a compiler program to turn programming source code into an executable machine-language binary program. After compiling once, the program can continue to be run from its binary form without compiling again
The last line of the above definition is really important.It is what emphasizes the main difference of a compiled language vs interpreted language.
C++ is considered to be a purely compiled language whereas Java is not.
To those who have no idea about what a compiled language is, the definition is as follows.
Compiled Language: A language that requires a compiler program to turn programming source code into an executable machine-language binary program. After compiling once, the program can continue to be run from its binary form without compiling again
The last line of the above definition is really important.It is what emphasizes the main difference of a compiled language vs interpreted language.
To illustrate the main idea of this post, let's look deep into how the compilation process work in C++ and Java. The following image illustrates how a source file in C or C++ gets converted into machine language.
As you can see in C++, once the compilation is done, it is already converted into machine code( Even without the linking process ). An Object file contains machine instructions for that specific source file.The linking process links all the object files and makes a single executable which can be executed on that platform. Therefore whenever you want to run this program, what you do is run the executable.You do not need to compile it again.
In Java the process is a bit different. The following image explains how it is done.
In Java when you run " javac MyClass.java "the output is a .class file( contains byte code).This file is platform independent, and you can run the same file within different platforms(Linux or Windows or MacOS). Which means if you compile the source in a windows machine, and you run the .class file in a Linux machine, the program works exactly the same (you should have the JRE to run a java program). If you have a Java Runtime Environment installed on your computer, there exists a Java Interpreter in it which is able to run a .class file ( converts byte code in .class to machine instructions). when you execute "java MyClass", what really gets executed is this .class file.
Therefore as you can see, Java is not a purely compiled language. it does not convert the source file into an executable by only using the compiler. It uses a program called the Interpreter which gets executed each time when you run that Java program. It is what converts the .class into 1s and 0s and makes your program understandable to your machine.
"goto" in C++ vs Java
Many programmers believe that "goto" is a keyword that exists in C++, but not in Java. To be exactly correct, that is wrong.
In Java, there exists the keyword "goto". If you go through the keywords in Java by doing a google search, you may come across "goto". The difference when comparing to C++ is that , in Java there is no implementation defined for this keyword until today, but it is reserved as a keyword.
Why would anyone reserve a keyword which is not used? My feeling is that so they could be used one day ( in a future release) if the language designers felt the need. Therefore it is an unimplemented, but reserved keyword in Java. You shouldn't be using it as an identifier, and if you do so, the compiler may output an error.
In Java, there exists the keyword "goto". If you go through the keywords in Java by doing a google search, you may come across "goto". The difference when comparing to C++ is that , in Java there is no implementation defined for this keyword until today, but it is reserved as a keyword.
Why would anyone reserve a keyword which is not used? My feeling is that so they could be used one day ( in a future release) if the language designers felt the need. Therefore it is an unimplemented, but reserved keyword in Java. You shouldn't be using it as an identifier, and if you do so, the compiler may output an error.
Subscribe to:
Comments (Atom)

