James Cagney, 86, who rose from a hard-knocks youth on New York's East Side to achieve enduring movie fame as a brash, intrepid, irrepressible image of urban masculinity, and whose gallery of portrayals ranged from ruthless gangsters to estimable song-and-dance men, died yesterday at his farm in Stanfordville, N.Y. He had heart and circulatory ailments and diabetes.

Cagney is perhaps best known for his legendary and definitive portrayals of gangsters in such 1930s pictures as "The Public Enemy" and "Angels With Dirty Faces." He played hoofer and songwriter George M. Cohan in "Yankee Doodle Dandy" in 1942, and a comical but sinister Navy captain in "Mister Roberts." He last appeared as a 1920s New York police commissioner in the 1981 film "Ragtime."

He played working men, servicemen and entertainers as often as he portrayed toughs or mobsters.

The role he regarded as his most satisfying was the Cohan part in the biographical musical "Yankee Doodle Dandy," for which he earned the Academy Award as best actor.

In a statement issued yesterday from the White House press office in Santa Barbara, Calif., President Reagan said: "Nancy and I have lost a dear friend of many years today, and America has lost one of her finest artists. Jimmy burst upon our movie screen with an energy and a talent we have never seen before and we will never see again. He was the best at whatever he did -- a hero, a villain, a comic, or a dancer. Jimmy Cagney was the classic American success story, lifting himself by determination and hard work out of poverty to national acclaim."

There was something about Cagney's general air of self-assurance and defiance, combined with a colloquial zest and familiarity, that seemed destined to evoke a richly sympathetic response in the early years of the Depression and talking pictures.

Cagney was easily distinguished from the acting pack by his compact frame, springy walk, incisive gestures and crisply accelerated diction. Also notable were his impudent-to-contemptuous smiles and peculiarly assertive posture, which suggested a disturbing potential for violence coiled inside a disarmingly relaxed and sawed-off frame.

He insisted that "I am not the characters I portray," disavowing any suggestion of personal identification with criminal characters or antisocial behavior. What he would affirm, in typically modest and practical terms, was a boyhood acquaintance with street fighting and companions who ended up behind bars. He regarded them as natural features of a slum setting.

Recruited from the Broadway stage in 1930, he quickly became a sensation. Cast as a vicious and unregenerate yet perversely attractive and sometimes amusing young thug in "The Public Enemy," he electrified the public and alarmed pressure groups, which deplored the movies as an influence on mass taste and behavior.

In the single most indelible gesture of his movie career, Cagney was obliged to shove a grapefruit half into the face of actress Mae Clarke, cast as a whining moll, to illustrate his character's contempt for self-pitying prattle.

Released in 1931, "The Public Enemy" was Cagney's fifth movie, an incisive and explosive gangster melodrama shot quickly (26 days) and cheaply ($150,000) in the early stages of a Warner Bros. contract that kept the actor averaging about five films a year through 1935.

When "The Public Enemy" promptly earned seven times its production cost while at the same time provoking controversy, it was clear that Cagney had emerged as a star.

When he accepted the American Film Institute's Life Achievement Award in 1974, Cagney invoked the names of many boyhood pals. All, he said, were "part of a stimulating early environment which produced that unmistakable touch of the gutter without which this evening might never have happened."

Cagney took a dim view of Hollywood studio management and celebrity life. Habitually suspicious of management and vigilant in his own self-interest, he staged periodic walkouts between 1932 and 1942 over contract terms.

He kept a considerable distance between his private and working lives, usually beating a hasty retreat to the Eastern countryside when not filming. He maintained farms on Martha's Vineyard and later in Dutchess County, N.Y.

His impact and influence were probably far more affirmative socially and artistically than critics who admire his gangster roles have been willing to recognize. Otis Ferguson, film critic for The New Republic, praised his work in Howard Hawks' 1935 aerial melodrama "Ceiling Zero."

Describing the typical Cagney character as "tough and bright and endearing as life," Ferguson went on to remark that "seeing him you couldn't help feeling better about the industry -- or the state of the union for that matter . . . . This half-pint of East Side Irish somehow managed to be a lot of what a typical American might be, nobody's fool and nobody's clever ape, quick and cocky but not too wise for his own goodness, frankly vulgar in the best sense, with the dignity of the genuine worn as easily as his skin.

" . . . It is hard to say what our impression of the total American character would have been without him. He is all crust and speed and snap on the surface, a gutter fighter with the grace of dancing, a boy who knows all the answers and won't even wait for them. But underneath: the quick generosity and hidden sweetness, the native humor and restless drive -- everything everybody would like to be, if he had the time."

James Cagney was born July 1, 1899, in New York City, the second of five children born to an Irish bookkeeper turned bartender and saloon owner, James Francis Cagney, and his Irish-Norwegian wife, the former Carolyn Nelson.

Cagney held part-time jobs from the age of 14 -- as an office boy at the New York Sun, a custodian at the New York Public Library and a package wrapper at the Wanamaker department store. He also worked at the Lenox Hill Settlement House, where he first became involved in amateur theatricals by painting sets and posters and designing the house magazine.

Following the death of his father, Cagney began to seek full-time employment. He heard about an opening in a vaudeville dance act playing Keith's 81st Street Theater. He got the job -- a member of the chorus line in "Every Sailor," a female impersonation act.

He got his first Broadway job the next year, 1920, in a musical called "Pitter Patter." A member of the chorus, he also met his future wife, Frances Vernon.

For the next few years Cagney shuttled between vaudeville and small business, polishing his song-and-dance routines and failing at two attempts to start a dance school with Vernon, whom he married in 1922. The newlyweds also toured with vaudeville acts of their own.

He made his Broadway dramatic debut in 1925 as a character named Little Red ("there wasn't much competition") in Maxwell Anderson's "Outside Looking In." He landed a pivotal leading role in 1929 in George Kelly's "Maggie the Magnificent," teamed with Joan Blondell.

Cagney and Blondell were soon reunited in "Penny Arcade," a play that cast him as "a sniveling murderer" and her as "a smart-cracking gal."

Though it folded in three weeks, the show caught the attention of Al Jolson, who optioned it and persuaded Warner Bros. to try a film version with the original leads. The movie was released as "Sinner's Holiday."

Signed by Warner Bros., he soon made "The Public Enemy." He recalled, "The story was about two street pals -- one soft-spoken, the other a really tough little article. For some incredible reason, I was cast as the quiet one; Eddie Woods, a fine actor but a boy of gentle background, well spoken and well educated, became the tough guy.

"Fortunately, Bill Wellman, the director . . . quickly became aware of the obvious casting error. He knew at once that I could project that direct gutter quality, so Eddie and I switched roles suspicious of management and vigilant in his own self-interest, he staged periodic walkouts between 1932 and 1942 over contract terms.

He kept a considerable distance between his private and working lives, usually beating a hasty retreat to the Eastern countryside when not filming. He maintained farms on Martha's Vineyard and later in Dutchess County, N.Y.

His impact and influence were probably far more affirmative socially and artistically than critics who admire his gangster roles have been willing to recognize. Otis Ferguson, film critic for The New Republic, praised his work in Howard Hawks' 1935 aerial melodrama "Ceiling Zero."

Describing the typical Cagney character as "tough and bright and endearing as life," Ferguson went on to remark that "seeing him you couldn't help feeling better about the industry -- or the state of the union for that matter . . . . This half-pint of East Side Irish somehow managed to be a lot of what a typical American might be, nobody's fool and nobody's clever ape, quick and cocky but not too wise for his own goodness, frankly vulgar in the best sense, with the dignity of the genuine worn as easily as his skin.

" . . . It is hard to say what our impression of the total American character would have been without him. He is all crust and speed and snap on the surface, a gutter fighter with the grace of dancing, a boy who knows all the answers and won't even wait for them. But underneath: the quick generosity and hidden sweetness, the native humor and restless drive -- everything everybody would like to be, if he had the time."

James Cagney was born July 1, 1899, in New York City, the second of five children born to an Irish bookkeeper turned bartender and saloon owner, James Francis Cagney, and his Irish-Norwegian wife, the former Carolyn Nelson.

Cagney held part-time jobs from the age of 14 -- as an office boy at the New York Sun, a custodian at the New York Public Library and a package wrapper at the Wanamaker department store. He also worked at the Lenox Hill Settlement House, where he first became involved in amateur theatricals by painting sets and posters and designing the house magazine.

Following the death of his father, Cagney began to seek full-time employment. He heard about an opening in a vaudeville dance act playing Keith's 81st Street Theater. He got the job -- a member of the chorus line in "Every Sailor," a female impersonation act.

He got his first Broadway job the next year, 1920, in a musical called "Pitter Patter." A member of the chorus, he also met his future wife, Frances Vernon.

For the next few years Cagney shuttled between vaudeville and small business, polishing his song-and-dance routines and failing at two attempts to start a dance school with Vernon, whom he married in 1922. The newlyweds also toured with vaudeville acts of their own.

He made his Broadway dramatic debut in 1925 as a character named Little Red ("there wasn't much competition") in Maxwell Anderson's "Outside Looking In." He landed a pivotal leading role in 1929 in George Kelly's "Maggie the Magnificent," teamed with Joan Blondell.

Cagney and Blondell were soon reunited in "Penny Arcade," a play that cast him as "a sniveling murderer" and her as "a smart-cracking gal."

Though it folded in three weeks, the show caught the attention of Al Jolson, who optioned it and persuaded Warner Bros. to try a film version with the original leads. The movie was released as "Sinner's Holiday."

Signed by Warner Bros., he soon made "The Public Enemy." He recalled, "The story was about two street pals -- one soft-spoken, the other a really tough little article. For some incredible reason, I was cast as the quiet one; Eddie Woods, a fine actor but a boy of gentle background, well spoken and well educated, became the tough guy.

"Fortunately, Bill Wellman, the director . . . quickly became aware of the obvious casting error. He knew at once that I could project that direct gutter quality, so Eddie and I switched roles after Wellman made an issue of it with production boss Darryl Zanuck."

Sensing his enhanced commercial value after "The Public Enemy" and aware that his weekly salary did not compare with the $125,000 a feature that Warner was paying established stars, Cagney resolved to "be difficult."

The bargaining, reinforced on Cagney's side by "an entire series of walkouts," brought salary increases that lifted him to $4,500 a week by 1935 and finally to $150,000 a picture by 1937, when he returned to Warner after a year of self-exile.

Few, if any, of the films of this period seem to have given him much satisfaction. He was fond of "Here Comes the Navy" in 1934 because it began his costarring series with Pat O'Brien, and he regarded "G-Men," a pivotal hit of 1935, as "a step up the ladder artistically." He also played an amusing Bottom in Warner's sumptuous but commercially misbegotten production of "A Midsummer Night's Dream."

He returned from his 1937 walkout to star in "Angels With Dirty Faces." He earned his first Oscar nomination and the New York Film Critics' award.

He was particularly proud of the denouement of the film, in which his character, Rocky, breaks down on his way to the death chamber but leaves it uncertain whether the hysteria is authentic or feigned, a farewell gesture of friendship to the priest who has begged him to "act yellow" in order to disillusion hero-worshiping slum kids.

The triumph of "Yankee Doodle Dandy" in 1942 left Cagney indisputably at the peak of his profession. "Yankee Doodle Dandy" served dual purposes as a rousing entertainment and wartime morale builder. Enormously popular in the United States and Britain, the film was such an effective fund raiser that the New York, London and Los Angeles benefit premieres subsidized the purchase of three Liberty ships.

Cagney had pursued the Cohan biography with more than usual deliberation, coveting it as both a show business salute and patriotic testament calculated to "remove the taint that . . . still attached itself to my reputation -- a reputation now scarred by my so-called radical activities in the thirties when I was a strong Roosevelt liberal."

A combination of union activism, Democratic Party loyalty and sometimes "radical" beau gestes -- such as support for the Loyalist cause in the Spanish Civil War and a starring role in the original radio broadcast of Dalton Trumbo's "Johnny Got His Gun" -- had combined to identify Cagney as a "fellow traveler" in right-wing circles.

A recent book, "Inside Warner Bros.," consisting of internal company letters and memoranda, revealed that this identification cost him the title role in "The Knute Rockne Story" in 1939. The first choice of studio management, his selection was vetoed by the University of Notre Dame, which cited his support for the Spanish Loyalists and his previous Hollywood roles as gangsters.

Cagney, who became a conservative in later life, had served as president of the Screen Actors Guild and had chaired the Actors' Committee of the National Victory Committee in the 1940s. He traced a political evolution from "what might in general be called a liberal stance" to "archconservatism," echoing Clemenceau's observation that he had little use for a young man who had not been a socialist at 20 and even less for one who remained a socialist at the age of 40.

The success of "Yankee Doodle Dandy" also led Cagney to "reassess" his position at Warner Bros. He staged his longest walkout, which lasted until 1948 and saw the formation of an independent production company with a five-picture deal for United Artists. None of those productions made a significant showing at the box office.

Turning 50, and reconciling once again with Warner, Cagney added an astonishing, flamboyant characterization to his rogues' gallery -- Cody Jarrett, the demonically insane, mother-fixated protagonist of "White Heat," which was released in 1949.

Becoming a free-lancer in 1954, Cagney drew two excellent roles the following after Wellman made an issue of it with production boss Darryl Zanuck."

Sensing his enhanced commercial value after "The Public Enemy" and aware that his weekly salary did not compare with the $125,000 a feature that Warner was paying established stars, Cagney resolved to "be difficult."

The bargaining, reinforced on Cagney's side by "an entire series of walkouts," brought salary increases that lifted him to $4,500 a week by 1935 and finally to $150,000 a picture by 1937, when he returned to Warner after a year of self-exile.

Few, if any, of the films of this period seem to have given him much satisfaction. He was fond of "Here Comes the Navy" in 1934 because it began his costarring series with Pat O'Brien, and he regarded "G-Men," a pivotal hit of 1935, as "a step up the ladder artistically." He also played an amusing Bottom in Warner's sumptuous but commercially misbegotten production of "A Midsummer Night's Dream."

He returned from his 1937 walkout to star in "Angels With Dirty Faces." He earned his first Oscar nomination and the New York Film Critics' award.

He was particularly proud of the denouement of the film, in which his character, Rocky, breaks down on his way to the death chamber but leaves it uncertain whether the hysteria is authentic or feigned, a farewell gesture of friendship to the priest who has begged him to "act yellow" in order to disillusion hero-worshiping slum kids.

The triumph of "Yankee Doodle Dandy" in 1942 left Cagney indisputably at the peak of his profession. "Yankee Doodle Dandy" served dual purposes as a rousing entertainment and wartime morale builder. Enormously popular in the United States and Britain, the film was such an effective fund raiser that the New York, London and Los Angeles benefit premieres subsidized the purchase of three Liberty ships.

Cagney had pursued the Cohan biography with more than usual deliberation, coveting it as both a show business salute and patriotic testament calculated to "remove the taint that . . . still attached itself to my reputation -- a reputation now scarred by my so-called radical activities in the thirties when I was a strong Roosevelt liberal."

A combination of union activism, Democratic Party loyalty and sometimes "radical" beau gestes -- such as support for the Loyalist cause in the Spanish Civil War and a starring role in the original radio broadcast of Dalton Trumbo's "Johnny Got His Gun" -- had combined to identify Cagney as a "fellow traveler" in right-wing circles.

A recent book, "Inside Warner Bros.," consisting of internal company letters and memoranda, revealed that this identification cost him the title role in "The Knute Rockne Story" in 1939. The first choice of studio management, his selection was vetoed by the University of Notre Dame, which cited his support for the Spanish Loyalists and his previous Hollywood roles as gangsters.

Cagney, who became a conservative in later life, had served as president of the Screen Actors Guild and had chaired the Actors' Committee of the National Victory Committee in the 1940s. He traced a political evolution from "what might in general be called a liberal stance" to "archconservatism," echoing Clemenceau's observation that he had little use for a young man who had not been a socialist at 20 and even less for one who remained a socialist at the age of 40.

The success of "Yankee Doodle Dandy" also led Cagney to "reassess" his position at Warner Bros. He staged his longest walkout, which lasted until 1948 and saw the formation of an independent production company with a five-picture deal for United Artists. None of those productions made a significant showing at the box office.

Turning 50, and reconciling once again with Warner, Cagney added an astonishing, flamboyant characterization to his rogues' gallery -- Cody Jarrett, the demonically insane, mother-fixated protagonist of "White Heat," which was released in 1949.

Becoming a free-lancer in 1954, Cagney drew two excellent roles the following year -- the choleric captain in the film version of "Mister Roberts" and Marty Snyder, the gangster patron of singer Ruth Etting, in "Love Me or Leave Me," which he embraced as "that extremely rare thing, the perfect script." It certainly had a perfect part for him and brought another Oscar nomination.

His performances remained creditable and distinctive for the remainder of the decade -- a cameo reprise of George M. Cohan in "The Seven Little Foys," Lon Chaney in "Man of a Thousand Faces," an IRA gunman in "Shake Hands With the Devil," Admiral Halsey in "The Gallant Hours" -- but the movies themselves were more presentable than inspired.

Cagney retired from the screen in 1961, following his performance as a fast-talking American sales executive in Billy Wilder's Cold War farce "One, Two, Three." His interests shifted to farming, painting, stockbreeding and conservation on a full-time basis.

In 1979 he was finally persuaded to come out of retirement, largely on the insistence of his doctor, who believed that inactivity was contributing to a worsening diabetic condition. Cast as a New York police chief in Milos Forman's movie version of "Ragtime," Cagney gave the film an invaluable publicity boost and a glimmer of human solidity and authority.

He received the Kennedy Center Honor for lifetime achievement in 1980. In 1983, the National Film Society presented him with its Golden Screen Award.

Among the actors with whom he had appeared was Ronald Reagan. They starred in the 1938 film "Boy Meets Girl." In 1984, President Reagan presented the Medal of Freedom to his old friend. Cagney listened, his eyes brimming with tears, as the president described him as "a giant in the world of entertainment" who "was never too busy to hold out a hand to a young fellow trying to get under way."

Cagney's survivors include his wife, who lives in Stanfordville. The Cagneys adopted two children, James Jr., who died in 1984, and Cathleen.