TOKYO — Japan’s growth showed surprising strength in the three months that ended in June, but the country faces its most serious economic challenges in years.

Japan’s economy, the third largest in the world behind the United States and China, grew at an annualized rate of 1.8 percent in the second quarter of 2019, according to data released on Friday by the country’s cabinet. The figure exceeded economists’ expectations, which had been tempered by slowing global demand.

The results followed a growth spurt in the first quarter, at a revised rate of 2.8 percent, a phenomenon that many economists saw as a statistical fluke stemming from a drop in imports that enhanced the bottom line.

Friday’s report suggested Japan still has significant strengths despite a worsening global situation. Its consumers are spending and its businesses are investing, both healthy signs for a country with a population that is aging and whose numbers are shrinking.