Cosmid Constantine

+ Follow
since Nov 13, 2020
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Cosmid Constantine

I finally got it! Because there's a = in all of these, so it's not modifying any of these objects but instead it keeps referencing a newly created object. And there is only one line without the = and that's where it's trying to modify a string object and causes an error. Thank you for your patience to keep explaining it to me!

I think taking a break also helped, haha
1 year ago
Right, it's trying to modify the object that s2 is referencing in the 2nd example. My confusion is why the string object referenced by s in the first example can be modified but the string object referenced by s2 in the 2nd example can not be modified.
1 year ago
That's awesome-Congratz!
1 year ago

The book provided the following example:

String s = "1";
s += "2";
s += 3;

Which prints 123. It's trying to show the reader when the + went into addition mode or concatenation mode. Then it jumps into Immutability.

"Once a String object is created, it is not allowed to change."

It then provided an example of Immutability:

String s1 = "1";
String s2 = s1.concat("2");

It prints "12".

Which really confused me since the two conflicts each other. Is it trying to say, a string object can be modified using the + operand but it can not be modified through the concat method?

Thanks in advance!
1 year ago
What's a cow? I mean, what does a cow mean on here.
1 year ago

Zachary Griggs wrote:So, in "a = ++n", the ++n operator means the ++ takes place first. In other words, it's essentially doing the following: "n++; a = n"
In "b = n++", the ++ happens last. So you can think of it as: "b = n; n++"

That is a good explanation. Thank you. My confusion wasn't about the final value of a or b from n++ or ++n. I understand how both the pre and post increment and decrement works. What confused me was the precedence of the two. Like I said in my question earlier, if post increment has a higher precedence than pre increment, why does it take effect later than pre. I mean, shouldn't Java use the pre increment to hide the increased value?

So my understanding is that even though the n++ has higher precedence and is evaluated first than other operators, but it just has one special talent, it hides its calculated value.

I am bad at asking questions. It's hard for me to express my question. Sorry about that. And thanks for everyone's help!
1 year ago

Sample program:
 n = 0;
 a = ++n;
 b = n++;

If the post-increment or post-decrement operator has higher precedence than the pre-increment or pre-decrement operator, then why b didn't get a value of 2? I mean, it would make more logical sense if a = 0 and b = 2 with the precedence of the two.

I guess my question is, if n++ has higher precedence, why didn't it apply the ++ effect sooner than the ++ on the ++n? Is this just a special case in Java?

1 year ago
Thanks Ritchie! Yes, it is the Java11 cert exam books by Boyarsky and Selikoff. I have the three in one version, 1Z0-815, 816, and 817. Maybe the contents are compressed and some of it isn't explained very well. I'm glad the book pointed out this website and there are good people like you to help me out!
1 year ago

The book provided 2 situations when an object is eligible for garbage collection when they are no longer reachable:

1. The object no longer has any references pointing to it
2. All references to the object have gone out of scope

I understand the first one. But can someone provide me an example of the 2nd one?

1 year ago
Ahh, I see. Thanks for explaining that to me.
1 year ago
Thanks for the replies everyone! Sorry I don't know how to reply to a specific person. I couldn't find the 'reply' button for each comment, like on most forums.

@Campbell, the name of the book is OCP Oracle Certified Professional Java SE11 Developer Complete Study Guide. I had the .java in the command but forgot to copy over my .java file to that directory.

@Norm, yes, the book was trying to show how to use the -cp option but only provided an example with java and not javac. It first exemplified with a -d option using the javac, so the classes directory didn't include any .java files and that's why I failed with my javac -cp command.

@Tim, yes, the book tried to show the 3 different versions of the -cp options, I honestly think the --class-path is useless and is there only to make people lose points on the exam. Thanks for providing the example, a bit complicated, but helped me understand it better.
1 year ago
Hi Stephan,

Sorry for my late reply. Still trying to get use to this website.

After several tries, the command finally worked for me and produced the right result.

Thanks again for your help!

1 year ago

The book gives an example of -cp using the java run command:

java -cp classes packageb.ClassB

It worked. But when I try to compile ClassB using the javac -cp it gives me two errors:

error: Class names, 'packageb/ClassB', are only accepted if annotation processing is explicitly requested

Here's the file structure:

Only two files in the directory:, ClassB.class

Can someone please show me how to use the -cp option with the javac command?

1 year ago
I guess I'm confused about what the 'dir' represents here.

Assuming the full working directory is /Home/A/B

I even replaced 'dir' with the full path like this:

jar -cvf myNewFile.jar -C /Home/A/B .

And it's still not working. I typed the above command while at my current directory is /Home/A/B

I guess my question is what goes after the -C?

1 year ago

I am trying this command:

 jar -cvf myNewFile.jar -C dir .

The message I received:

 dir/. : no such file or directory

However, it works without the -C option:

 jar -cvf myNewFile.jar .

I even replaced the 'dir' with the complete path with and without the '.' at the end. I also tried with and without the 'dir' itself. Also, I'm using a Mac.

Thanks in advance for your help!
1 year ago