NEW YORK (Reuters) -- Japan overtook China as the largest non-U.S. holder of Treasuries in June, after raising its holdings to a nearly three-year high, according to U.S. Treasury department data released on Thursday.

Japan's holdings of U.S. Treasuries rose to $1.122 trillion in June, from $1.101 trillion in May, and were its largest since October 2016.

It was not the first time that Japan supplanted China as the largest non-U.S. Treasury holder. From January to May 2017, Japan held more Treasuries than China, data showed.

"The generally low- and negative-yielding sovereign debt market make Treasuries comparatively more attractive than European and Japanese debt," said Benjamin Jeffery, rates strategist at BMO Capital Markets in New York.

The steepening of the yield curve -- when rates on long-term bonds are higher than short-term notes -- in June made currency hedging costs "a bit less onerous for foreign buyers," he added.

China was the second largest owner of U.S. Treasuries with $1.112 trillion in June, compared with $1.110 trillion the previous month.

Overall, major foreign holders of Treasuries had $6.636 trillion of U.S. government debt in June, up from $6.539 trillion in May, suggesting continued demand for the safe-haven asset.

Foreign flows of U.S. Treasuries showed an outflow of $7.71 billion in June, from net selling of $32.785 billion in May. Foreign official institutions sold $14.605 billion during the month, compared with outflows from the same group totaling $21.998 billion in May.

Offshore private investors purchased Treasuries amounting to $7.071 billion in June.

Data also showed that after 13 straight months of selling, foreigners finally bought U.S. stocks in June to the tune of $26.589 billion, after outflows of $1.445 billion in May.

Foreigners also bought $99.1 billion in net long-term securities in April, after buying $4.6 billion in May, the report showed.