Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp

ethan crown

Greenhorn
+ Follow
since Apr 17, 2016
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
14
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by ethan crown


I have an object that has several key/values and it’s being passed as an argument to my function’s parameter as an array.

I need that function to iterate through said array that has objects in its indexes and it also needs to check if any of the array’s indexes key/values pairs have a string.

If there are strings, have those key/value pairs with strings be inside a new array that will be used to return it to the function.

The function’s returned value will be tested in a comparison in a TDD were an actual value is compared with an expected value.

In this case an array with an object with string values.


//TDD TEST
function test(actual, expected) {
   if (JSON.stringify(actual) === JSON.stringify(expected)) {
     console.log("Yay! Test PASSED.");
   } else {
     console.error("Test FAILED. Keep trying!");
     console.log("    actual: ", actual);
     console.log("  expected: ", expected);
     console.trace();
   }
 }

//The objects

const obj1 = { a: "A" };
const obj2 = { a: "A", b: 2 };
const obj3 = { a: "A", b: 2, c: "C", d: true };
const obj4 = { a: "A", c: "C" };

//My solution up to now
function filterArrayForStrings(arrayOfObjects){
 let newArrayWithObjects = [];
 for(const key in arrayOfObjects){
   if(typeof arrayOfObjects[key] === 'string'){
     newArrayWithObjects.push(arrayOfObjects[key]);
   }
   
 }
 return newArrayWithObjects;
}


test(filterArrayForStrings([obj1]), [obj1]);

Knute Snortum wrote:Try this:

C:\Users\userNAME\javaprojects> javac -d bin src/HelloWorld.java

or

C:\Users\userNAME\javaprojects\src> javac -d ..\bin HelloWorld.java




Thank you very much. this is exactly what I needed to understand as to why I was getting the kind of errors and or outcomes I was not expecting.
2 months ago
I do know that you need the  "javac -d "  command from the console to generate the .class file but I am having trouble writing the path correctly. I have a source code file in javaprojects\src directory, I wish to compile it and have its .class file be created and stored in a separate folder named javaprojects\bin

when using the command:

C:\Users\userNAME\javaprojects\src> javac -d bin   HelloWorld.java

When I compile the .java file the .class file is created along with a new duplicate "bin" folder:

javaprojects\bin\src\bin HelloWorld.java


I do not know what is the proper way to just create the .class file in the folder I need it to be create it at. I have searched
but I am not finding anything on the matter. Any help would be greatly appreciated, thank you.

EDIT:
I'd like to thank you all for the helpful replies. For some reason or another I am unable to post any replies!

EDIT2:
Tried both methods that were advised and they work, thank you again. Now though my issues is that whatever named package
I write in my source code file, e.g: "package mypackage;" , is created within the "bin" folder after I compile the HelloWorld.java file:
javaprojects\bin\mypackage HelloWorld.class
2 months ago
The links do provide a wide variety of topics centered on my question. but it does provide the direct link to the JLS`s topic: 14.21. Unreachable Statements. This is much appreciated, thank you.
This helps clarify my confusion. Thank you, for taking the time.
I am a bit confused about the unreachable statement error and as to why that is.
I do understand how it happens and why is happening, the throw statement is
always thrown leaving the print statement unreachable. but some how I do not get
it? I am bit confused. Is it that the program must reach that statement otherwise
if it can not then it falls into a loop, making it uncompilable?
Thank you in advance for any clarification.







Yes indeed, this is a question from the Enthuware mock test. Thank you for the heads up, that’s an essential thing you’ve pointed out for me. I will be doing this exactly.
Thank you for taking the time to look into this for me. Now it makes sense. I will be going over this with my notes and book.
I am confused by the following code, I am under the assumption that this is an overloaded method but
the code runs. I thought that it would fail to compile because the method in the Beta class has the same
type, access level, and return. Any help would be greatly appreciated and thank
you in advance.

Yes you are right, it was what I was referring to. As in Java
just about everything is an object! Thank you though
for further breaking this down for me.
OK now I am confident about my knowledge of how the creating of
a String works. @Luitauras Vilda just as you first posted, when
creating Strings using the "new" keyword there are two objects
being created. The one for the named reference variable s pointing
at the object in the normal heap which does contain the value "OCA"
and the second object the String literal value which contains "OCA"
inside the String constant pool.
The String literal value "OCA" is now available for any other to use and
the reference variable (s) is pointing at its own String literal "OCA" in
the normal heap.
Indeed, two objects one reference variable.
Thank you all for the deep insight into what at first appears to be a simple concept.
So just to further clarify:


String s = new (“OCA”);

1) Creates a reference variable named (s)
pointing at a String object.

2) A new object is instanciated and memory
is allocated for the object in the general heap.

3) The String literal “OCA” is created without
being first searched for in the String constant
pool.

4) The String literal “OCA” is placed inside the
String constant pool and the  String literal value
“OCA” can be used by any other object who will
use the same String literal value “OCA”.

5) The reference variable (s) points at the  
object which has the String literal value “OCA” inside
The String constant pool.

6) Any other reference variable can reference the object with
the String literal value “OCA” in the constant String pool.....right!?



I am preparing for the OCA Java 8 exam and I am confused in regards to strings and what
happens when they're created. I understand that Strings are created in one of two methods: by "new"
keyword and by String literals. When created by String literals, Java first looks inside the String
constant pool to see if there are any matching Strings, if non are available then a new String object is
created and a reference to it is returned. When created by the "new" keyword a new String object is
created in the non pool heap. Both String objects are not related even if both String objects have the
same characters.
I hope I have this concept correct. My question is, when creating a String object by means of the
"new" keyword, does the String object's created by the "new" keyword String literal go inside the
String constant pool and becomes available for reuse or those it permanently stay outside the pool?
I feel that because it's created in the non pool heap and because you can create Strings using both
methods and both objects won't equal to be the same but rather point to different references, Strings
created using the "new" keyword will create a non pool String and the String stays outside the pool
and cannot be use by any other reference variable?

Thank you for taking the time to clear my confusion.
Did a lot of research before asking this question. Clarification it's quite important to me, also I'm a bit obsessive. I'm studying for the OCA Java 8 cert. for the section: Know how to read and write to Object fields.  I'm confused about whether there's a difference between object fields and fields. When reading through the materials there's no difference being made and the word "field" is used for member variables; both instance variables and class variables (static variables). But the Oracle Glossary of terms explains the following: "field: A data member of a class. Unless specified otherwise, a field is not static."
For further clarification I've looked at the following resources:
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta p.g 188
OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide by Jeanne Boyarsky & Scott Selikoff p.g. 18
Oracle Java Tutorial: Declaring Member Variables
and StackOverflow

The way I interpret the information is that you have "fields" the general term, meaning members of the class, both class variables (static variables) & instance variables. And then there is "object fields" which are exclusively instance variables.
I would greatly appreciate clarification to this, thank you in advance.