Time |
S |
Nick |
Message |
01:27 |
|
|
Jantz joined ##friendlyjava |
03:09 |
|
|
aditsu joined ##friendlyjava |
05:18 |
|
|
mr_lou joined ##friendlyjava |
05:41 |
|
|
Jantz joined ##friendlyjava |
06:38 |
|
|
Jantz joined ##friendlyjava |
06:41 |
|
|
Jantz joined ##friendlyjava |
06:44 |
|
|
Jantz joined ##friendlyjava |
06:46 |
|
|
Jantz joined ##friendlyjava |
14:35 |
|
|
mr_lou joined ##friendlyjava |
17:32 |
|
mr_lou |
Ohoy |
17:33 |
|
mr_lou |
Reading from a text file, we do: thisLine = br.readLine(); |
17:33 |
|
mr_lou |
But we can also do: while ((thisLine = br.readLine()) != null) { // do stuff } |
17:34 |
|
mr_lou |
Where can I read about that? I mean, the fact that putting an if statement in a paranthese in order to check if it worked? |
17:34 |
|
mr_lou |
Or whatever to call it. What's it called? |
17:40 |
|
aditsu |
that's a while statement |
18:07 |
|
aditsu |
mr_lou: the only "special" thing in there is using an assignment as an expression |
18:08 |
|
mr_lou |
I mean...... what's it called when you put a condition in paranthese, and can get out true or false or null? |
18:08 |
|
mr_lou |
if (value = read()) { Yes, a value was read} |
18:08 |
|
mr_lou |
instead of |
18:09 |
|
mr_lou |
value = read(); |
18:09 |
|
mr_lou |
if (value) { Yes, a value was read} |
18:09 |
|
aditsu |
well, what I said, using an assignment as an expression (more specifically, using the result of an assignment) |
18:10 |
|
aditsu |
and if (value = read()) is only in parentheses because "if" requires parentheses |
18:11 |
|
aditsu |
also, you "can get out true or false or null" only from an expression of type Boolean |
18:13 |
|
aditsu |
and you can do this kind of thing without any condition or if or while statement |
18:14 |
|
aditsu |
e.g. a = (b = 3) + 2; |
18:16 |
|
aditsu |
ah, it can be called an "assignment expression" |
20:06 |
|
mr_lou |
Let's take BufferedReader. |
20:06 |
|
mr_lou |
It has readline() |
20:07 |
|
mr_lou |
readline() reads a line of text. But returns null if it's the end of stream. |
20:07 |
|
mr_lou |
So you do |
20:07 |
|
mr_lou |
String line = ""; |
20:07 |
|
mr_lou |
while (line!=null) line = readline(); |
20:08 |
|
mr_lou |
And this is where you instead can do |
20:08 |
|
mr_lou |
while ((line = readline())!=null) |
20:08 |
|
mr_lou |
Where does null come from here? Is it because readline() returns null, or is it because the expression returns null? |
20:12 |
|
aditsu |
both |
20:12 |
|
aditsu |
the expression can return null because it returns the result of readline() |
20:17 |
|
mr_lou |
Hm |
20:17 |
|
mr_lou |
int a = 4; System.out.println(a=7); |
20:17 |
|
mr_lou |
I thought something like that would maybe output "true" because it was succesful, or something. |
20:17 |
|
mr_lou |
But it outputs 7 |
20:18 |
|
mr_lou |
Anyway.... break time. |
20:18 |
|
aditsu |
well, how can the statement "a=7" be considered successful? |
20:19 |
|
aditsu |
or not successful? |
20:19 |
|
aditsu |
you're just assigning the value 7 |
20:19 |
|
mr_lou |
More of a generalization. If statements in a paranthese would return true or false depending on success or not, it would also apply to such simple things. |
20:20 |
|
mr_lou |
I think PHP returns true/false |
20:20 |
|
aditsu |
they would only return true or false if the result is a boolean |
20:20 |
|
aditsu |
7 is not boolean |
20:20 |
|
mr_lou |
I think PHP don't care. |
20:20 |
|
mr_lou |
Thought it might be the same with Java |
20:20 |
|
aditsu |
php probably casts non-zero to true and zero to false |
20:21 |
|
aditsu |
maybe non-null and null in a similar way |
20:22 |
|
aditsu |
but why are we talking about php? |
20:22 |
|
mr_lou |
Because Java returning null confused me because PHP returns false |
20:25 |
|
aditsu |
java can return null, false, 5, "foobar" or any possible value in the world, depending on what expression you are using |
20:26 |
|
aditsu |
and I bet the same is true for php |
20:29 |
|
mr_lou |
So it's just the return value. It's not the expression. |
20:29 |
|
mr_lou |
https://www.php.net/manual/en/function.fgets.php |
20:29 |
|
mr_lou |
PHP returns false, because fgets() returns false. |
20:29 |
|
mr_lou |
And Java returns null because readline() returns null. |
20:30 |
|
mr_lou |
Ok, bedtime. |
20:30 |
|
mr_lou |
G'nite. |
20:30 |
|
aditsu |
well, if you call readline() then it returns whatever readline() returns |
20:30 |
|
aditsu |
g'night |