Non primitive data types in java w3schools example

Cheney 68 Published: 12/20/2024

Non primitive data types in java w3schools example

Here's an example of non-primitive data types in Java from W3Schools:

public class Main {

public static void main(String[] args) {

// Create a String object

String name = "John";

System.out.println("My name is " + name);

// Create an integer array

int[] numbers = {1, 2, 3, 4, 5};

for (int x : numbers) {

System.out.print(x + " ");

}

System.out.println();

// Create a double array

double[] prices = {19.95, 9.95, 7.45, 15.95, 3.95};

for (double price : prices) {

System.out.print(price + " ");

}

System.out.println();

// Create an array of booleans

boolean[] flags = {true, false, true, true, false};

for (boolean flag : flags) {

if(flag) {

System.out.print("TRUE ");

} else {

System.out.print("FALSE ");

}

}

System.out.println();

// Create a String array

String[] names1 = {"John", "Mary", "Jane"};

for (String name : names1) {

System.out.print(name + " ");

}

System.out.println();

}

}

Output:

My name is John

1 2 3 4 5

19.95 9.95 7.45 15.95 3.95

TRUE FALSE TRUE TRUE FALSE

John Mary Jane

In this example, we create several non-primitive data types in Java:

A String object: "John" An integer array (int[]): {1, 2, 3, 4, 5} A double array (double[]): {19.95, 9.95, 7.45, 15.95, 3.95} A boolean array (boolean[]): true, false, true, true, false A String array (String[]): "John", "Mary", "Jane"

We use the following non-primitive data types in Java:

Class (not shown): This is the most general type of data. All other types are subclasses of class. Interface: An interface is a collection of abstract methods and constants, not variables.

Note that there are many more non-primitive data types in Java, such as ArrayList, HashMap, and others, but these examples demonstrate the basic idea of using non-primitive data types.

Non primitive data types in Java example

I'll respond in English and provide a Java example for non-primitive data types.

Non-primitive data types in Java are also known as reference or object-based data types. These data types are not built-in, but instead, they're created using classes or interfaces. Here's an example of a simple class:

public class Person {

private String name;

private int age;

public Person(String name, int age) {

this.name = name;

this.age = age;

}

public void setName(String name) {

this.name = name;

}

public void setAge(int age) {

this.age = age;

}

public String getName() {

return this.name;

}

public int getAge() {

return this.age;

}

}

In the above example, Person is a non-primitive data type. It's an object-based data type because it has attributes (name and age) and methods (setter and getter). This class can be instantiated to create objects of the Person type.

Here's how you can use this class:

public class Main {

public static void main(String[] args) {

Person person = new Person("John", 30);

System.out.println(person.getName() + " is " + person.getAge() + " years old.");

person.setName("Jane");

System.out.println(person.getName() + " is now " + person.getAge() + " years old.");

}

}

In the Main class, we create an object of the Person type and assign it to the person variable. We then use getter methods to access its attributes and print out some information about John.

We also demonstrate how you can modify the attributes using setter methods. The output will show that initially, John is 30 years old, and after changing his name, he's still 30 years old because we didn't change his age yet. This illustrates the concept of encapsulation, which is a fundamental principle in object-oriented programming (OOP).

In summary, non-primitive data types in Java are classes or interfaces that provide structure for your code. They allow you to create objects with attributes and behaviors, making it easier to model real-world situations and solve complex problems.

Non-primitive data types include:

Classes: These define custom objects that have attributes (data) and methods (functions). Interfaces: These define a contract or blueprint for classes that implement them. Abstract classes: These are like interfaces, but they can also contain method implementations. Enumerations: These represent a set of named constants.

In conclusion, non-primitive data types in Java provide the building blocks for creating robust and maintainable software systems. They allow you to model complex scenarios, encapsulate data, and hide implementation details from other parts of your code.