Jim Nabors, a singer and comic actor who played the bumbling but good-natured hayseed Gomer Pyle on "The Andy Griffith Show" before starring as an unlikely Marine recruit in "Gomer Pyle, U.S.M.C.," one of the most popular sitcoms of the 1960s, died Nov. 30 at his home in Hono­lulu. He was 87.

His death was confirmed to the Associated Press by his husband, Stan Cadwallader. The cause was not disclosed, but Mr. Nabors had a liver transplant in 1994 and heart surgery in 2012.

Mr. Nabors had never acted before Griffith saw him perform at a nightclub, where he portrayed a bumpkin whose high-pitched drawl changed to an operatic baritone when he broke into song.

"I don't know what you do," Griffith told Mr. Nabors, "but it's magic, whatever it is."

Mr. Nabors joined the cast of "The Andy Griffith Show" in 1963. The sitcom, which debuted in 1960, featured Griffith as a small-town sheriff in Mayberry, N.C., with Don Knotts as his hapless deputy, Barney Fife.

Mr. Nabors's character of Gomer Pyle — originally promised to George Lindsey, who later played Gomer's cousin, Goober — was a gas station attendant of limited abilities and lamblike innocence. Gomer was modeled, Mr. Nabors said, on people he knew in his native Alabama.

Jim Nabors, left, and Andy Griffith in 2003. (Louis Lanzano/AP)

Gomer's slack-jawed expressions of amazement — "Goll-leee!," "Shazam!" and "Surprise, surprise, surprise!" — became catchphrases of the time.

"He showed up in Mayberry and was an instant success," Daniel de Visé, author of the 2015 book "Andy & Don: The Making of a Friendship and a Classic American TV Show," said in an interview. "I think the very best episodes of that show were the ones that featured the three of them, Andy, Don and Jim. Andy and Don both thought Jim was a natural."

In one of his first encounters with Griffith, as Sheriff Andy Taylor, Gomer explains that he can't do mechanical work, "jus' gas an' oil, water 'nd air. Water and air's free. We don't make no charge for it.

"Now, you take gas and oil" — pronounced "awl" — "that is a different proposition entire" — pronounced en-TAHR. "We make a charge for that, depending on how many times the pump out there goes ding-dong. It's 30 cents a ding."

In another episode, Barney Fife stops Gomer on a Mayberry street for making an illegal U-turn.

As Barney leaves the scene, making a U-turn of his own, Gomer runs toward his police car: "Stop, Barney, stop! Citizen's arrest, citizen's arrest, citizen's arrest!"

He asks Barney to write himself a ticket.

"You've got to be kidding!" Barney says.

"You hear that, folks?" Gomer replies. "There are two sets of laws: One for the PO-lice and one for the ordinary citizens."

After a year and a half as one of the most beloved characters on "The Andy Griffith Show," Gomer enlisted in the Marines, thus launching a spinoff, "Gomer Pyle, U.S.M.C."

The show, which premiered in 1964, also featured Frank Sutton as Sgt. Carter, Gomer's crew-cut and flummoxed drill sergeant. Always out of step as he marched, Gomer performed menial tasks with eager pride, as his openhearted good nature prevailed in the end.

"Much of the show's humor," critic Gerald Nachman wrote in the New York Post in 1964, "seems to hinge on Gomer's slow blink and an undependable jaw that drops open on him at difficult moments, leaving a face that in essence is half teeth and half nose; two trusting brown eyes often give him the look of a well-meaning, unassuming old plow horse."

Mr. Nabors displayed his rich singing voice in some of the episodes of "Gomer Pyle, U.S.M.C.," which ran for five years and never finished out of the Top 10 in ratings.

The show aired during the height of the Vietnam War, but it deliberately did not address real-world concerns. Nevertheless, it made Mr. Nabors one of television's biggest stars.

"Gomer, he's part of me," Mr. Nabors told TV Guide in 1969. "He sees only the good. He's not interested in the bad."

Mr. Nabors often joined entertainer Bob Hope's USO tours of war zones, including a 1971 visit to a Marine base in Danang, South Vietnam.

"Gentlemen," Hope said to raucous applause, "I bring you your leader."

James Thurston Nabors was born June 12, 1930 in Sylacauga, Ala. His father was a police officer.

He was a self-taught singer who often amused himself by doing vocal impressions. He graduated from the University of Alabama in 1952, worked as a typist at the United Nations in New York, then behind the scenes in television in Tennessee before moving in the late 1950s to Los Angeles. He worked as a film cutter at NBC while polishing his comic nightclub act. He had a few appearances on Steve Allen's variety show before he was tapped for "Andy Griffith."

In addition to his role as Gomer Pyle, Mr. Nabors made frequent TV guest appearances as a singer and actor in the 1960s and 1970s. He had two variety shows of his own, one from 1969 to 1971 and another in the late 1970s.

One of his closest friends, Carol Burnett, had Mr. Nabors as a guest on each first episode of her TV variety show for 12 years running.

As a singer, he recorded more than 25 albums, several of which sold more than 1 million copies. He settled in Hawaii in the 1970s and made frequent returns to the mainland for appearances in Las Vegas and elsewhere. Almost every year from 1972 to 2014, Mr. Nabors sang "Back Home Again in Indiana" at the beginning of the Indianapolis 500 car race.

After living with hepatitis for many years, Mr. Nabors had a liver transplant in 1994.

Although many people in Hollywood knew that Mr. Nabors was gay, "I haven't ever made a public spectacle of it," he said in 2013. That year he married Cadwallader, a onetime firefighter who had been his partner for 38 years.

He has no other immediate survivors.

In a 2001, at a Marine base in Hawaii, the commandant of the Marine Corps, Gen. James Jones, officially gave Gomer Pyle a promotion from private first class to corporal, pinning a stripe on Mr. Nabors's lapel.

"It took me 38 years to get promoted," Mr. Nabors told the Marines gathered for the ceremony. "Y'all just hang in there — y'all can get a stripe, too!"