TOKYO - Japan's Nikkei share average rose for a fourth consecutive day on Wednesday, as heavyweight technology stocks tracked overnight gains in U.S. megacap growth stocks.

The Nikkei .N225 gained 1.26% to close at 40,580.76.

The broader Topix .TOPX also rose for a fourth straight day, closing up 0.54% at 2,872.18.

Wall Street's main stock indexes closed higher on Tuesday, boosted by gains in Tesla TSLA.O and megacap growth stocks, but volumes were thin ahead of the July 4 holiday and the closely watched release of June nonfarm payrolls on Friday. .N

"Investors rotated their targets today. They sold outperforming shares to take profits and bought cheaper stocks," said Naoki Fujiwara, senior fund manager at Shinkin Asset Management.

"The Nikkei tracked Wall Street, higher but also those who were late in catching up to the recent rally bought Nikkei futures, which lifted the main index," said Fujiwara.

Topix's growth stock index .TOPXG, which lags behind the value share index for its weekly performance, jumped 0.93%, while the value share index .TOPXV edged up 0.18%

Chip-making equipment maker Tokyo Electron 8035.T rose 2.6% to provide the biggest boost to the Nikkei. Chip-testing equipment maker Advantest 6857.T gained 2.38%.

Electronic components maker TDK 6762.T jumped 5.24% and silicon wafer maker Shin-Etsu Chemical 4063.T climbed 2.22%.

Meanwhile, Hitachi 6305.T slipped 1.58% to weigh on the Topix the most. Insurer Tokio Marine Holdings 8766.T dropped 1.18%. Toyota Motor 7203.T fell 0.24%.

Of the more than 1,600 stocks trading on the Tokyo Stock Exchange's (TSE) prime market, 59% of the shares rose, while 37% fell and 2% were unchanged.

The machinery sector .IMCHN.T rose 2.2% and was the top performer among the TSE's 33 industry sub-indexes.

(Reporting by Junko Fujita; Editing by Sonia Cheema and Varun H K)