Your idea can be made sound either by
- Using
.equals() instead of == if you are really storing strings, or
- Store characters instead of strings, in which case
== is safe.
Now if you are really making a noughts and crosses game, you have 8 different winning conditions, which, if you grow your current style of coding will have the form (here I am assuming characters, not strings):
winner =
(b[0] != ' ' && b[0] == b[1] && b[1] == b[2]) ||
(b[3] != ' ' && b[3] == b[4] && b[4] == b[5]) ||
...
(b[0] != ' ' && b[0] == b[4] && b[4] == b[8]);
There are other ways to do this; I'm sure a google search for tic-tac-toe or naughts and crosses implementations will show you quite a few.
If you would like to get fancy, there is a well-known technique of "labeling" each cell with a power of two. Then by adding up the scores (i.e., looking at the player's bit vector) and doing a binary AND on the set of eight winning conditions you can determine a winning position in one shot.
Here is a comment block for a tic-tac-toe game that illustrates the technique. (You didn't ask for actual code, so I'm withholding that from the answer):
/*
* To determine a win condition, each square is "tagged"
* from left to right, top to bottom, with successive
* powers of 2. Each cell thus represents an individual
* bit in a 9-bit string, and a player's squares at any
* given time can be represented as a unique 9-bit value.
* A winner can thus be easily determined by checking
* whether the player's current 9 bits have covered any
* of the eight "three-in-a-row" combinations.
*
* 273 84
* \ /
* 1 | 2 | 4 = 7
* -----+-----+-----
* 8 | 16 | 32 = 56
* -----+-----+-----
* 64 | 128 | 256 = 448
* =================
* 73 146 292
*
*/
var wins = [7, 56, 448, 73, 146, 292, 273, 84];
This was JavaScript. For Java, use
private static final int[] WINS = new int[]{7, 56, 448, 73, 146, 292, 273, 84};
Apologies if the binary logic approach here is not what you want; I thought it would be a good place to show it off though, in case others land on this page.
==on strings really means in Java....