South Korea and Japan today agreed on a broad framework for strengthening ties between the two countries by expanding Japan's economic aid and the flow of industrial technology to its closest Asian neighbor, but they appeared to differ sharply on the formation of a three-way military alliance with the United States.

In a joint communique South Korean President Chun Doo Hwan and Japanese Prime Minister Yasuhiro Nakasone stressed that the "maintenance of peace and stability on the Korean Peninsula is of vital importance to the peace and stability of Japan and East Asia." The statement capped two days of summit talks during which Tokyo promised to provide Seoul with a $4 billion economic aid package and to undertake an enlarged role in the region more in keeping with Japan's economic influence.

Nakasone, the first Japanese leader to visit South Korea for political talks, arrived here yesterday in a bid to improve badly strained ties with the country that Japan ruled as a colony until the end of World War II. According to diplomatic analysts here, the trip, which came less than a week before Nakasone is scheduled to go to Washington for talks with President Reagan, marked a major step in Japan's efforts to bolster its responsibilities for underwriting the security of its Asian neighbors by nonmilitary means.

Nakasone, however, was quick to deny widespread speculation here that he intended to forge closer military ties among Japan, South Korea, and the United States. He told reporters today that Japan's Constitution, which renounces war, "allows the right to defend the nation, but rules out the right to collective defense." Japan, he said, would continue to limit its military efforts to "cooperation with the United States under the 32-year-old security treaty between the two countries."

Despite Nakasone's remarks, the South Korean press reported that Nakasone and Chun had agreed in talks on the need to cement a stronger bond in the defense sphere in cooperation with the United States. Both Seoul and Washington are believed to favor such an arrangement because it would prompt Tokyo to increase its now comparatively modest appropriations for security-related affairs. Japanese officials today denied that the two leaders had discussed the issue.

Reagan administration officials have been pressing Japan to shoulder greater responsibilities for protecting its vital sea lanes to a distance of 1,000 miles beyond its shores, while South Korea, in their view, should concentrate on its defenses against the threat of military aggression from Communist North Korea. Stronger ties between America's two key Asian allies, officials assert, are needed to help offset a rapid Soviet military buildup in the Pacific.

In a rare departure for a Japanese leader, however, Nakasone told Chun that he appreciated South Korea's defense efforts as contributing to stability on the Korean Peninsula under the current "severe circumstances." Japanese prime ministers have, in the past, shied away from stressing the importance of Seoul's heavy burden of military spending, which contrasts sharply with Japan's defense budgets. South Korea routinely spends roughly 6 percent of its gross national product on defense, compared to slightly less than 1 percent for Japan.

In the joint communique, Nakasone said he supported Chun's recent series of initiatives to reopen long-stalled talks with North Korea on the issue of the political unification of the Korean Peninsula and pledged that Japan would "as far as possible" provide South Korea with aid for use in its five-year economic development program, which began in 1982.

In separate talks today, Japanese Foreign Minister Shintaro Abe and his South Korean counterpart, Lee Bum Suk, agreed on a $4 billion package consisting of $1.85 billion in concessionary Japanese yen credits and $2.15 billion in loans from Japan's export-import bank.

The two sides also discussed the need to pare South Korea's large deficit in trade with Japan, which has averaged roughly $3 billion annually for the past five years. They agreed to address the issues of how to open Japan's markets to more South Korean goods and to accelerate the transfer of sophisticated Japanese industrial know-how at upcoming talks between officials at the working level.

Knowledgeable South Korean observers said that the interest charges on the aid loans and other conditions, which had been expected to be announced during Nakasone's visit, remained to be hammered out in similar talks.

As part of his attempts to establish a new period of more friendly ties with South Korea, Nakasone promised that he would take steps to improve the status of the approximately 700,000 Korean residents in Japan whose treatment has been a source of bitterness in relations between the two countries. Korean residents, many of whom were forced to go to Japan under a wartime labor conscription program, have been subjected to intense discrimination and denied Japanese citizenship and government-sponsored benefits