How to make an IO "equivalence" in Java and C++?

Asked

Viewed 185 times

9

I made the following code in C++:

#include <iostream>
template <typename type> class Foo
{
    public:
    Foo(type FooTest)
    {
        std::cout << "Foo " << FooTest;
    }
};

int main
{
    new Foo<double>(10.0);
}

Output: "Foo 10".

Java: (Mainclass.java)

public class MainClass
{

        public static void main(String[] args)
        {
            new Foo<Double>(10.0);
        }
}

    class Foo<type>
    {
        public Foo(type FooTest)
        {
            System.out.print("Foo ");
            System.out.print(FooTest);
        }
    }

Output: "Foo 10.0".

This, just one case to demonstrate what happens, the output is different in both cases for double. How can I match them (Java = 10 and C++ = 10.0) ?

  • It’s not the focus of the question but I think it’s important to note that your C++ code does some things that are not recommended/unnecessary, such as using new in this case ( causing a memory leak ) and creating a class for a function that could be released.

  • @Tiagogomes of course. If I want to make a code demonstrative, I should not worry too much about improvements; even so, to make a similar code in Java, the correct thing is to use orientation-to-objects.

2 answers

7


The default formatting in C++ tries to act "smart" and discards zeros from the decimal place that do not interfere with the value, if you put 1.1 both of you will see that the exit will be the same.

To have a fixed precision formatting you can use the handler std::setprecision accompanied by the std::fixed.

  • +1 for using Std :). In Java (if there was any way to manipulate it), I would cast the object for Double. You know a better solution?

  • @Lucashenrique I do not understand the question, what is going out is already a value Double then why the cast you say you would do?

  • For example, (in Java,) make a function that checks if the number has nonzero decimal places. If it returns false (it does not have), turn the number into int to show it as 10. bool has_decimal(double num) { return num%1==0;}

  • @Lucashenrique ah, I have no idea or program Java to speak the truth, I recommend a search in the formatting options in libraries. You can lean to one side: use fixed formatting on two, or try to follow what C++ does in Java, which even you are not asking.

  • @Lucashenrique The response to Java behaving like this is: http://stackoverflow.com/a/14126736/1000282

1

You can use the printf :)

C / C++

printf("Foo %.01f", fooTest);

Java

System.out.printf("Foo %.01f", fooTest);

Of course this is a specific case for double, how you are using a generic type would be interesting to declare a interface common in both systems (or else declare Wrappers to make the envelope of the operator<< in C++ and the method toString in Java), so that each type knows how to "print".

  • 1

    For C++ you can use the functions width and Precision. See http://www.cplusplus.com/reference/ostream/ostream/

  • Yes, and in Java the Decimalformat, but it’s nice to use the same method in both :).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.