Difference in primitive data between Java and c++?

I've programmed in Java before and now that I'm learning C++ I'm curious as to how C++ treats primitive data differently than java. What are some ways C++ handles primitive data differently than Java that I should look out for?
It's not so much primitive data types that are handled differently, but rather non-primitive types.
However, you should be aware that the size of primitive data types depends on the platform in C++.
Last edited on
I don't know how Java initializes primitive data types, but here's a quick summary on how C++ does it:

static and thread-local storage duration: all primitive data types are zero-initialized unless specified otherwise, that is,

1
2
3
4
5
6
7
#include <iostream>
double f1;
double f2 = 3.14;
int main()
{
    cout << f1 << '\n' << f2 << '\n';
}
0
3.14


automatic and dynamic storage duration: primitive data types are non-initialized if no initializer is used, and are zero-initialized if an empty initializer is used:


1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <memory>
int main()
{
    std::unique_ptr<double> pf1(new double);
    std::unique_ptr<double> pf2(new double());
    double f1;
    double f2 = double();
    std::cout << f1 << '\n' << f2 << '\n';
    std::cout << *pf1 << '\n' << *pf2 << '\n';
}
garbage
0
garbage
0


And yes, unlike Java, the sizes of the primitive data types in C++ change platform to platform and compiler to compiler.
Last edited on
Cubbi, in the first example, f1 doesn't print 0 for me, it prints some random number :\
@L B Talk about standards-defying compilers. That rule has been there since C++98 (just checked)
Topic archived. No new replies allowed.