Asked  10 Months ago    Answers:  5   Viewed   13 times

PHP has a var_dump() function which outputs the internal contents of an object, showing an object's type and content.

For example:

class Person {
  private $firstName;
  private $lastName;

  public function __construct($firstName, $lastName) {
    $this->firstName = $firstName;
    $this->lastName = $lastName;
  }
}

$person = new Person('Jon', 'Smith');
var_dump($person);

will output:

object(Person)#1 (2) {
  ["firstName:private"]=>
  string(3) "Jon"
  ["lastName:private"]=>
  string(5) "Smith"
}

What is the equivalent in Java that will do the same?

 Answers

1

It is not quite as baked-in in Java, so you don't get this for free. It is done with convention rather than language constructs. In all data transfer classes (and maybe even in all classes you write...), you should implement a sensible toString method. So here you need to override toString() in your Person class and return the desired state.

There are utilities available that help with writing a good toString method, or most IDEs have an automatic toString() writing shortcut.

Saturday, July 31, 2021
 
Pravi
 
1

I suggest you take a look at http://phpaes.com/. It's a free AES encryption library implemented purely in PHP; it's fast and very very simple to use.

At the very least, it allows you get one step closer to isolating the true source of the issue.

Thursday, April 1, 2021
 
jakubos
 
2

In your PHP encryption method, you have the following code:

for ($i = 0; $i < $ml; $i++){
  $newmsg = $newmsg . ($msg[$i] ^ $key[$i % $kl]);
}

However, $msg is not defined anywhere. That should be $message.

Saturday, May 29, 2021
 
penpen
 
5

NSDictionary is a class cluster (see the "Class Cluster" section in The Cocoa Fundamentals Guide), meaning that the actual implementation is hidden from you, the API user. In fact, the Foundation framework will choose the appropriate implementation at run time based on amount of data etc. In addition, NSDictionary can take any id as a key, not just NSString (of course, the -hash of the key object must be constant).

Thus, closest analog is probably Map<Object,Object>.

Saturday, July 3, 2021
 
Sujith
 
2

Support for singletons is not on a language level, but the language provides enough facilities to create them without any trouble.

Consider the following code:

public class Singleton {
    private static final Singleton instance = new Singleton();

    // Private constructor prevents instantiation from other classes
    private Singleton() {}

    public static Singleton getInstance() {
        return instance;
    }
}

This is an example from Wikipedia, which explains how a singleton can be made. An instance is kept in a private field, constructor is inaccessible outside the class, the method returns this single instance.

As for constructors: every class by default has a so-called default constructor which takes no arguments and simply calls the no-args constructor of the superclass. If the superclass doesn't have any accessible constructor without arguments, you will have to write an explicit constructor.

So a class must have a constructor, but you don't have to write it if the superclass has a no-args constructor.

Tuesday, August 3, 2021
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :