Can cyclists legally ride on the sidewalk?
Safety

In general, riding on the sidewalk is more dangerous than riding on the road. There's higher risk from cars coming out of driveways than cars overtaking you from behind. Generally cars aren't looking for anything on a sidewalk moving more than about 3-4 mph, there's often visual obstacles (trees, sign posts, etc) so you're likely to be invisible to cars that might turn into you. Going against the direction of traffic gives everybody a bit less time to react to anything going wrong and puts you on the side car drivers typically aren't looking in.

If it's legal on a specific block, I might ride slowly (5mph, 8mph tops) for a block on the sidewalk to get to a spot where I can get onto the road properly. But be extra-aware of all driveways and side streets. I might also ride on the sidewalk for under 10 feet when coming off of the road via a driveway or curb cut in order to stop and lock up my bike.

Check your local laws

There is no statewide California law prohibiting operating a bicycle on a sidewalk, however California Vehicle Code Section 21206 allows local (county, city, etc) governments to regulate operation of bicycles on pedestrian facilities.

In other words, there is no California-wide answer to the legality. It may be illegal in the specific area you were riding, but legal on the next block over or legal on the other side of the street.

In the city I live in (in California), it's illegal in two defined "downtown" areas, but otherwise legal. In some cities it's illegal to ride on the sidewalk anywhere in the city.

Or perhaps it's simpler to explain that California Vehicle Code Section 21650(g) explicitly allows riding on sidewalks and through crosswalks, unless prohibited by local ordinance.