[Solved] In JDK1.8, is it necessary to use ConcurrentHashMap on all occasions that require synchronization?

No, it is not necessary to replace use of Collections.synchronizedMap with ConcurrentHashMap when used by multiple threads. The Concurrent Collections section in the javadoc for package java.util.concurrent says: “Synchronized” classes can be useful when you need to prevent all access to a collection via a single lock, at the expense of poorer scalability. In other … Read more

[Solved] Object Array Initialization [duplicate]

Variables and array elements are all references to objects. They are not the actual objects. To illustrate, I’ll “name” actual objects using a number scheme, e.g. #1 is the first object. Your code runs as follows: Class is initialized by the JVM, and root1, root2, and root3 are all null. Node root[]={root1,root2,root3} is executed and … Read more

[Solved] NullPointerException When trying to get string element from array

I just tested your code. Its working fine. no error I am getting output PC public class StackOverFlow { public static String[] CONSOLES = { “PC”, “PS4”, “XBOX ONE”, “PS VITA” }; public static void main(String arg[]){ System.out.println(getConsoleName(0)); } public static String getConsoleName(int ID) { String rtext = “unknown”; try { rtext = CONSOLES[ID]; } … Read more

[Solved] How to invert a word? [closed]

You can use StringBuffer or StringBuilder for this task, StringBuilder would be my choice since its more efficient. its not thread safe so multiple threads can call its methods simultaneously. String reversedString = new StringBuilder(originalString).reverse().toString() If you prefer not to use API support you can do something like this static String reverse(String stringIn) { char[] … Read more

[Solved] Reassign reference returned by a method

It’s simply how Java is specified. From JLS Sec 15.26, the grammar of the assignment is given by: Assignment: LeftHandSide AssignmentOperator Expression LeftHandSide: ExpressionName FieldAccess ArrayAccess So the LHS has to be either a variable, a field or an array element. Anything else is invalid syntax. The reason why you can’t reassign a method’s return … Read more

[Solved] Size difference between base class and derived class

Because you declared: Box simpleBox = new Box(); In this case, the “simpleBox” variable is declared of type “Box” this means that you can re-assign any object instance to it that is assignment compatible with Box. At declaration time, you’ve given it a value which happens to be of that same class. Since wt is … Read more

[Solved] sort int string java [closed]

The reasons for this question getting downvoted are: There’s no effort shown. Tell us what you’ve tried and what you know. We love code. Please show us your code. Even the non-working one. If one thinks about the problem for a while, unanswered questions begin to pop up. For example: Do all the numbers have … Read more

[Solved] When do you use a String over int [closed]

Conventionally, you use the type that best fits your scenario. If you need to do math associated with a number, use a datatype capable of that, like int, double, long, float, etc. If you don’t need to perform math on it, or you’re using it as a label, then a String is acceptable. (In all … Read more

[Solved] Using String#contains() and String#indexOf() to locate a date format String in a comma separated list finds incorrect matches [closed]

Currently you are not searching for a single date format in a list of date formats, you are searching for a date format String within a single String that contains multiple comma separated date formats. Since that String starts with “dd/MM/yyyy”, which contains “dd/MM/yy”, both contains and indexOf find a match (which, given your requirements, … Read more

[Solved] Try/catch doesn’t run full code

Most probably your code runs into an exception. Add a e.printStackTrace() to your catch clause and execute your application again to see what is printed on the console. try { // your code } catch (Exception e) { e.printStackTrace(); } That should help. 1 solved Try/catch doesn’t run full code

[Solved] Java and C: different outputs of same code [closed]

Although that code would compile neither in C nor C++, your basic problem is here: System.out.println(h.m1() + “……” + h.m2() + “…….” + h.m3()); In C and C++ there are no constraints on how the compiler orders those calls. It could call m3 then m2 then m1, or m2 then m3 then m1, etc, etc… … Read more

[Solved] What is app control panel [closed]

usually when somebody wants to make a mobile application, he creates a control panel using any back-end (php, java, node.js …etc) so he can control the API (if your app uses API call), he may also want to disable the application, so in each run in the application side, an API call will be invoked … Read more