DEV Community

mohandass
mohandass

Posted on

Rule for Creating a class And Data Types In java

Rule for Creating Class

  • Using Class keyword to create a class.
public class Home
{


}
Enter fullscreen mode Exit fullscreen mode
  • Give the meaningful name in class.

  • Class name started with uppercase.

  • No special characters not allow except ( _ )underscore and ($) dollor symbol.

  • No space allowed inbetween class name.

Data Types

  • Java classifies data types into two distinct groups: primitive data types which store basic values directly, and non-primitive data types which reference complex objects and structures.

Primitive Data Types

The 8 primitive data types:

  • Java features eight built-in primitive data types designed for high-performance memory management.

1. Numeric Integer Types (Whole Numbers)

  • byte: 1 byte (8 bits).Store whole numbers from - 128 to 127.

  • short:2 bytes (16 bits). Stores whole numbers from -32,768 to 32,767.

  • int: 4 bytes (32 bits). Standard choice for integers (-2 billion to 2 billion).

  • long: 8 bytes (64 bits). Used for huge numbers.

2. Numeric (Floating-Point)

  • float: 4 bytes (32 bits). Single-precision. Useful for saving memory in massive decimal arrays.

  • double: 8 bytes (64 bits). Double-precision. The standard default for fractional math in Java.

3. Non-Numeric

  • char: 2 bytes (16 bits). Stores a single Unicode character or ASCII value. Enclosed in single quotes.

  • boolean: 1 bit (informational size). Stores only true or false values. Primarily handles conditional logic.

Static Variable

  • A static variable (also known as a class variable) is a variable that belongs to the class itself rather than to any specific instance (object) of that class. When you declare a variable as static, only one single copy of that variable is created in memory and shared among all instances.

  • Every object of the class references the exact same memory location. If one object changes the value, it changes for all others.

  • Memory is allocated only once in the Metaspace (Class Area) when the Java Virtual Machine (JVM) loads the class.

public class Car{

static String name = "Tata Punch";
 static int price = 500000;

 public static void main(String[] args) {


System.out.println(Car.name);
System.out.println(Car.price);


}

}
Enter fullscreen mode Exit fullscreen mode

OUTPUT

Top comments (0)