Win a copy of liveProject: Protecting User Data with Spring Security and OAuth2 this week in the Spring forum!

Jeff Stam

Greenhorn
+ Follow
since Aug 27, 2019
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
3
Given in last 30 days
2
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jeff Stam

@Campbell Ritchie

Would you please help me so I can solve your challenge.
I am still a beginner and eager to learn but I am stuck.
Every notification on my phone I hope it is an update of this topic but unfortunately so far it isn't.
It is probaly very simple but I cannot connet it to a Java learn objective.

Thanks in advance.
@Campbell Ritchie

I'm still trying to solve your challenge and searching for few hours my conclusion is that it is not possible to get args[0] to be null.
If I do not provide any arguments on the command line, then the String array of main() method will be empty but never null.
Right?

I hope you will reveal you quiz question so I can learn from this.

I meant how can you get args[0] to be null


Without assigning it?
I'm afraid I have to say I don't know.

No, the first three operands are operands of &&. It is only after the &&s have been evaluated that the whole expression (as far as !false) becomes the left operand of the ?: operator.


Yes, thanks I understand now.

I presume the correct answr is A.


correct answer is A.

Little challenge: under what circumstances can the leftmost expression, with != null) evaluate to false.


I accept the challenge....see below code.

Yes, thanks Mike this has broken my tunnel vision.
The first three operands are a part of the ternary expression.
Hello everyone,

Can anyone help me to understand this piece of code from  OCA/OCP Java SE 8 Programmer Practice Tests CH3Q46.

46. Which of the following is not a possible result of executing the following application?

A. Nothing is printed.
B. The application throws an exception at runtime.
C. Go Outside is printed.
D. Stay Inside is printed.

When I compile and then run from command-line with java OutsideLogic sunny then it outputs "Go Outside".
When I do the same with another argument e.g. java OutsideLogic test then it outputs "Stay Inside".

I do not understand that the third and last operand of && is a ternary operator and outputs a String.
When I flip the ternary operator to the first or second operand of && then it produces an error.
e.g. System.out.print(!false ? "Go Outside" : "Stay Inside" && args[0]!=null && args[0].equals("sunny"));
or
System.out.print(args[0]!=null && args[0].equals("sunny") && "test");
error: bad operand types for binary operator '&&'

Please help to understand why the third operand of && is not a boolean and (can) outputs a String.
Thanks in advance.

Roel De Nijs wrote:

AngelinaJeana Brown wrote:Can someone explain how many objects are eligible for gc and when they become eligible?


And for 1 bonus point: how many objects will be eligible for GC if youngestChild is a class variable instead of an instance variable?



This is what you said in this post:

Please be aware that only objects can be eligible for GC, reference variables are never eligible for GC. And the number of reference variables with null as "value" don't tell anything about the number of objects eligible for GC.



So I think if it is a class variable it doesn't affect the garbage collection of the objects.
"Trying to set a short variable to an int results in a compiler error."
should be
"Trying to set an int variable to a short results in a compiler error."

Right?

Example:
int y = 5;
short x = y; // cannot convert from int to short
Aha I didn't noticed the attachment button, this will work I hope.
Can anyone help me with this from Boyarsky/Selikoff OCA Java 8 Pratise Tests exam question CH6Q15?



15. Given the following application, which diagram best represents the state of the
mySkier, mySpeed, and myName variables in the main() method after the call to the
slalom() method?

A:
mySkier -> null
myName  -> "Rosie"
mySpeed -> int[] 18

B:
mySkier -> Ski.age 18
myName  -> "Wendy"
mySpeed -> int[] 11

C:
mySkier -> Ski.age 18
myName  -> "Rosie"
mySpeed -> int[] 0

D:
mySkier -> Ski.age 16
myName  -> "Wendy"
mySpeed -> int[] 0


Answer in book:
To solve this problem, it helps to remember that Java is a pass-by-value language in which copies of primitives and object references are sent to methods.
This also means that an object’s data can be modified within a method and shared with the caller, but not the reference to the object.
Any changes to the object’s reference within the method are not carried over to the caller.

In the slalom() method, the Ski object is updated with an age value of 18. Although, the last line of the slalom() method changes the variable value to null,
it does not affect the mySkier object or reference in the main() method.
Therefore, the mySkier object is not null and the age variable is set to 18, making Options A and D incorrect.

Next, the name variable is reassigned to the Wendy object, but this does not change the reference in the main() method, so myName remains Rosie.

Finally, the speed array is assigned a new object and updated. Since the array is updated after the reference is reassigned,
it does not affect the mySpeed array in the main() method.
The result is that mySpeed continues to have a single element with the default int value of 0. For these reasons,
Option B is incorrect, and Option C is correct.



My conclusion and struggle:
I got a little bit confused because answer to this part says: the name variable is reassigned to the Wendy object,
but this does not change the reference in the main() method, so myName remains Rosie.

Reassigned to the Wendy object? Wendy an object?
Also I do not fully understand why the IDE warns that method Parameter name is not used?
Is this because it is a void method and doesn't return a value or it doesn't print out a value in the method?
Because when I add System.out.print(name); in method or change to a non-void method and return name then the warning disappears.
So my conclusion is that myName doesn't have any relation with name. Both a different address in String-pool.
Right?
Also it feels strange to pass a final variable to a method where it is altered.

Passing the complete mySkier instance as parameter to slalom method is, I assume, a polymorphic parameter objective.
So racer is infact mySkier instance and racer can change instance variables of mySkier, right?
And why is this assigned value never used? -> racer = null;

And last in my opinion has int[] speed nothing to do with mySpeed which is initialized with new int[1]; what produces 0.

P.S.
I started learning (myself) Java a few month's ago with an amateur level PHP background for several years and I really want to take the 1Z0-808 exam
Sometimes I'm suffering from a tunnelvision and need some help to broaden my research horizon and appreciate to get some advise.
Thanks in advance.

So you can assign an int[][] to a reference of declared type Object[]. So all the code will compile.



I would like to understand how I can access and print the Object[] elements and beside that how to check what type game is ?

game instanceof Object is always true right?
and
obj[0].getClass().getComponentType() returns int
and
game.getClass().getComponentType() returns class [I

Can you help me to understand this objective so I can learn?
I really will appreciate it, thanks.
Hi,

Can anybody help me to understand this question and answer?
I can't link line 7 en 8 to OCA Fundamentals and key learning point for exam.
What exactly happens in these two lines?
Though I understand you can't assign a String to an int,
and I'm trying to unravel how to avoid ArrayStoreException in this example.
How can test if obj[3] is really an int[] behind the scenes and not an int.


28. What is the result of running the following program?



A. X
B. The code does not compile.
C. The code compiles but throws a NullPointerException at runtime.
D. The code compiles but throws a different exception at runtime.


28. D.
Line 6 assigns an int to a cell in a 2D array. This is fine. Line 7 casts to a general Object[]. This is dangerous, but legal. Why is it dangerous, you ask? That brings us to line 8. The compiler can’t protect us from assigning a String to the int[] because the reference is more generic. Therefore, line 8 throws an ArrayStoreException because the type is incorrect, and Option D is correct. You couldn’t have assigned an int on line 8 either because obj[3] is really an int[] behind the scenes and not an int.
Hi Brecht,

If one of the operands of ^ is true, then the result is always true.



So assuming, that if one of the operands is true the other is false, is wrong?



https://www.baeldung.com/java-xor-operator

36. What statement about the ^ operator is correct?
  A. If one of the operands of ^ is true, then the result is always true.
  B. There is a conditional form of the operator, denoted as ^^.
  C. If both operands of ^ are true, the result is true.
  D. The ^ operator can only be applied to boolean values.

Option D is said to be correct: Finally, Option D is correct as the ^ is only applied to boolean values in Java.
But there is a bitwise ^ operator in Java too, so it can be applied to both boolean and numerical values.



Errata says: Option D is incorrect if you have extra knowledge. So you give yourself credit if you wanted to choose “none of the above”

None of the above?  
In my opinion A is the correct answer. Right?
Please help me explain.
Thanks.