понедельник, 1 января 2024 г.

Data Types in Java

    Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java:

  1. Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
  2. Non-primitive data types: The non-primitive data types include String, Classes, Interfaces and Arrays

Primitive data types have default values if they are declared as instance variables but not explicitly initialized. The default values depend on the type of the primitive data. Here are the primitive data types and their default values:

  • byte myByte; // Default value: 0, Default size: 1 byte
  • short myShort; // Default value: 0Defaut size: 2 byte
  • int myInt; // Default value: 0Defaut size: 4 byte
  • long myLong; // Default value: 0LDefaut size: 8 byte
  • float myFloat; // Default value: 0.0f, Defaut size: 4 byte
  • double myDouble; // Default value: 0.0, Defaut size: 8 byte
  • char myChar; // Default value: '\u0000', Default size: 2 byte
  • boolean myBoolean; // Default value: false, Defaut size: 1 bit

For local variables (variables declared inside methods, constructors, or blocks), they must be explicitly initialized before being used. They do not have default values.

public class Example { public void myMethod() { int localVar; // Error: Variable 'localVar' might not have been initialized // Need to initialize 'localVar' before using it } }

Remember that these default values only apply to instance variables. Local variables must be explicitly initialized before use, as shown in the example above.

Non-primitive data types, also known as reference types, represent objects and are more complex than primitive types. They are created using classes and interfaces and include various data structures and objects. Here are some common non-primitive data types in Java:

Class Types:

  • Instances of user-defined classes.
  • Objects are created based on class blueprints.

  • class Person {
    String name;
    int age;
    }

    // Creating an object of the Person class
    Person personObj = new Person();


Array Types:

  • An ordered collection of elements of the same or different types.

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



Interface Types:

  • A type that defines a set of abstract methods that a class must implement.
interface Printable {
void print();
}

class Document implements Printable {
public void print() {
System.out.println("Printing document...");
}
}

String Type:

  • A sequence of characters.
  • Although it is often used like a primitive type, String is an object.
String greeting = "Hello, Java!";



Collection Types:

  • Java Collections Framework provides a set of interfaces and classes for various collection types, such as List, Set, Map, etc.

List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");

User-Defined Reference Types:

  • Any type defined by the programmer using classes or interfaces.
class Car {
String make;
String model;
}

Car myCar = new Car();




Комментариев нет:

Отправить комментарий