What Does new Do in Java?
In Java, the new keyword is used to create a new object. It is a fundamental concept in object-oriented programming (OOP) and is used to instantiate classes, objects, and arrays. In this article, we will delve into the details of what new does in Java and explore its various uses.
What is new?
new is a keyword in Java that is used to create a new object. It is a shorthand way of creating a new instance of a class. When you use new, you are essentially asking the Java Virtual Machine (JVM) to create a new object that is an instance of the class you are trying to create.
Creating a New Object
When you use new, you need to specify the class of the object you want to create. For example, if you want to create a new String object, you would use new String("Hello, World!"). The String class is a built-in class in Java that represents a sequence of characters.
Here is a simple example of creating a new String object:
String name = new String("John Doe");
Types of Objects Created with new
When you use new, you can create objects of various classes, including:
- Primitive types:
int,double,boolean, etc. - Reference types:
String,Object,Array, etc. - Custom classes:
Person,Employee,Car, etc.
Here is a table summarizing the types of objects created with new:
| Type of Object | Description |
| --- | --- |
| Primitive types | `int`, `double`, `boolean`, etc. |
| Reference types | `String`, `Object`, `Array`, etc. |
| Custom classes | `Person`, `Employee`, `Car`, etc. |
Constructors
When you create an object using new, the constructor is called automatically. The constructor is a special method that is used to initialize the object with its initial values. Constructors are used to provide default values for parameters or to initialize objects with specific values.
Here is an example of a constructor for a Person class:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
Passing Arguments to a Constructor
When you create an object using new, you can pass arguments to the constructor. These arguments are used to initialize the object with specific values.
Here is an example of passing arguments to a constructor:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void printDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person("John Doe", 30);
person.printDetails();
}
}
Destructuring
When you create an object using new, you can also use destructuring to extract the values of the object’s fields.
Here is an example of destructuring:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void printDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person("John Doe", 30);
System.out.println("Name: " + person.name);
System.out.println("Age: " + person.age);
}
}
Conclusion
In conclusion, new is a fundamental concept in Java that is used to create new objects. It is a shorthand way of creating a new instance of a class and is used to instantiate classes, objects, and arrays. When you use new, you need to specify the class of the object you want to create, and you can create objects of various classes, including primitive types, reference types, and custom classes. Constructors are used to initialize objects with their initial values, and destructuring is used to extract the values of the object’s fields.
By understanding the basics of new in Java, you can write more efficient and effective code that takes advantage of the language’s features and capabilities.
