Przeglądaj źródła

Hello Java, again.

I foolishly thought that I couldn't learn anything from Java, but I can
(and should, probably). In this place I'll play a bit with java and see
if I can learn a few new things about it.
Lucas Stadler 12 lat temu
rodzic
commit
1116ef1a9d

+ 22 - 0
java/README.md

@ -0,0 +1,22 @@
1
# Java can surprise you
2
3
I thought I knew Java, I'd written code in it for a few years now, but I
4
was *wrong*. While I was able to solve a problem in Java, I didn't
5
really understand it, I had just developed enough mental workarounds to
6
make it appear so.
7
8
With this little project, I'll try to remedy this a bit. I have to write
9
more Java now, and I'd like to be able to say "I know Java" with
10
confidence.
11
12
Recently, I came across an interesting idea: to start playing with a
13
language using unit tests, so as to test your understanding of the
14
language and practicing testing at the same time. So I'll try this style
15
of playing on for size.
16
17
## Open questions
18
19
* output arguments
20
* string immutability
21
* copying/passing objects and primitives (when is it a copy, when a
22
  reference)

+ 18 - 0
java/pom.xml

@ -0,0 +1,18 @@
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0"
3
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
    <modelVersion>4.0.0</modelVersion>
6
7
    <groupId>lp.java</groupId>
8
    <artifactId>understanding</artifactId>
9
    <version>0.0-SNAPSHOT</version>
10
11
    <dependencies>
12
        <dependency>
13
            <groupId>junit</groupId>
14
            <artifactId>junit</artifactId>
15
            <version>4.8.2</version>
16
        </dependency>
17
    </dependencies>
18
</project>

+ 57 - 0
java/src/test/java/understanding/TestOutputParameters.java

@ -0,0 +1,57 @@
1
package understanding;
2
3
import org.junit.Test;
4
5
import static org.junit.Assert.*;
6
7
/**
8
 * Documenting the behaviour of output parameters in Java.
9
 */
10
public class TestOutputParameters {
11
	// FIXME: The name already suggests that this should go in either a
12
	//        Strings class or in a separate ImmutableObjects class.
13
	//        However, because immutable objects probably behave
14
	//        differently than mutable objects especially in the context
15
	//        of output parameters, at least a note regarding their
16
	//        interactions should be retained.
17
	@Test
18
	public void stringsAreImmutable() {
19
		String input = "the thing";
20
		this.tryAssigningAStringArgument(input, "will not be assigned");
21
		assertEquals(input, "the thing");
22
	}
23
	
24
	public void tryAssigningAStringArgument(String s, String assignment) {
25
		s = assignment;
26
	}
27
	
28
	@Test
29
	public void aMethodCantChangeItsParameters() {
30
		int output = 0;
31
		this.increment(output);
32
		assertEquals(output, 0);
33
	}
34
	
35
	public void increment(int i) {
36
		i += 1;
37
	}
38
	
39
	@Test
40
	public void thisActsLikeAnOutputArgument() {
41
		SelfChangingObject selfChangingObject = new SelfChangingObject("hello");
42
		selfChangingObject.change();
43
		assertEquals(selfChangingObject.value, "bye");
44
	}
45
	
46
	private class SelfChangingObject {
47
		String value;
48
49
		SelfChangingObject(String value) {
50
			this.value = value;
51
		}
52
53
		void change() {
54
			this.value = "bye";
55
		}
56
	}
57
}