Skip to main content

(Not So) Stupid Questions 7: >>, >>>, <<, and ?: operators

January 17, 2006


(Not So) Stupid Questions

>>, >>>, <<, and ?: operators





Editor's note: Sometimes the most interesting discussions begin when someone says, "This may be a stupid question, but .... " If the person asking the question has taken the time to think about the problem before asking, the question is often not stupid at all. The uncertainty points out an ambiguity in the specs, holes in the docs, or a search for how more experienced programmers might address a particular problem. From time to time, we will print one of the "(Not So) Stupid Questions" we receive and invite our readers to answer the question in the feedback section.

Remember that new people are joining the Java community all the time and may be looking for help from those with more experience. Also, those who began with Java as their first language can benefit from those coming to the community with experience in other languages. As always, answer the questions with kindness. You are also welcome to submit your questions to

This may be a stupid question, but ... "There are some weird Java operators I don't understand."

First thoughts:

In my development, I often use the operators for mathematical functions (+, -, *, etc.), assigning values (=), testing values (==, >, >=, etc.), and combinations of these (+=).

But sometimes, in other people's code, I occasionally see some weird "greater than/less than" type operators like >> and <<. I assume those aren't the mathematical "much greater than" and "much less than," but then, what are they? There's also a "question mark" operator (?) that really isn't very self-descriptive. What is that supposed to be telling me?

My question is:

What are these operators, what do they do, and why would I ever want or need to use them?

Related Topics >> Programming   |   

Comments

the "<<" and ">>" are bit

the "<<" and ">>" are bit wise operations and cause my binary mathematics is a little off, I won't go into to much detail, needless to say, they tend to move the "bits" of "bytes" around to either change the value or check for given values. You can see this a lot in color operations where the RGB values have been "encoded" into a integer and you want to decode the values again (not like web color values). The "?" operation is a short hand if statement...for example String result = null; if (check > 100) result = "Doing great"; else result = "Keep working!"; could be expressed as result = check > 100 ? "Doing great" : "Keep working!"