JavaScript type data type in Java?

I have experience with JavaScript, but new to Java. JavaScript has “objects” of data types in which a given variable essentially has sub-variables with their own unique values, for example:

var car = {type:"Fiat", model:500, color:"white"}; 

It's almost like an array, but not quite (JavaScript has arrays too). I am wondering if the same type of thing exists in Java? If so, how would you declare the same thing in Java? Based on my searches, I cannot find the data type of the object, but I thought maybe there was something like that?

+5
source share
3 answers

While Java has a type called object , this is not what you want. Almost everything in Java is an object, and there are two ways to handle this:

  • Define a strongly typed object with the correct properties as a class. Javascript has a similar concept, implemented in different ways, but it should be recognizable:

     public class Car { private String type; private int model; private String color; public Car(final String type, final int model, final String color) { this.type = type; this.model = model; this.color = color; } public String getType() { return this.type; } public int getModel() { return this.model; } public String getColor() { return this.color; } } final Car car = new Car("Fiat", 500, "white"); // To get the color, you must: car.getColor(); 

    Add methods to get and set attributes as needed, start, stop, disk, etc.

  • If you need a free collection of properties without behavior or restriction, use Map . Again, there is a Javascript equivalent (the {x: 1, y: 2} construct without using the new keyword).

     final Map<String, Object> car = new HashMap<>(); car.put("type", "Fiat"); car.put("model", 500); car.put("color", "white"); // To get the color, you: car.get("color"); 

    The disadvantage of this approach is that the compiler cannot force the types of these objects (almost the same way), and the map cannot have custom behavior (in any reasonable way). In your example, model was a number, but that would allow you to assign something regardless of whether it makes sense (maybe someone is storing data on the server and using HttpConnection , all your code expecting the number to explode).

In Java, it is recommended that you use the first method if you know that you will have several cars, all with the same (or similar) properties. This allows the compiler to both provide and optimize the properties that, as you know, will exist, and inheritance allows you to add additional properties later. The class also allows you to define methods that work on this instance, which can help create an abstraction between parts of the system (you do not need to know how the machine starts, you just tell the machine to start it yourself).

For reference, Javascript equivalents:

 // #1 var Car = function(type, model, color) { this.type = type; this.model = model; this.color = color; } var car = new Car("Fiat", 500, "white"); // #2 var car = {type: "Fiat", model: 500, color: "white"}; // For either option, to get the color you can simply: console.log(car.color); 

What should stand out most obviously is that Java keeps track of the type of each variable. It is not visible that Java will not allow you to assign unknown properties, say car.mileage , where Javascript will happily add a new property. Finally, Java has the concept of visibility and makes things private (invisible to external viewers) by default. Replication of this in Javascript will look something like this:

 var Car = function(type, model, color) { var _type = type; var _model = model; var _color = color; this.getType = function() { return _type; } this.getModel = function() { return _model; } this.getColor = function() { return _color; } } console.log(car.getColor()); 

In Javascript, you use closure to hide data. Java is hidden by default and requires you to provide data when you need it. This is an interesting choice, very important when comparing code bases, and can help keep classes independent of each other. It's also very easy (and tempting) to break when you start writing in OO languages, so something you need to keep in mind (using simple structures, will come back to haunt you).

+8
source

Yes, they are called objects and are defined by classes. This is primarily the first thing you learn when learning Java.

 //The definition of an object and it members public class Car { String type, model, color; } 

Then you can make them public for access and change them external to the class

 public class Car { public String type, model, color; } 

And address them like that

 //Create an instance of a Car, point to it with variable c and set one of it properties/members Car c = new Car(); c.type = "Fiesta"; 

But resolving class variables editable from the outside is considered bad form in Java, usually you add methods to access them called accessors

 public class Car { private String type, model, color; //Return the type of this object public String getType(){ return type; } //Set the type of this object public void setType(String type){ this.type = type; } //etc } 

Then refer to them like that

  Car c = new Car(); c.setType("Fiesta"); 

A class is a template that you write to create objects that are instances of class runtime.

+1
source

The most enjoyable object for this purpose is LinkedHashMap . You can use it as a map, but at iteration it will store the order of the keys.

+1
source

Source: https://habr.com/ru/post/1206358/


All Articles