Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!

Ole Sandum

Ranch Hand
+ Follow
since Sep 27, 2013
Ole likes ...
IntelliJ IDE Java Windows
Cows and Likes
Cows
Total received
3
In last 30 days
0
Total given
0
Likes
Total received
18
Received in last 30 days
0
Total given
18
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ole Sandum

Sergiu Dobozi wrote:So addActionListener really in fact is taking just a reference to an object of the class it is in as a parameter.


No. It takes a reference to any object that implements the ActionListener interface. Which the current class in your example happens to do.
3 years ago

Ioanna Katsanou wrote:

Stephan van Hulst wrote:That's because in Java, operator precedence does not determine order of evaluation. Order of evaluation is always left to right.

Java will evaluate the a on the left of the addition operator before it evaluates the assignment expression on the right of the addition operator. Here are the steps that are performed:



Yes but my question is the following:
why in the bellow case it would first evaluate the parenthesis, and in the above case not?


What is the difference in this scenario???



To clarify. In this case it is still evaluating from left to right. That just happens to produce the same result as evaluating the parentheses first.
3 years ago

Ioanna Katsanou wrote:

Stephan van Hulst wrote:That's because in Java, operator precedence does not determine order of evaluation. Order of evaluation is always left to right.

Java will evaluate the a on the left of the addition operator before it evaluates the assignment expression on the right of the addition operator. Here are the steps that are performed:



Yes but my question is the following:
why in the bellow case it would first evaluate the parenthesis, and in the above case not?


What is the difference in this scenario???



In this scenario it wouldn't matter if the parentheses were evaluated first or not. It would still give the same result. In Java, though, it is evaluated from left to right.
3 years ago
Even though the parentheses take precedence over the plus, the expression is still evaluated from left to right.

So we have
a = 10;
a = a + (a = 4);
--evaluating from left to right gives:
a = 10 + (a = 4);
--then
a = 10 + (4); // a is now 4
--then
a = 14;

a is 14
3 years ago

Junilu Lacar wrote:Then read Chapter 2 on Names to see why I and O are horrible names.



Well, they are generic type parameters after all. Would you also say names like T, R, K, V and E are also horrible?
3 years ago

Ole Sandum wrote:
Was this the one in London, by any chance?



Of course it was. Silly me didn't bother to click the link before posting.
3 years ago
Looks very interesting. Definitely will watch it when I find the time. Thank you for this.
Was this the one in London, by any chance?
3 years ago
Well, you didn't ask a question. What is your doubt regarding this?
3 years ago
I started this thread a while ago while learning the ins and outs (pun intended) of Kotlin. Back then, this thing confused me mainly because of two things:

1. I wasn't really experienced with Kotlin. The transition from Java was a bit tough.
2. Java being the only programming language in which I had previously used Generics, the notion of declaration site variance was new to me (Java only has use site variance)

Now, coming back to this thread and reflecting, I can see why I was confused. There were a number of new concepts in play. Not just one.

First, there is declaration site variance. This seemed intuitive at first, but I realised there was a bit more to it than I initially thought. And combining this with use site variance didn't make it any easier.
Then, there was something the tutorial completely failed to mention. Namely, the type Nothing acts in Kotlin as a subtype of all types. This was such an important part to understanding the whole idea, that it baffles me that they left it out.

Now, having worked with both C# and Kotlin in a couple of projects, this is no longer an issue to me, but i felt it was important to come back and clarify this just in case someone else comes along with the same problem.

Thank you again, for helping
3 years ago

Jeanne Boyarsky wrote:In the real world, you are correct. In the simplified world of the exam, the singleton is shared more broadly. On the bright side, the real exam tells you how many answers are correct. If it said "choose three", I bet you'd have included F!



No. I think this would be an unacceptable mistake in the question if it happened on the real exam. A singleton simply does not require the instance to be publicly available. Moreover, a method isn't the only way to expose data.

Now, I realise that using a method to return the instance follows encapsulation and all that, but in the case of a singleton I do not think it adds any value.

In my opinion, the best way to expose a singleton instance is with a (public) static final field initialised at declaration.

So that's at least one alternative. I know the exam likes to be vague and tricky, but here I think it is just wrong.
Would you kindly post the code again, with the missibg end of that method added?
3 years ago

Suhaas Mohandos wrote:1. So if I use new in the called method, the change will not be reflected in the calling method?


That depends entirely on what you assign the value to. If you assign the new value to the parameter variable, no change will be reflected outside the method (because of pass-by-value). If you alter the object referred to by the parameter variable - for instance, you assign the value to an instance field of the referenced object - then you will see the change reflected outside the method. Not because of pass-by-reference (there is no such thing in Java). But because multiple variables can point to the same object.

Suhaas Mohandos wrote:2. If you are saying primitives are pass by value and objects are also passed by value in java then give me a fresh example of both(primitive and object) where the change is not reflected in the calling method?


Certainly:

Output:
----
myArray1: [2, 4, 8, 16, 32, 64]
myArray2: [1, 1, 2, 3, 5, 8]
myClass1: MyClass with name: John
myClass1: MyClass with name: Martha

myArray1: [2, 4, 8, 16, 32, 64]
myArray2: [1, 1, 2, 3, 5, 8]
myClass1: MyClass with name: John
myClass1: MyClass with name: Martha
----

As you can see, no change.

Suhaas Mohandos wrote:3. When  they say java is pass by value does it mean that changes made in the called method will not affect the calling method?


It means changes made to the parameter variable will not affect the variable that was passed to the method. That is, if you make the parameter variable point to another value, the passed variable will not point to the same value. Because, it isn't really the variable that is passed to the method, it is its value that is passed. Hence pass-by-value.
3 years ago
Your two examples are fundamentally different. In the first example you are changing a value contained in the array. In the second example you are changing the value of the local parameter variable itself.

If the first example had been 'value = new int[] {150}' or the second had been 'd.name = "Fifi"' (or, ideally, 'd.setName("Fifi")'), then you would see similar results for both.
3 years ago

Jesper de Jong wrote:That has to do with generics, specifically with wildcards.

This is sometimes useful, but it's also a little more advanced and confusing to understand. One very common problem is that people think a Set<?> is a set that can contain any kind of object, but that's not the case; it's a Set that contains objects of one specific type, but what that type is, is unknown. This is sometimes useful if the method that works on the Set for example doesn't care or need to know what exactly the type of the objects in the Set is.



...but more often than not, you see bounded wildcards, like Set<? extends CharSequence> and Set<? super String>.

The former meaning "a Set that contains objects of some unknown type that is a subtype of CharSequence", and the latter meaning "a Set that contains objects of some unknown type that is a supertype of String>. These reference types contain more type information than an unbounded wildcard type, and are more useful in most (not all) cases.
3 years ago